2
2
3
3
import os
4
4
import sys
5
+ import textwrap
5
6
6
7
from pytest_bdd .scripts import main
7
8
@@ -16,3 +17,67 @@ def test_main(monkeypatch, capsys):
16
17
out , err = capsys .readouterr ()
17
18
assert "usage: pytest-bdd [-h]" in err
18
19
assert "pytest-bdd: error:" in err
20
+
21
+
22
+ def test_step_definitions_found_using_main (pytester ):
23
+ """Issue 173: Ensure step definitions are found when using pytest.main."""
24
+ pytester .makefile (
25
+ ".feature" ,
26
+ outline = textwrap .dedent (
27
+ """\
28
+ Feature: Outlined Scenarios
29
+
30
+ Scenario Outline: Outlined given, when, then
31
+ Given there are <start> cucumbers
32
+ When I eat <eat> cucumbers
33
+ Then I should have <left> cucumbers
34
+
35
+ Examples:
36
+ | start | eat | left |
37
+ | 12 | 5 | 7 |
38
+ """
39
+ ),
40
+ )
41
+
42
+ pytester .makepyfile (
43
+ textwrap .dedent (
44
+ """\
45
+ from pytest_bdd import given, when, then, parsers, scenarios
46
+
47
+ scenarios(".")
48
+
49
+ @given(parsers.parse("there are {start:d} cucumbers"), target_fixture="cucumbers")
50
+ def _(start):
51
+ assert isinstance(start, int)
52
+ return {"start": start}
53
+
54
+
55
+ @when(parsers.parse("I eat {eat:g} cucumbers"))
56
+ def _(cucumbers, eat):
57
+ assert isinstance(eat, float)
58
+ cucumbers["eat"] = eat
59
+
60
+
61
+ @then(parsers.parse("I should have {left} cucumbers"))
62
+ def _(cucumbers, left):
63
+ assert isinstance(left, str)
64
+ assert cucumbers["start"] - cucumbers["eat"] == int(left)
65
+ """
66
+ )
67
+ )
68
+
69
+ pytester .makepyfile (
70
+ main = textwrap .dedent (
71
+ """\
72
+ import pytest
73
+ import os
74
+
75
+ # Programmatically run pytest
76
+ if __name__ == "__main__":
77
+ pytest.main([os.path.abspath("test_step_definitions_found_using_main.py")])
78
+ """
79
+ )
80
+ )
81
+
82
+ result = pytester .runpython (pytester .path / "main.py" )
83
+ result .assert_outcomes (passed = 1 , failed = 0 )
0 commit comments