-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_fixture_teardown.py
50 lines (32 loc) · 1.12 KB
/
test_fixture_teardown.py
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
"""
pytest provides two ways for fixtures to do teardown.
One is with a finalizer function:
http://pytest.org/latest/fixture.html#fixture-finalization-executing-teardown-code,
and the other is with yield fixtures:
http://pytest.org/latest/yieldfixture.html#yieldfixture
A lot of fixtures provide something to tests so they are injected,
but that's not always necessary:
http://pytest.org/latest/fixture.html#using-fixtures-from-classes-modules-or-projects
Command:
py.test -v -s test_fixture_teardown.py
"""
import pytest
@pytest.fixture
def finalized(request):
print('\nfixture setup for', request.fixturename)
def teardown():
print('\nfixture teardown for', request.fixturename)
request.addfinalizer(teardown)
return 'finalized'
@pytest.yield_fixture
def yielder(request):
print('\nfixture setup for', request.fixturename)
yield 'yielder'
print('\nfixture teardown for', request.fixturename)
def test_finalized(finalized):
assert finalized == 'finalized'
def test_yielder(yielder):
assert yielder == 'yielder'
@pytest.mark.usefixtures('yielder')
def test_yielder_too():
pass