diff --git a/tests/scripts/test_main.py b/tests/scripts/test_main.py index f343396..4cb2265 100644 --- a/tests/scripts/test_main.py +++ b/tests/scripts/test_main.py @@ -2,6 +2,7 @@ import os import sys +import textwrap from pytest_bdd.scripts import main @@ -16,3 +17,67 @@ def test_main(monkeypatch, capsys): out, err = capsys.readouterr() assert "usage: pytest-bdd [-h]" in err assert "pytest-bdd: error:" in err + + +def test_step_definitions_found_using_main(pytester): + """Issue 173: Ensure step definitions are found when using pytest.main.""" + pytester.makefile( + ".feature", + outline=textwrap.dedent( + """\ + Feature: Outlined Scenarios + + Scenario Outline: Outlined given, when, then + Given there are cucumbers + When I eat cucumbers + Then I should have cucumbers + + Examples: + | start | eat | left | + | 12 | 5 | 7 | + """ + ), + ) + + pytester.makepyfile( + textwrap.dedent( + """\ + from pytest_bdd import given, when, then, parsers, scenarios + + scenarios(".") + + @given(parsers.parse("there are {start:d} cucumbers"), target_fixture="cucumbers") + def _(start): + assert isinstance(start, int) + return {"start": start} + + + @when(parsers.parse("I eat {eat:g} cucumbers")) + def _(cucumbers, eat): + assert isinstance(eat, float) + cucumbers["eat"] = eat + + + @then(parsers.parse("I should have {left} cucumbers")) + def _(cucumbers, left): + assert isinstance(left, str) + assert cucumbers["start"] - cucumbers["eat"] == int(left) + """ + ) + ) + + pytester.makepyfile( + main=textwrap.dedent( + """\ + import pytest + import os + + # Programmatically run pytest + if __name__ == "__main__": + pytest.main([os.path.abspath("test_step_definitions_found_using_main.py")]) + """ + ) + ) + + result = pytester.runpython(pytester.path / "main.py") + result.assert_outcomes(passed=1, failed=0)