Merge pull request #739 from pytest-dev/test-python-main-steps-found

Test using pytest main to invoke test finds step defs.
This commit is contained in:
Alessio Bogon 2024-11-13 23:01:04 +01:00 committed by GitHub
commit a01ca25e5a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 65 additions and 0 deletions

View File

@ -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 <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> 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)