File: test_args.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 (109 lines) | stat: -rw-r--r-- 2,815 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
"""Step arguments tests."""

import textwrap


def test_every_step_takes_param_with_the_same_name(pytester):
    """Test every step takes param with the same name."""
    pytester.makefile(
        ".feature",
        arguments=textwrap.dedent(
            """\
            Feature: Step arguments
                Scenario: Every step takes a parameter with the same name
                    Given I have 1 Euro
                    When I pay 2 Euro
                    And I pay 1 Euro
                    Then I should have 0 Euro
                    And I should have 999999 Euro # In my dream...

            """
        ),
    )

    pytester.makepyfile(
        textwrap.dedent(
            """\
        import pytest
        from pytest_bdd import parsers, given, when, then, scenario

        @scenario("arguments.feature", "Every step takes a parameter with the same name")
        def test_arguments():
            pass

        @pytest.fixture
        def values():
            return [1, 2, 1, 0, 999999]


        @given(parsers.cfparse("I have {euro:d} Euro"))
        def _(euro, values):
            assert euro == values.pop(0)


        @when(parsers.cfparse("I pay {euro:d} Euro"))
        def _(euro, values, request):
            assert euro == values.pop(0)


        @then(parsers.cfparse("I should have {euro:d} Euro"))
        def _(euro, values):
            assert euro == values.pop(0)

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


def test_argument_in_when(pytester):
    """Test step arguments in when steps."""
    pytester.makefile(
        ".feature",
        arguments=textwrap.dedent(
            """\
            Feature: Step arguments
                Scenario: Argument in when
                    Given I have an argument 1
                    When I get argument 5
                    Then My argument should be 5
            """
        ),
    )

    pytester.makepyfile(
        textwrap.dedent(
            """\
        import pytest
        from pytest_bdd import parsers, given, when, then, scenario

        @scenario("arguments.feature", "Argument in when")
        def test_arguments():
            pass


        @pytest.fixture
        def arguments():
            return dict()


        @given(parsers.cfparse("I have an argument {arg:Number}", extra_types=dict(Number=int)))
        def _(arguments, arg):
            arguments["arg"] = arg


        @when(parsers.cfparse("I get argument {arg:d}"))
        def _(arguments, arg):
            arguments["arg"] = arg


        @then(parsers.cfparse("My argument should be {arg:d}"))
        def _(arguments, arg):
            assert arguments["arg"] == arg

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