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
|
import pytest
@pytest.fixture
def sync_fix():
return "sync_fix"
@pytest.mark.trio
async def test_single_sync_fixture(sync_fix):
assert sync_fix == "sync_fix"
def test_single_yield_fixture(testdir):
testdir.makepyfile(
"""
import pytest
events = []
@pytest.fixture
def fix1():
events.append('fixture setup')
yield 'fix1'
events.append('fixture teardown')
def test_before():
assert not events
@pytest.mark.trio
async def test_actual_test(fix1):
assert events == ['fixture setup']
assert fix1 == 'fix1'
def test_after():
assert events == [
'fixture setup',
'fixture teardown',
]
"""
)
result = testdir.runpytest()
result.assert_outcomes(passed=3)
def test_single_yield_fixture_with_async_deps(testdir):
testdir.makepyfile(
"""
import pytest
import trio
events = []
@pytest.fixture
async def fix0():
events.append('fix0 setup')
await trio.sleep(0)
return 'fix0'
@pytest.fixture
def fix1(fix0):
events.append('fix1 setup')
yield 'fix1 - ' + fix0
events.append('fix1 teardown')
def test_before():
assert not events
@pytest.mark.trio
async def test_actual_test(fix1):
assert events == ['fix0 setup', 'fix1 setup']
assert fix1 == 'fix1 - fix0'
def test_after():
assert events == [
'fix0 setup',
'fix1 setup',
'fix1 teardown',
]
"""
)
result = testdir.runpytest()
result.assert_outcomes(passed=3)
def test_sync_yield_fixture_crashed_teardown_allow_other_teardowns(testdir):
testdir.makepyfile(
"""
import pytest
import trio
setup_events = set()
teardown_events = set()
@pytest.fixture
async def force_async_fixture():
pass
@pytest.fixture
def good_fixture(force_async_fixture):
setup_events.add('good_fixture setup')
yield
teardown_events.add('good_fixture teardown')
@pytest.fixture
def bad_fixture(force_async_fixture):
setup_events.add('bad_fixture setup')
yield
teardown_events.add('bad_fixture teardown')
raise RuntimeError('Crash during fixture teardown')
def test_before():
assert not setup_events
assert not teardown_events
@pytest.mark.trio
async def test_actual_test(bad_fixture, good_fixture):
pass
def test_after():
assert setup_events == {
'good_fixture setup',
'bad_fixture setup',
}
assert teardown_events == {
'bad_fixture teardown',
'good_fixture teardown',
}
"""
)
result = testdir.runpytest()
result.assert_outcomes(failed=1, passed=2)
result.stdout.re_match_lines(
[r"(E\W+| +\| )RuntimeError: Crash during fixture teardown"]
)
|