|
1 | 1 | """C2 - Test with Yield Fixtures
|
2 | 2 | Yield fixtures use the "yield" statement to return something to the test function,
|
3 | 3 | but they keep running after the test (similarly to context managers).
|
4 |
| -We run 2 identical tests to verify that the data inserted by previous tests is deleted on the fixture. |
5 | 4 | https://pytest.readthedocs.io/en/2.9.1/yieldfixture.html
|
6 |
| -https://docs.pytest.org/en/latest/yieldfixture.html |
7 |
| -""" |
| 5 | +https://docs.pytest.org/en/latest/yieldfixture.html""" |
8 | 6 |
|
9 | 7 | import random
|
10 | 8 | import pytest
|
11 |
| -from pymongo import MongoClient |
12 |
| -from pymongo.collection import Collection |
13 | 9 |
|
14 | 10 |
|
15 | 11 | @pytest.fixture
|
16 |
| -def mongo_collection(): |
17 |
| - """This fixture will create a MongoClient instance and return a collection where we can write and read data. |
18 |
| - The collection is returned with yield statement to the test. |
19 |
| - After test run, all documents from the collection are deleted. |
20 |
| - """ |
21 |
| - client = MongoClient("mongodb://127.0.0.1:27017") |
22 |
| - collection = client["test_database"]["test_collection"] |
| 12 | +def random_number(): |
| 13 | + """This fixture will return a random number""" |
| 14 | + print("A) Fixture starts") |
| 15 | + number = random.randint(0, 1000) |
| 16 | + yield number |
| 17 | + print("D) Fixture ends") |
23 | 18 |
|
24 |
| - # Delete all documents on collections before test run |
25 |
| - # (to clean possible documents from previous tests) |
26 |
| - collection.delete_many({}) |
27 |
| - print("Deleted all documents in Mongo test collection!") |
28 | 19 |
|
29 |
| - print("Returning collection to the test") |
30 |
| - yield collection |
| 20 | +def test_random_number_fixture_yield(random_number): |
| 21 | + print("B) Test starts") |
| 22 | + assert type(random_number) == int |
| 23 | + print("C) Test ends with number", random_number) |
31 | 24 |
|
32 |
| - # Delete all documents on collection after test run |
33 |
| - # (to keep it clean for next tests) |
34 |
| - collection.delete_many({}) |
35 | 25 |
|
36 |
| - |
37 |
| -@pytest.fixture |
38 |
| -def sample_data(): |
39 |
| - return {"random_number": random.randint(0, 100000)} |
40 |
| - |
41 |
| - |
42 |
| -def test_insert_read_delete_1(mongo_collection: Collection, sample_data): |
43 |
| - read = mongo_collection.find({}) |
44 |
| - assert list(read) == [] |
45 |
| - |
46 |
| - result = mongo_collection.insert_one(sample_data) |
47 |
| - |
48 |
| - read = mongo_collection.find_one({"_id": result.inserted_id}) |
49 |
| - assert read["random_number"] == sample_data["random_number"] |
50 |
| - |
51 |
| - |
52 |
| -def test_insert_read_delete_2(mongo_collection: Collection, sample_data): |
53 |
| - read = mongo_collection.find({}) |
54 |
| - assert list(read) == [] |
55 |
| - |
56 |
| - result = mongo_collection.insert_one(sample_data) |
57 |
| - |
58 |
| - read = mongo_collection.find_one({"_id": result.inserted_id}) |
59 |
| - assert read["random_number"] == sample_data["random_number"] |
| 26 | +def test_random_number_fixture_fails(random_number): |
| 27 | + """Even if a test fails, the fixture runs the code after the yield statement""" |
| 28 | + print("B) Test starts") |
| 29 | + assert type(random_number) == str |
| 30 | + print("C) Test ends (it should not reach this point!)") |
0 commit comments