| 1 |
# mypy: allow-untyped-defs |
| 2 |
"""Test correct setup/teardowns at module, class, and instance level.""" |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
from _pytest.pytester import Pytester |
| 7 |
import pytest |
| 8 |
|
| 9 |
|
| 10 |
def test_module_and_function_setup(pytester: Pytester) -> None: |
| 11 |
reprec = pytester.inline_runsource( |
| 12 |
""" |
| 13 |
modlevel = [] |
| 14 |
def setup_module(module): |
| 15 |
assert not modlevel |
| 16 |
module.modlevel.append(42) |
| 17 |
|
| 18 |
def teardown_module(module): |
| 19 |
modlevel.pop() |
| 20 |
|
| 21 |
def setup_function(function): |
| 22 |
function.answer = 17 |
| 23 |
|
| 24 |
def teardown_function(function): |
| 25 |
del function.answer |
| 26 |
|
| 27 |
def test_modlevel(): |
| 28 |
assert modlevel[0] == 42 |
| 29 |
assert test_modlevel.answer == 17 |
| 30 |
|
| 31 |
class TestFromClass(object): |
| 32 |
def test_module(self): |
| 33 |
assert modlevel[0] == 42 |
| 34 |
assert not hasattr(test_modlevel, 'answer') |
| 35 |
""" |
| 36 |
) |
| 37 |
rep = reprec.matchreport("test_modlevel") |
| 38 |
assert rep.passed |
| 39 |
rep = reprec.matchreport("test_module") |
| 40 |
assert rep.passed |
| 41 |
|
| 42 |
|
| 43 |
def test_module_setup_failure_no_teardown(pytester: Pytester) -> None: |
| 44 |
reprec = pytester.inline_runsource( |
| 45 |
""" |
| 46 |
values = [] |
| 47 |
def setup_module(module): |
| 48 |
values.append(1) |
| 49 |
0/0 |
| 50 |
|
| 51 |
def test_nothing(): |
| 52 |
pass |
| 53 |
|
| 54 |
def teardown_module(module): |
| 55 |
values.append(2) |
| 56 |
""" |
| 57 |
) |
| 58 |
reprec.assertoutcome(failed=1) |
| 59 |
calls = reprec.getcalls("pytest_runtest_setup") |
| 60 |
assert calls[0].item.module.values == [1] |
| 61 |
|
| 62 |
|
| 63 |
def test_setup_function_failure_no_teardown(pytester: Pytester) -> None: |
| 64 |
reprec = pytester.inline_runsource( |
| 65 |
""" |
| 66 |
modlevel = [] |
| 67 |
def setup_function(function): |
| 68 |
modlevel.append(1) |
| 69 |
0/0 |
| 70 |
|
| 71 |
def teardown_function(module): |
| 72 |
modlevel.append(2) |
| 73 |
|
| 74 |
def test_func(): |
| 75 |
pass |
| 76 |
""" |
| 77 |
) |
| 78 |
calls = reprec.getcalls("pytest_runtest_setup") |
| 79 |
assert calls[0].item.module.modlevel == [1] |
| 80 |
|
| 81 |
|
| 82 |
def test_class_setup(pytester: Pytester) -> None: |
| 83 |
reprec = pytester.inline_runsource( |
| 84 |
""" |
| 85 |
class TestSimpleClassSetup(object): |
| 86 |
clslevel = [] |
| 87 |
def setup_class(cls): |
| 88 |
cls.clslevel.append(23) |
| 89 |
|
| 90 |
def teardown_class(cls): |
| 91 |
cls.clslevel.pop() |
| 92 |
|
| 93 |
def test_classlevel(self): |
| 94 |
assert self.clslevel[0] == 23 |
| 95 |
|
| 96 |
class TestInheritedClassSetupStillWorks(TestSimpleClassSetup): |
| 97 |
def test_classlevel_anothertime(self): |
| 98 |
assert self.clslevel == [23] |
| 99 |
|
| 100 |
def test_cleanup(): |
| 101 |
assert not TestSimpleClassSetup.clslevel |
| 102 |
assert not TestInheritedClassSetupStillWorks.clslevel |
| 103 |
""" |
| 104 |
) |
| 105 |
reprec.assertoutcome(passed=1 + 2 + 1) |
| 106 |
|
| 107 |
|
| 108 |
def test_class_setup_failure_no_teardown(pytester: Pytester) -> None: |
| 109 |
reprec = pytester.inline_runsource( |
| 110 |
""" |
| 111 |
class TestSimpleClassSetup(object): |
| 112 |
clslevel = [] |
| 113 |
def setup_class(cls): |
| 114 |
0/0 |
| 115 |
|
| 116 |
def teardown_class(cls): |
| 117 |
cls.clslevel.append(1) |
| 118 |
|
| 119 |
def test_classlevel(self): |
| 120 |
pass |
| 121 |
|
| 122 |
def test_cleanup(): |
| 123 |
assert not TestSimpleClassSetup.clslevel |
| 124 |
""" |
| 125 |
) |
| 126 |
reprec.assertoutcome(failed=1, passed=1) |
| 127 |
|
| 128 |
|
| 129 |
def test_method_setup(pytester: Pytester) -> None: |
| 130 |
reprec = pytester.inline_runsource( |
| 131 |
""" |
| 132 |
class TestSetupMethod(object): |
| 133 |
def setup_method(self, meth): |
| 134 |
self.methsetup = meth |
| 135 |
def teardown_method(self, meth): |
| 136 |
del self.methsetup |
| 137 |
|
| 138 |
def test_some(self): |
| 139 |
assert self.methsetup == self.test_some |
| 140 |
|
| 141 |
def test_other(self): |
| 142 |
assert self.methsetup == self.test_other |
| 143 |
""" |
| 144 |
) |
| 145 |
reprec.assertoutcome(passed=2) |
| 146 |
|
| 147 |
|
| 148 |
def test_method_setup_failure_no_teardown(pytester: Pytester) -> None: |
| 149 |
reprec = pytester.inline_runsource( |
| 150 |
""" |
| 151 |
class TestMethodSetup(object): |
| 152 |
clslevel = [] |
| 153 |
def setup_method(self, method): |
| 154 |
self.clslevel.append(1) |
| 155 |
0/0 |
| 156 |
|
| 157 |
def teardown_method(self, method): |
| 158 |
self.clslevel.append(2) |
| 159 |
|
| 160 |
def test_method(self): |
| 161 |
pass |
| 162 |
|
| 163 |
def test_cleanup(): |
| 164 |
assert TestMethodSetup.clslevel == [1] |
| 165 |
""" |
| 166 |
) |
| 167 |
reprec.assertoutcome(failed=1, passed=1) |
| 168 |
|
| 169 |
|
| 170 |
def test_method_setup_uses_fresh_instances(pytester: Pytester) -> None: |
| 171 |
reprec = pytester.inline_runsource( |
| 172 |
""" |
| 173 |
class TestSelfState1(object): |
| 174 |
memory = [] |
| 175 |
def test_hello(self): |
| 176 |
self.memory.append(self) |
| 177 |
|
| 178 |
def test_afterhello(self): |
| 179 |
assert self != self.memory[0] |
| 180 |
""" |
| 181 |
) |
| 182 |
reprec.assertoutcome(passed=2, failed=0) |
| 183 |
|
| 184 |
|
| 185 |
def test_setup_that_skips_calledagain(pytester: Pytester) -> None: |
| 186 |
p = pytester.makepyfile( |
| 187 |
""" |
| 188 |
import pytest |
| 189 |
def setup_module(mod): |
| 190 |
pytest.skip("x") |
| 191 |
def test_function1(): |
| 192 |
pass |
| 193 |
def test_function2(): |
| 194 |
pass |
| 195 |
""" |
| 196 |
) |
| 197 |
reprec = pytester.inline_run(p) |
| 198 |
reprec.assertoutcome(skipped=2) |
| 199 |
|
| 200 |
|
| 201 |
def test_setup_fails_again_on_all_tests(pytester: Pytester) -> None: |
| 202 |
p = pytester.makepyfile( |
| 203 |
""" |
| 204 |
import pytest |
| 205 |
def setup_module(mod): |
| 206 |
raise ValueError(42) |
| 207 |
def test_function1(): |
| 208 |
pass |
| 209 |
def test_function2(): |
| 210 |
pass |
| 211 |
""" |
| 212 |
) |
| 213 |
reprec = pytester.inline_run(p) |
| 214 |
reprec.assertoutcome(failed=2) |
| 215 |
|
| 216 |
|
| 217 |
def test_setup_funcarg_setup_when_outer_scope_fails(pytester: Pytester) -> None: |
| 218 |
p = pytester.makepyfile( |
| 219 |
""" |
| 220 |
import pytest |
| 221 |
def setup_module(mod): |
| 222 |
raise ValueError(42) |
| 223 |
@pytest.fixture |
| 224 |
def hello(request): |
| 225 |
raise ValueError("xyz43") |
| 226 |
def test_function1(hello): |
| 227 |
pass |
| 228 |
def test_function2(hello): |
| 229 |
pass |
| 230 |
""" |
| 231 |
) |
| 232 |
result = pytester.runpytest(p) |
| 233 |
result.stdout.fnmatch_lines( |
| 234 |
[ |
| 235 |
"*function1*", |
| 236 |
"*ValueError*42*", |
| 237 |
"*function2*", |
| 238 |
"*ValueError*42*", |
| 239 |
"*2 errors*", |
| 240 |
] |
| 241 |
) |
| 242 |
result.stdout.no_fnmatch_line("*xyz43*") |
| 243 |
|
| 244 |
|
| 245 |
@pytest.mark.parametrize("arg", ["", "arg"]) |
| 246 |
def test_setup_teardown_function_level_with_optional_argument( |
| 247 |
pytester: Pytester, |
| 248 |
monkeypatch, |
| 249 |
arg: str, |
| 250 |
) -> None: |
| 251 |
"""Parameter to setup/teardown xunit-style functions parameter is now optional (#1728).""" |
| 252 |
import sys |
| 253 |
|
| 254 |
trace_setups_teardowns: list[str] = [] |
| 255 |
monkeypatch.setattr( |
| 256 |
sys, "trace_setups_teardowns", trace_setups_teardowns, raising=False |
| 257 |
) |
| 258 |
p = pytester.makepyfile( |
| 259 |
f""" |
| 260 |
import pytest |
| 261 |
import sys |
| 262 |
|
| 263 |
trace = sys.trace_setups_teardowns.append |
| 264 |
|
| 265 |
def setup_module({arg}): trace('setup_module') |
| 266 |
def teardown_module({arg}): trace('teardown_module') |
| 267 |
|
| 268 |
def setup_function({arg}): trace('setup_function') |
| 269 |
def teardown_function({arg}): trace('teardown_function') |
| 270 |
|
| 271 |
def test_function_1(): pass |
| 272 |
def test_function_2(): pass |
| 273 |
|
| 274 |
class Test(object): |
| 275 |
def setup_method(self, {arg}): trace('setup_method') |
| 276 |
def teardown_method(self, {arg}): trace('teardown_method') |
| 277 |
|
| 278 |
def test_method_1(self): pass |
| 279 |
def test_method_2(self): pass |
| 280 |
""" |
| 281 |
) |
| 282 |
result = pytester.inline_run(p) |
| 283 |
result.assertoutcome(passed=4) |
| 284 |
|
| 285 |
expected = [ |
| 286 |
"setup_module", |
| 287 |
"setup_function", |
| 288 |
"teardown_function", |
| 289 |
"setup_function", |
| 290 |
"teardown_function", |
| 291 |
"setup_method", |
| 292 |
"teardown_method", |
| 293 |
"setup_method", |
| 294 |
"teardown_method", |
| 295 |
"teardown_module", |
| 296 |
] |
| 297 |
assert trace_setups_teardowns == expected |