Skip to content

Commit ac51558

Browse files
committed
Add example of function scope pytest fixtures
1 parent de40ca5 commit ac51558

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

source-code/testing/PyTest/Fixtures/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ A simple illustration of using fixtures in pytest.
44
## What is it?
55
1. `test_data_structure.py`: creates a test and a module fixture, and
66
defines three tests, one of which is intended to fail.
7+
1. `test_list.py`: creates a list and tests whether `pop` and `append` work,
8+
illustrates the advantage of `function` scope fixtures.
79
1. `test_wc.py`: creates a module fixture that will be torn down when
810
the test is done.
911

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def non_empty_list():
6+
return list(range(5))
7+
8+
9+
def test_pop(non_empty_list):
10+
orig_len = len(non_empty_list)
11+
assert non_empty_list[-1] == orig_len - 1
12+
non_empty_list.pop()
13+
assert len(non_empty_list) == orig_len - 1
14+
assert non_empty_list[-1] == orig_len - 2
15+
16+
17+
def test_append(non_empty_list):
18+
orig_len = len(non_empty_list)
19+
assert non_empty_list[-1] == orig_len - 1
20+
non_empty_list.append(orig_len)
21+
assert len(non_empty_list) == orig_len + 1
22+
assert non_empty_list[-1] == orig_len

0 commit comments

Comments
 (0)