|
| 1 | +import pytest |
| 2 | + |
| 3 | + |
| 4 | +@pytest.fixture |
| 5 | +def stepwise_testdir(testdir): |
| 6 | + # Rather than having to modify our testfile between tests, we introduce |
| 7 | + # a flag for wether or not the second test should fail. |
| 8 | + testdir.makeconftest( |
| 9 | + """ |
| 10 | +def pytest_addoption(parser): |
| 11 | + group = parser.getgroup('general') |
| 12 | + group.addoption('--fail', action='store_true', dest='fail') |
| 13 | + group.addoption('--fail-last', action='store_true', dest='fail_last') |
| 14 | +""" |
| 15 | + ) |
| 16 | + |
| 17 | + # Create a simple test suite. |
| 18 | + testdir.makepyfile( |
| 19 | + test_a=""" |
| 20 | +def test_success_before_fail(): |
| 21 | + assert 1 |
| 22 | +
|
| 23 | +def test_fail_on_flag(request): |
| 24 | + assert not request.config.getvalue('fail') |
| 25 | +
|
| 26 | +def test_success_after_fail(): |
| 27 | + assert 1 |
| 28 | +
|
| 29 | +def test_fail_last_on_flag(request): |
| 30 | + assert not request.config.getvalue('fail_last') |
| 31 | +
|
| 32 | +def test_success_after_last_fail(): |
| 33 | + assert 1 |
| 34 | +""" |
| 35 | + ) |
| 36 | + |
| 37 | + testdir.makepyfile( |
| 38 | + test_b=""" |
| 39 | +def test_success(): |
| 40 | + assert 1 |
| 41 | +""" |
| 42 | + ) |
| 43 | + |
| 44 | + return testdir |
| 45 | + |
| 46 | + |
| 47 | +@pytest.fixture |
| 48 | +def error_testdir(testdir): |
| 49 | + testdir.makepyfile( |
| 50 | + test_a=""" |
| 51 | +def test_error(nonexisting_fixture): |
| 52 | + assert 1 |
| 53 | +
|
| 54 | +def test_success_after_fail(): |
| 55 | + assert 1 |
| 56 | +""" |
| 57 | + ) |
| 58 | + |
| 59 | + return testdir |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture |
| 63 | +def broken_testdir(testdir): |
| 64 | + testdir.makepyfile( |
| 65 | + working_testfile="def test_proper(): assert 1", broken_testfile="foobar" |
| 66 | + ) |
| 67 | + return testdir |
| 68 | + |
| 69 | + |
| 70 | +def test_run_without_stepwise(stepwise_testdir): |
| 71 | + result = stepwise_testdir.runpytest("-v", "--strict", "--fail") |
| 72 | + |
| 73 | + result.stdout.fnmatch_lines(["*test_success_before_fail PASSED*"]) |
| 74 | + result.stdout.fnmatch_lines(["*test_fail_on_flag FAILED*"]) |
| 75 | + result.stdout.fnmatch_lines(["*test_success_after_fail PASSED*"]) |
| 76 | + |
| 77 | + |
| 78 | +def test_fail_and_continue_with_stepwise(stepwise_testdir): |
| 79 | + # Run the tests with a failing second test. |
| 80 | + result = stepwise_testdir.runpytest("-v", "--strict", "--stepwise", "--fail") |
| 81 | + assert not result.stderr.str() |
| 82 | + |
| 83 | + stdout = result.stdout.str() |
| 84 | + # Make sure we stop after first failing test. |
| 85 | + assert "test_success_before_fail PASSED" in stdout |
| 86 | + assert "test_fail_on_flag FAILED" in stdout |
| 87 | + assert "test_success_after_fail" not in stdout |
| 88 | + |
| 89 | + # "Fix" the test that failed in the last run and run it again. |
| 90 | + result = stepwise_testdir.runpytest("-v", "--strict", "--stepwise") |
| 91 | + assert not result.stderr.str() |
| 92 | + |
| 93 | + stdout = result.stdout.str() |
| 94 | + # Make sure the latest failing test runs and then continues. |
| 95 | + assert "test_success_before_fail" not in stdout |
| 96 | + assert "test_fail_on_flag PASSED" in stdout |
| 97 | + assert "test_success_after_fail PASSED" in stdout |
| 98 | + |
| 99 | + |
| 100 | +def test_run_with_skip_option(stepwise_testdir): |
| 101 | + result = stepwise_testdir.runpytest( |
| 102 | + "-v", "--strict", "--stepwise", "--stepwise-skip", "--fail", "--fail-last" |
| 103 | + ) |
| 104 | + assert not result.stderr.str() |
| 105 | + |
| 106 | + stdout = result.stdout.str() |
| 107 | + # Make sure first fail is ignore and second fail stops the test run. |
| 108 | + assert "test_fail_on_flag FAILED" in stdout |
| 109 | + assert "test_success_after_fail PASSED" in stdout |
| 110 | + assert "test_fail_last_on_flag FAILED" in stdout |
| 111 | + assert "test_success_after_last_fail" not in stdout |
| 112 | + |
| 113 | + |
| 114 | +def test_fail_on_errors(error_testdir): |
| 115 | + result = error_testdir.runpytest("-v", "--strict", "--stepwise") |
| 116 | + |
| 117 | + assert not result.stderr.str() |
| 118 | + stdout = result.stdout.str() |
| 119 | + |
| 120 | + assert "test_error ERROR" in stdout |
| 121 | + assert "test_success_after_fail" not in stdout |
| 122 | + |
| 123 | + |
| 124 | +def test_change_testfile(stepwise_testdir): |
| 125 | + result = stepwise_testdir.runpytest( |
| 126 | + "-v", "--strict", "--stepwise", "--fail", "test_a.py" |
| 127 | + ) |
| 128 | + assert not result.stderr.str() |
| 129 | + |
| 130 | + stdout = result.stdout.str() |
| 131 | + assert "test_fail_on_flag FAILED" in stdout |
| 132 | + |
| 133 | + # Make sure the second test run starts from the beginning, since the |
| 134 | + # test to continue from does not exist in testfile_b. |
| 135 | + result = stepwise_testdir.runpytest("-v", "--strict", "--stepwise", "test_b.py") |
| 136 | + assert not result.stderr.str() |
| 137 | + |
| 138 | + stdout = result.stdout.str() |
| 139 | + assert "test_success PASSED" in stdout |
| 140 | + |
| 141 | + |
| 142 | +def test_stop_on_collection_errors(broken_testdir): |
| 143 | + result = broken_testdir.runpytest( |
| 144 | + "-v", "--strict", "--stepwise", "working_testfile.py", "broken_testfile.py" |
| 145 | + ) |
| 146 | + |
| 147 | + stdout = result.stdout.str() |
| 148 | + assert "errors during collection" in stdout |
0 commit comments