File: test_trio_mode.py

package info (click to toggle)
python-pytest-trio 0.8.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 464 kB
  • sloc: python: 944; sh: 49; makefile: 20
file content (177 lines) | stat: -rw-r--r-- 4,046 bytes parent folder | download | duplicates (3)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import pytest

from .helpers import enable_trio_mode

test_text = """
import pytest
import trio
from hypothesis import given, settings, strategies

async def test_pass():
    await trio.sleep(0)

async def test_fail():
    await trio.sleep(0)
    assert False

@settings(deadline=None, max_examples=5)
@given(strategies.binary())
async def test_hypothesis_pass(b):
    await trio.sleep(0)
    assert isinstance(b, bytes)

@settings(deadline=None, max_examples=5)
@given(strategies.binary())
async def test_hypothesis_fail(b):
    await trio.sleep(0)
    assert isinstance(b, int)
"""


@enable_trio_mode
def test_trio_mode(testdir, enable_trio_mode):
    enable_trio_mode(testdir)

    testdir.makepyfile(test_text)

    result = testdir.runpytest()
    result.assert_outcomes(passed=2, failed=2)


# This is faking qtrio due to real qtrio's dependence on either
# PyQt5 or PySide2.  They are both large and require special
# handling in CI.  The testing here is able to focus on the
# pytest-trio features with just this minimal substitute.
qtrio_text = """
import trio

fake_used = False

def run(*args, **kwargs):
    global fake_used
    fake_used = True

    return trio.run(*args, **kwargs)
"""


def test_trio_mode_and_qtrio_run_configuration(testdir):
    testdir.makefile(".ini", pytest="[pytest]\ntrio_mode = true\ntrio_run = qtrio\n")

    testdir.makepyfile(qtrio=qtrio_text)

    test_text = """
    import qtrio
    import trio

    async def test_fake_qtrio_used():
        await trio.sleep(0)
        assert qtrio.fake_used
    """
    testdir.makepyfile(test_text)

    result = testdir.runpytest()
    result.assert_outcomes(passed=1)


def test_trio_mode_and_qtrio_marker(testdir):
    testdir.makefile(".ini", pytest="[pytest]\ntrio_mode = true\n")

    testdir.makepyfile(qtrio=qtrio_text)

    test_text = """
    import pytest
    import qtrio
    import trio

    @pytest.mark.trio(run=qtrio.run)
    async def test_fake_qtrio_used():
        await trio.sleep(0)
        assert qtrio.fake_used
    """
    testdir.makepyfile(test_text)

    result = testdir.runpytest()
    result.assert_outcomes(passed=1)


def test_qtrio_just_run_configuration(testdir):
    testdir.makefile(".ini", pytest="[pytest]\ntrio_run = qtrio\n")

    testdir.makepyfile(qtrio=qtrio_text)

    test_text = """
    import pytest
    import qtrio
    import trio

    @pytest.mark.trio
    async def test_fake_qtrio_used():
        await trio.sleep(0)
        assert qtrio.fake_used
    """
    testdir.makepyfile(test_text)

    result = testdir.runpytest()
    result.assert_outcomes(passed=1)


def test_invalid_trio_run_fails(testdir):
    run_name = "invalid_trio_run"

    testdir.makefile(
        ".ini", pytest=f"[pytest]\ntrio_mode = true\ntrio_run = {run_name}\n"
    )

    test_text = """
    async def test():
        pass
    """
    testdir.makepyfile(test_text)

    result = testdir.runpytest()
    result.assert_outcomes()
    result.stdout.fnmatch_lines(
        [
            f"*ValueError: {run_name!r} not valid for 'trio_run' config.  Must be one of: *"
        ]
    )


def test_closest_explicit_run_wins(testdir):
    testdir.makefile(".ini", pytest=f"[pytest]\ntrio_mode = true\ntrio_run = trio\n")
    testdir.makepyfile(qtrio=qtrio_text)

    test_text = """
    import pytest
    import pytest_trio
    import qtrio

    @pytest.mark.trio(run='should be ignored')
    @pytest.mark.trio(run=qtrio.run)
    async def test():
        assert qtrio.fake_used
    """
    testdir.makepyfile(test_text)

    result = testdir.runpytest()
    result.assert_outcomes(passed=1)


def test_ini_run_wins_with_blank_marker(testdir):
    testdir.makefile(".ini", pytest=f"[pytest]\ntrio_mode = true\ntrio_run = qtrio\n")
    testdir.makepyfile(qtrio=qtrio_text)

    test_text = """
    import pytest
    import pytest_trio
    import qtrio

    @pytest.mark.trio
    async def test():
        assert qtrio.fake_used
    """
    testdir.makepyfile(test_text)

    result = testdir.runpytest()
    result.assert_outcomes(passed=1)