(pytester: Pytester)
| 240 | |
| 241 | @pytest.mark.filterwarnings("always::UserWarning") |
| 242 | def test_warning_captured_hook(pytester: Pytester) -> None: |
| 243 | pytester.makeconftest( |
| 244 | """ |
| 245 | def pytest_configure(config): |
| 246 | config.issue_config_time_warning(UserWarning("config warning"), stacklevel=2) |
| 247 | """ |
| 248 | ) |
| 249 | pytester.makepyfile( |
| 250 | """ |
| 251 | import pytest, warnings |
| 252 | |
| 253 | warnings.warn(UserWarning("collect warning")) |
| 254 | |
| 255 | @pytest.fixture |
| 256 | def fix(): |
| 257 | warnings.warn(UserWarning("setup warning")) |
| 258 | yield 1 |
| 259 | warnings.warn(UserWarning("teardown warning")) |
| 260 | |
| 261 | def test_func(fix): |
| 262 | warnings.warn(UserWarning("call warning")) |
| 263 | assert fix == 1 |
| 264 | """ |
| 265 | ) |
| 266 | |
| 267 | collected = [] |
| 268 | |
| 269 | class WarningCollector: |
| 270 | def pytest_warning_recorded(self, warning_message, when, nodeid, location): |
| 271 | collected.append((str(warning_message.message), when, nodeid, location)) |
| 272 | |
| 273 | result = pytester.runpytest(plugins=[WarningCollector()]) |
| 274 | result.stdout.fnmatch_lines(["*1 passed*"]) |
| 275 | |
| 276 | expected = [ |
| 277 | ("config warning", "config", ""), |
| 278 | ("collect warning", "collect", ""), |
| 279 | ("setup warning", "runtest", "test_warning_captured_hook.py::test_func"), |
| 280 | ("call warning", "runtest", "test_warning_captured_hook.py::test_func"), |
| 281 | ("teardown warning", "runtest", "test_warning_captured_hook.py::test_func"), |
| 282 | ] |
| 283 | for index in range(len(expected)): |
| 284 | collected_result = collected[index] |
| 285 | expected_result = expected[index] |
| 286 | |
| 287 | assert collected_result[0] == expected_result[0], str(collected) |
| 288 | assert collected_result[1] == expected_result[1], str(collected) |
| 289 | assert collected_result[2] == expected_result[2], str(collected) |
| 290 | |
| 291 | # NOTE: collected_result[3] is location, which differs based on the platform you are on |
| 292 | # thus, the best we can do here is assert the types of the parameters match what we expect |
| 293 | # and not try and preload it in the expected array |
| 294 | if collected_result[3] is not None: |
| 295 | assert type(collected_result[3][0]) is str, str(collected) |
| 296 | assert type(collected_result[3][1]) is int, str(collected) |
| 297 | assert type(collected_result[3][2]) is str, str(collected) |
| 298 | else: |
| 299 | assert collected_result[3] is None, str(collected) |
nothing calls this directly
no test coverage detected