2014-09-21 07:54:39 +08:00
|
|
|
"""Code generation and assertion tests."""
|
2015-06-24 07:16:10 +08:00
|
|
|
import itertools
|
2018-07-06 05:45:27 +08:00
|
|
|
import textwrap
|
2014-09-21 07:54:39 +08:00
|
|
|
|
2015-06-24 07:16:10 +08:00
|
|
|
from pytest_bdd.scenario import get_python_name_generator
|
2020-09-06 01:05:57 +08:00
|
|
|
from tests.utils import assert_outcomes
|
2015-06-24 07:16:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_python_name_generator():
|
|
|
|
"""Test python name generator function."""
|
2020-09-08 15:25:39 +08:00
|
|
|
assert list(itertools.islice(get_python_name_generator("Some name"), 3)) == [
|
|
|
|
"test_some_name",
|
|
|
|
"test_some_name_1",
|
|
|
|
"test_some_name_2",
|
|
|
|
]
|
2015-06-24 07:16:10 +08:00
|
|
|
|
2014-09-21 07:54:39 +08:00
|
|
|
|
|
|
|
def test_generate_missing(testdir):
|
2015-06-24 07:16:10 +08:00
|
|
|
"""Test generate missing command."""
|
2020-06-14 18:16:09 +08:00
|
|
|
testdir.makefile(
|
|
|
|
".feature",
|
|
|
|
generation=textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
Feature: Missing code generation
|
2014-09-21 07:54:39 +08:00
|
|
|
|
2020-06-14 18:16:09 +08:00
|
|
|
Background:
|
|
|
|
Given I have a foobar
|
2021-04-09 21:15:10 +08:00
|
|
|
And I have 3 baz
|
2020-06-14 18:16:09 +08:00
|
|
|
|
|
|
|
Scenario: Scenario tests which are already bound to the tests stay as is
|
|
|
|
Given I have a bar
|
|
|
|
|
|
|
|
|
|
|
|
Scenario: Code is generated for scenarios which are not bound to any tests
|
|
|
|
Given I have a bar
|
|
|
|
|
|
|
|
|
|
|
|
Scenario: Code is generated for scenario steps which are not yet defined(implemented)
|
|
|
|
Given I have a custom bar
|
2019-08-24 02:08:09 +08:00
|
|
|
"""
|
2020-06-14 18:16:09 +08:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
testdir.makepyfile(
|
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
2014-09-21 07:54:39 +08:00
|
|
|
import functools
|
|
|
|
|
2021-04-09 21:15:10 +08:00
|
|
|
from pytest_bdd import scenario, given, parsers
|
2014-09-21 07:54:39 +08:00
|
|
|
|
2014-11-09 19:56:50 +08:00
|
|
|
scenario = functools.partial(scenario, "generation.feature")
|
2014-09-21 07:54:39 +08:00
|
|
|
|
2014-11-09 19:56:50 +08:00
|
|
|
@given("I have a bar")
|
2014-09-21 07:54:39 +08:00
|
|
|
def i_have_a_bar():
|
2014-11-09 19:56:50 +08:00
|
|
|
return "bar"
|
2014-09-21 07:54:39 +08:00
|
|
|
|
2021-04-09 21:15:10 +08:00
|
|
|
@given(parsers.parse("I have {n:d} baz"))
|
|
|
|
def i_have_n_baz(n):
|
|
|
|
return "baz"
|
|
|
|
|
2014-11-09 19:56:50 +08:00
|
|
|
@scenario("Scenario tests which are already bound to the tests stay as is")
|
2014-09-21 07:54:39 +08:00
|
|
|
def test_foo():
|
|
|
|
pass
|
|
|
|
|
2014-11-09 19:56:50 +08:00
|
|
|
@scenario("Code is generated for scenario steps which are not yet defined(implemented)")
|
2014-09-21 07:54:39 +08:00
|
|
|
def test_missing_steps():
|
|
|
|
pass
|
2020-06-14 18:16:09 +08:00
|
|
|
"""
|
2019-08-24 02:08:09 +08:00
|
|
|
)
|
|
|
|
)
|
2014-09-21 07:54:39 +08:00
|
|
|
|
2020-06-14 18:16:09 +08:00
|
|
|
result = testdir.runpytest("--generate-missing", "--feature", "generation.feature")
|
2020-09-06 01:05:57 +08:00
|
|
|
assert_outcomes(result, passed=0, failed=0, errors=0)
|
2020-06-14 18:16:09 +08:00
|
|
|
assert not result.stderr.str()
|
|
|
|
assert result.ret == 0
|
2014-09-21 07:54:39 +08:00
|
|
|
|
2019-08-24 02:08:09 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
['Scenario "Code is generated for scenarios which are not bound to any tests" is not bound to any test *']
|
2014-11-10 16:58:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2015-06-10 06:53:12 +08:00
|
|
|
'Step Given "I have a custom bar" is not defined in the scenario '
|
2019-08-24 02:08:09 +08:00
|
|
|
'"Code is generated for scenario steps which are not yet defined(implemented)" *'
|
2014-11-10 16:58:46 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2021-04-09 21:15:10 +08:00
|
|
|
assert 'Step Given "I have 3 baz" is not defined' not in result.stdout.str()
|
2021-04-27 16:43:14 +08:00
|
|
|
|
2014-11-10 16:58:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2015-06-10 06:53:12 +08:00
|
|
|
['Step Given "I have a foobar" is not defined in the background of the feature "Missing code generation" *']
|
2014-11-10 16:58:46 +08:00
|
|
|
)
|
2014-09-22 14:27:46 +08:00
|
|
|
|
2014-11-09 19:56:50 +08:00
|
|
|
result.stdout.fnmatch_lines(["Please place the code above to the test file(s):"])
|