File: test_common.py

package info (click to toggle)
pytest-bdd 7.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 540 kB
  • sloc: python: 3,535; makefile: 134
file content (116 lines) | stat: -rw-r--r-- 3,193 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import textwrap

from pytest_bdd.utils import collect_dumped_objects


def test_reuse_same_step_different_converters(pytester):
    pytester.makefile(
        ".feature",
        arguments=textwrap.dedent(
            """\
            Feature: Reuse same step with different converters
                Scenario: Step function should be able to be decorated multiple times with different converters
                    Given I have a foo with int value 42
                    And I have a foo with str value 42
                    And I have a foo with float value 42
                    When pass
                    Then pass
            """
        ),
    )

    pytester.makepyfile(
        textwrap.dedent(
            r"""
        import pytest
        from pytest_bdd import parsers, given, when, then, scenarios
        from pytest_bdd.utils import dump_obj

        scenarios("arguments.feature")

        @given(parsers.re(r"^I have a foo with int value (?P<value>.*?)$"), converters={"value": int})
        @given(parsers.re(r"^I have a foo with str value (?P<value>.*?)$"), converters={"value": str})
        @given(parsers.re(r"^I have a foo with float value (?P<value>.*?)$"), converters={"value": float})
        def _(value):
            dump_obj(value)
            return value


        @then("pass")
        @when("pass")
        def _():
            pass
        """
        )
    )
    result = pytester.runpytest("-s")
    result.assert_outcomes(passed=1)

    [int_value, str_value, float_value] = collect_dumped_objects(result)
    assert type(int_value) is int
    assert int_value == 42

    assert type(str_value) is str
    assert str_value == "42"

    assert type(float_value) is float
    assert float_value == 42.0


def test_string_steps_dont_take_precedence(pytester):
    """Test that normal steps don't take precedence over the other steps."""
    pytester.makefile(
        ".feature",
        arguments=textwrap.dedent(
            """\
            Feature: Step precedence
                Scenario: String steps don't take precedence over other steps
                    Given I have a foo with value 42
                    When pass
                    Then pass
            """
        ),
    )
    pytester.makeconftest(
        textwrap.dedent(
            """
        from pytest_bdd import given, when, then, parsers
        from pytest_bdd.utils import dump_obj


        @given("I have a foo with value 42")
        def _():
            dump_obj("str")
            return 42


        @then("pass")
        @when("pass")
        def _():
            pass
        """
        )
    )

    pytester.makepyfile(
        textwrap.dedent(
            r"""
        import pytest
        from pytest_bdd import parsers, given, when, then, scenarios
        from pytest_bdd.utils import dump_obj

        scenarios("arguments.feature")

        @given(parsers.re(r"^I have a foo with value (?P<value>.*?)$"))
        def _(value):
            dump_obj("re")
            return 42

        """
        )
    )
    result = pytester.runpytest("-s")
    result.assert_outcomes(passed=1)

    [which] = collect_dumped_objects(result)
    assert which == "re"