File: test_background.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 (118 lines) | stat: -rw-r--r-- 2,415 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
117
118
"""Test feature background."""

import textwrap

FEATURE = """\
Feature: Background support

    Background:
        Given foo has a value "bar"
        And a background step with multiple lines:
            one
            two


    Scenario: Basic usage
        Then foo should have value "bar"

    Scenario: Background steps are executed first
        Given foo has no value "bar"
        And foo has a value "dummy"

        Then foo should have value "dummy"
        And foo should not have value "bar"
"""

STEPS = r"""\
import re
import pytest
from pytest_bdd import given, then, parsers

@pytest.fixture
def foo():
    return {}


@given(parsers.re(r"a background step with multiple lines:\n(?P<data>.+)", flags=re.DOTALL))
def _(foo, data):
    assert data == "one\ntwo"


@given('foo has a value "bar"')
def _(foo):
    foo["bar"] = "bar"
    return foo["bar"]


@given('foo has a value "dummy"')
def _(foo):
    foo["dummy"] = "dummy"
    return foo["dummy"]


@given('foo has no value "bar"')
def _(foo):
    assert foo["bar"]
    del foo["bar"]


@then('foo should have value "bar"')
def _(foo):
    assert foo["bar"] == "bar"


@then('foo should have value "dummy"')
def _(foo):
    assert foo["dummy"] == "dummy"


@then('foo should not have value "bar"')
def _(foo):
    assert "bar" not in foo

"""


def test_background_basic(pytester):
    """Test feature background."""
    pytester.makefile(".feature", background=textwrap.dedent(FEATURE))

    pytester.makeconftest(textwrap.dedent(STEPS))

    pytester.makepyfile(
        textwrap.dedent(
            """\
        from pytest_bdd import scenario

        @scenario("background.feature", "Basic usage")
        def test_background():
            pass

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


def test_background_check_order(pytester):
    """Test feature background to ensure that background steps are executed first."""

    pytester.makefile(".feature", background=textwrap.dedent(FEATURE))

    pytester.makeconftest(textwrap.dedent(STEPS))

    pytester.makepyfile(
        textwrap.dedent(
            """\
        from pytest_bdd import scenario

        @scenario("background.feature", "Background steps are executed first")
        def test_background():
            pass

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