|
1 | 1 | import os |
2 | 2 | import re |
| 3 | +import sys |
3 | 4 | import textwrap |
4 | 5 |
|
5 | 6 | import py |
@@ -358,6 +359,30 @@ def test_hello(): |
358 | 359 |
|
359 | 360 |
|
360 | 361 | class TestTerminalReporting: |
| 362 | + @pytest.mark.parametrize("verbosity", ["", "-q", "-v"]) |
| 363 | + def test_output_verbosity(self, testdir, verbosity): |
| 364 | + testdir.makepyfile( |
| 365 | + """ |
| 366 | + def test_ok(): |
| 367 | + pass |
| 368 | + """ |
| 369 | + ) |
| 370 | + args = ["-n1"] |
| 371 | + if verbosity: |
| 372 | + args.append(verbosity) |
| 373 | + result = testdir.runpytest(*args) |
| 374 | + out = result.stdout.str() |
| 375 | + if verbosity == "-v": |
| 376 | + assert "scheduling tests" in out |
| 377 | + assert "gw" in out |
| 378 | + elif verbosity == "-q": |
| 379 | + assert "scheduling tests" not in out |
| 380 | + assert "gw" not in out |
| 381 | + assert "bringing up nodes..." in out |
| 382 | + else: |
| 383 | + assert "scheduling tests" not in out |
| 384 | + assert "gw" in out |
| 385 | + |
361 | 386 | def test_pass_skip_fail(self, testdir): |
362 | 387 | testdir.makepyfile( |
363 | 388 | """ |
@@ -465,9 +490,8 @@ def pytest_sessionfinish(session): |
465 | 490 | name = "worker" |
466 | 491 | else: |
467 | 492 | name = "master" |
468 | | - f = open(name, "w") |
469 | | - f.write("xy") |
470 | | - f.close() |
| 493 | + with open(name, "w") as f: |
| 494 | + f.write("xy") |
471 | 495 | # let's fail on the worker |
472 | 496 | if name == "worker": |
473 | 497 | raise ValueError(42) |
@@ -517,16 +541,15 @@ def pytest_sessionfinish(session): |
517 | 541 | assert collected_file.read() == "collected = 3" |
518 | 542 |
|
519 | 543 |
|
520 | | -def test_funcarg_teardown_failure(testdir): |
| 544 | +def test_fixture_teardown_failure(testdir): |
521 | 545 | p = testdir.makepyfile( |
522 | 546 | """ |
523 | 547 | import pytest |
524 | | - @pytest.fixture |
| 548 | + @pytest.fixture(scope="module") |
525 | 549 | def myarg(request): |
526 | | - def teardown(val): |
527 | | - raise ValueError(val) |
528 | | - return request.cached_setup(setup=lambda: 42, teardown=teardown, |
529 | | - scope="module") |
| 550 | + yield 42 |
| 551 | + raise ValueError(42) |
| 552 | +
|
530 | 553 | def test_hello(myarg): |
531 | 554 | pass |
532 | 555 | """ |
@@ -612,6 +635,11 @@ def test_crash(): |
612 | 635 |
|
613 | 636 |
|
614 | 637 | def test_issue34_pluginloading_in_subprocess(testdir): |
| 638 | + import _pytest.hookspec |
| 639 | + |
| 640 | + if not hasattr(_pytest.hookspec, "pytest_namespace"): |
| 641 | + pytest.skip("this pytest version no longer supports pytest_namespace()") |
| 642 | + |
615 | 643 | testdir.tmpdir.join("plugin123.py").write( |
616 | 644 | textwrap.dedent( |
617 | 645 | """ |
@@ -710,10 +738,12 @@ def test_ok(): |
710 | 738 | class TestWarnings: |
711 | 739 | @pytest.mark.parametrize("n", ["-n0", "-n1"]) |
712 | 740 | @pytest.mark.parametrize("warn_type", ["pytest", "builtin"]) |
713 | | - def test_warnings(self, testdir, n, warn_type): |
| 741 | + def test_warnings(self, testdir, n, request, warn_type): |
714 | 742 | if warn_type == "builtin": |
715 | 743 | warn_code = """warnings.warn(UserWarning('this is a warning'))""" |
716 | 744 | elif warn_type == "pytest": |
| 745 | + if not hasattr(request.config, "warn"): |
| 746 | + pytest.skip("config.warn has been removed in pytest 4.1") |
717 | 747 | warn_code = """request.config.warn('', 'this is a warning', |
718 | 748 | fslocation=py.path.local())""" |
719 | 749 | else: |
@@ -773,6 +803,39 @@ def test_func(tmpdir): |
773 | 803 | result = testdir.runpytest(n) |
774 | 804 | result.stdout.fnmatch_lines(["*UserWarning*foo.txt*", "*1 passed, 1 warnings*"]) |
775 | 805 |
|
| 806 | + @pytest.mark.parametrize("n", ["-n0", "-n1"]) |
| 807 | + def test_unserializable_warning_details(self, testdir, n): |
| 808 | + """Check that warnings with unserializable _WARNING_DETAILS are |
| 809 | + handled correctly (#379). |
| 810 | + """ |
| 811 | + if sys.version_info[0] < 3: |
| 812 | + # The issue is only present in Python 3 warnings |
| 813 | + return |
| 814 | + testdir.makepyfile( |
| 815 | + """ |
| 816 | + import warnings, pytest |
| 817 | + import socket |
| 818 | + import gc |
| 819 | + def abuse_socket(): |
| 820 | + s = socket.socket() |
| 821 | + del s |
| 822 | +
|
| 823 | + # Deliberately provoke a ResourceWarning for an unclosed socket. |
| 824 | + # The socket itself will end up attached as a value in |
| 825 | + # _WARNING_DETAIL. We need to test that it is not serialized |
| 826 | + # (it can't be, so the test will fail if we try to). |
| 827 | + @pytest.mark.filterwarnings('always') |
| 828 | + def test_func(tmpdir): |
| 829 | + abuse_socket() |
| 830 | + gc.collect() |
| 831 | + """ |
| 832 | + ) |
| 833 | + testdir.syspathinsert() |
| 834 | + result = testdir.runpytest(n) |
| 835 | + result.stdout.fnmatch_lines( |
| 836 | + ["*ResourceWarning*unclosed*", "*1 passed, 1 warnings*"] |
| 837 | + ) |
| 838 | + |
776 | 839 |
|
777 | 840 | class TestNodeFailure: |
778 | 841 | def test_load_single(self, testdir): |
|
0 commit comments