11"""Test feature base dir."""
22import pytest
33
4+ NOT_EXISTING_FEATURE_PATHS = [
5+ '.' ,
6+ '/does/not/exist/' ,
7+ ]
8+
49
510@pytest .mark .parametrize (
6- 'base_dir' , [
7- '.' ,
8- '/does/not/exist/' ,
9- ]
11+ 'base_dir' , NOT_EXISTING_FEATURE_PATHS
1012)
1113def test_feature_path_not_found (testdir , base_dir ):
1214 """Test feature base dir."""
1315 prepare_testdir (testdir , base_dir )
1416
15- result = testdir .runpytest ('-k' , 'test_not_found ' )
16- result .assert_outcomes (passed = 1 )
17+ result = testdir .runpytest ('-k' , 'test_not_found_by_ini ' )
18+ result .assert_outcomes (passed = 2 )
1719
1820
1921def test_feature_path_ok (testdir ):
2022 base_dir = 'features'
2123 prepare_testdir (testdir , base_dir )
2224
23- result = testdir .runpytest ('-k' , 'test_ok' )
24- result .assert_outcomes (passed = 1 )
25+ result = testdir .runpytest ('-k' , 'test_ok_by_ini' )
26+ result .assert_outcomes (passed = 2 )
27+
2528
29+ def test_feature_path_by_param_not_found (testdir ):
30+ """As param takes precendence even if ini config is correct it should fail
31+ if passed param is incorrect"""
32+ base_dir = 'features'
33+ prepare_testdir (testdir , base_dir )
2634
27- def prepare_testdir (testdir , base_dir ):
35+ result = testdir .runpytest ('-k' , 'test_not_found_by_param' )
36+ result .assert_outcomes (passed = 4 )
37+
38+
39+ @pytest .mark .parametrize (
40+ 'base_dir' , NOT_EXISTING_FEATURE_PATHS
41+ )
42+ def test_feature_path_by_param_ok (testdir , base_dir ):
43+ """If ini config is incorrect but param path is fine it should be able
44+ to find features"""
45+ prepare_testdir (testdir , base_dir )
46+
47+ result = testdir .runpytest ('-k' , 'test_ok_by_param' )
48+ result .assert_outcomes (passed = 2 )
49+
50+
51+ def prepare_testdir (testdir , ini_base_dir ):
2852 testdir .makeini ("""
2953 [pytest]
3054 bdd_feature_base_dir={}
31- """ .format (base_dir ))
55+ """ .format (ini_base_dir ))
3256
3357 feature_file = testdir .mkdir ('features' ).join ('steps.feature' )
3458 feature_file .write ("""
@@ -37,24 +61,71 @@ def prepare_testdir(testdir, base_dir):
3761 """ )
3862
3963 testdir .makepyfile ("""
40- import pytest
41- from pytest_bdd import scenario
4264 import os.path
4365
66+ import pytest
67+
68+ from pytest_bdd import scenario, scenarios
69+
70+ FEATURE = 'steps.feature'
71+
72+
4473 @pytest.fixture(params=[
4574 'When scenario found',
4675 ])
4776 def scenario_name(request):
4877 return request.param
4978
50- def test_not_found(scenario_name):
51- base_dir = '{}'
52- print("BS: %s" % base_dir)
79+
80+ @pytest.mark.parametrize(
81+ 'multiple', [True, False]
82+ )
83+ def test_not_found_by_ini(scenario_name, multiple):
5384 with pytest.raises(IOError) as exc:
54- scenario('steps.feature', scenario_name)
55- assert os.path.abspath(os.path.join(base_dir, 'steps.feature')) in str(exc.value)
85+ if multiple:
86+ scenarios(FEATURE)
87+ else:
88+ scenario(FEATURE, scenario_name)
89+ assert os.path.abspath(os.path.join('{}', FEATURE)) in str(exc.value)
90+
5691
57- def test_ok(scenario_name):
92+ @pytest.mark.parametrize(
93+ 'multiple', [True, False]
94+ )
95+ def test_ok_by_ini(scenario_name, multiple):
5896 # Shouldn't raise any exception
59- scenario('steps.feature', scenario_name)
60- """ .format (base_dir ))
97+ if multiple:
98+ scenarios(FEATURE)
99+ else:
100+ scenario(FEATURE, scenario_name)
101+
102+
103+ @pytest.mark.parametrize(
104+ 'multiple', [True, False]
105+ )
106+ @pytest.mark.parametrize(
107+ 'param_base_dir', [
108+ '.',
109+ '/does/not/exist/',
110+ ]
111+ )
112+ def test_not_found_by_param(scenario_name, param_base_dir, multiple):
113+ with pytest.raises(IOError) as exc:
114+ if multiple:
115+ scenarios(FEATURE, feature_base_dir=param_base_dir)
116+ else:
117+ scenario(FEATURE, scenario_name, feature_base_dir=param_base_dir)
118+ assert os.path.abspath(os.path.join(param_base_dir, FEATURE)) in str(exc.value)
119+
120+
121+ @pytest.mark.parametrize(
122+ 'multiple', [True, False]
123+ )
124+ def test_ok_by_param(scenario_name, multiple):
125+ # Shouldn't raise any exception no matter of bdd_feature_base_dir in ini
126+ if multiple:
127+ scenarios(FEATURE, feature_base_dir='features')
128+ else:
129+ scenario(FEATURE, scenario_name, feature_base_dir='features')
130+
131+ """ .format (ini_base_dir ))
0 commit comments