Skip to content

Commit

Permalink
Merge pull request #739 from pytest-dev/test-python-main-steps-found
Browse files Browse the repository at this point in the history
Test using pytest main to invoke test finds step defs.
  • Loading branch information
youtux authored Nov 13, 2024
2 parents 4d995b2 + 3f567bf commit a01ca25
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions tests/scripts/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import sys
import textwrap

from pytest_bdd.scripts import main

Expand All @@ -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)

0 comments on commit a01ca25

Please sign in to comment.