-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_task_2.py
95 lines (67 loc) · 2.94 KB
/
test_task_2.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
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
"""Task for learning pytest fixtures and marks."""
import models
# Mutable Global state is evil, don't try this at home.
UGLY_STATE = {"setup": False, "teardown": False}
def planets():
"""A fixture which returns a list of planets once per session."""
# Hint: use load_data from models to, well, load the data.
def jupiter():
"""A fixture which returns a new instance of jupiter for each use."""
# Hint: use the load data function in models, find jupiter, return
# a copy. Ensure the scope parameter is set right in the fixture
# decorator. You can use `deepcopy` from the copy module or the copy
# method on the Satellite instance.
def satellite():
"""A parametrized fixture which returns each moon for all planets."""
# Hint: https://docs.pytest.org/en/6.2.x/fixture.html#parametrizing-fixtures
def setup_teardown_fixture():
"""
A function scoped fixture which does setup and teardown.
For setup set UGLY_STATE['setup'] = True and for teardown set
UGLY_STATE['setup'] = True. Yield 42 as the value for this fixture.
"""
class TestPlanetsFixture:
"""Tests for the planets fixture."""
def test_planet_length_and_type(self, planets):
"""Ensure a sequence of 9 is returned."""
assert len(planets) == 9
for planet in planets:
assert isinstance(planet, models.Planet)
class TestJupiterFixture:
"""Tests for the jupiter fixture."""
def test_modify(self, jupiter):
"""
Tests that a new jupiter is returned each time.
If this is not the case, downstream tests will fail.
"""
jupiter.name = "not_jupiter_any_more"
def test_is_jupiter(self, jupiter):
"""Ensure the fixture returns jupiter."""
assert jupiter.name.lower() == "jupiter"
class TestSatelliteFixture:
"""Tests for the moon fixture, which should be parametrized."""
found_satellites = []
def test_single_moon(self, satellite):
"""Ensure a single moon was returned."""
assert isinstance(satellite, models.Satellite)
self.found_satellites.append(satellite)
def teardown_class(self):
"""Ensure all satellites were found."""
planets = models.load_data()
moon_count = sum([len(x.satellites) for x in planets])
assert len(self.found_satellites) == moon_count
class TestSetupTearDownFixture:
"""Tests to ensure the setting up and tearing down worked."""
def setup_class(self):
"""Ensure fixture setup hasn't yet run."""
assert not UGLY_STATE["setup"]
assert not UGLY_STATE["teardown"]
def test_fixture_values(self, setup_teardown_fixture):
"""Ensure requested value is returned."""
assert UGLY_STATE["setup"]
assert setup_teardown_fixture == 42
assert not UGLY_STATE["teardown"]
def teardown_class(self):
"""Ensure teardown flag was switched."""
assert UGLY_STATE["setup"]
assert UGLY_STATE["teardown"]