test_ok1/testing/test_stepwise.py

157 lines
4.3 KiB
Python
Raw Normal View History

2015-09-26 20:30:16 +08:00
import pytest
@pytest.fixture
def stepwise_testdir(testdir):
# Rather than having to modify our testfile between tests, we introduce
# a flag for wether or not the second test should fail.
2018-10-15 03:48:46 +08:00
testdir.makeconftest(
"""
2015-09-26 20:30:16 +08:00
def pytest_addoption(parser):
group = parser.getgroup('general')
group.addoption('--fail', action='store_true', dest='fail')
group.addoption('--fail-last', action='store_true', dest='fail_last')
2018-10-15 03:48:46 +08:00
"""
)
2015-09-26 20:30:16 +08:00
# Create a simple test suite.
2018-10-15 03:48:46 +08:00
testdir.makepyfile(
test_a="""
2015-09-26 20:30:16 +08:00
def test_success_before_fail():
assert 1
def test_fail_on_flag(request):
assert not request.config.getvalue('fail')
def test_success_after_fail():
assert 1
def test_fail_last_on_flag(request):
assert not request.config.getvalue('fail_last')
def test_success_after_last_fail():
assert 1
2018-10-15 03:48:46 +08:00
"""
)
2015-09-26 20:30:16 +08:00
2018-10-15 03:48:46 +08:00
testdir.makepyfile(
test_b="""
2015-09-26 20:30:16 +08:00
def test_success():
assert 1
2018-10-15 03:48:46 +08:00
"""
)
2015-09-26 20:30:16 +08:00
# customize cache directory so we don't use the tox's cache directory, which makes tests in this module flaky
testdir.makeini(
"""
[pytest]
cache_dir = .cache
"""
)
2015-09-26 20:30:16 +08:00
return testdir
@pytest.fixture
def error_testdir(testdir):
2018-10-15 03:48:46 +08:00
testdir.makepyfile(
test_a="""
2015-09-26 20:30:16 +08:00
def test_error(nonexisting_fixture):
assert 1
def test_success_after_fail():
assert 1
2018-10-15 03:48:46 +08:00
"""
)
2015-09-26 20:30:16 +08:00
return testdir
@pytest.fixture
def broken_testdir(testdir):
2018-10-15 03:48:46 +08:00
testdir.makepyfile(
working_testfile="def test_proper(): assert 1", broken_testfile="foobar"
)
2015-09-26 20:30:16 +08:00
return testdir
def test_run_without_stepwise(stepwise_testdir):
2018-10-15 03:48:46 +08:00
result = stepwise_testdir.runpytest("-v", "--strict", "--fail")
2015-09-26 20:30:16 +08:00
2018-10-15 03:48:46 +08:00
result.stdout.fnmatch_lines(["*test_success_before_fail PASSED*"])
result.stdout.fnmatch_lines(["*test_fail_on_flag FAILED*"])
result.stdout.fnmatch_lines(["*test_success_after_fail PASSED*"])
2015-09-26 20:30:16 +08:00
def test_fail_and_continue_with_stepwise(stepwise_testdir):
# Run the tests with a failing second test.
2018-10-15 03:48:46 +08:00
result = stepwise_testdir.runpytest("-v", "--strict", "--stepwise", "--fail")
assert not result.stderr.str()
2015-09-26 20:30:16 +08:00
stdout = result.stdout.str()
# Make sure we stop after first failing test.
2018-10-15 03:48:46 +08:00
assert "test_success_before_fail PASSED" in stdout
assert "test_fail_on_flag FAILED" in stdout
assert "test_success_after_fail" not in stdout
2015-09-26 20:30:16 +08:00
# "Fix" the test that failed in the last run and run it again.
2018-10-15 03:48:46 +08:00
result = stepwise_testdir.runpytest("-v", "--strict", "--stepwise")
assert not result.stderr.str()
2015-09-26 20:30:16 +08:00
stdout = result.stdout.str()
# Make sure the latest failing test runs and then continues.
2018-10-15 03:48:46 +08:00
assert "test_success_before_fail" not in stdout
assert "test_fail_on_flag PASSED" in stdout
assert "test_success_after_fail PASSED" in stdout
2015-09-26 20:30:16 +08:00
def test_run_with_skip_option(stepwise_testdir):
2018-10-15 03:48:46 +08:00
result = stepwise_testdir.runpytest(
"-v", "--strict", "--stepwise", "--stepwise-skip", "--fail", "--fail-last"
)
assert not result.stderr.str()
2015-09-26 20:30:16 +08:00
stdout = result.stdout.str()
# Make sure first fail is ignore and second fail stops the test run.
2018-10-15 03:48:46 +08:00
assert "test_fail_on_flag FAILED" in stdout
assert "test_success_after_fail PASSED" in stdout
assert "test_fail_last_on_flag FAILED" in stdout
assert "test_success_after_last_fail" not in stdout
2015-09-26 20:30:16 +08:00
def test_fail_on_errors(error_testdir):
2018-10-15 03:48:46 +08:00
result = error_testdir.runpytest("-v", "--strict", "--stepwise")
2015-09-26 20:30:16 +08:00
assert not result.stderr.str()
2015-09-26 20:30:16 +08:00
stdout = result.stdout.str()
2018-10-15 03:48:46 +08:00
assert "test_error ERROR" in stdout
assert "test_success_after_fail" not in stdout
2015-09-26 20:30:16 +08:00
def test_change_testfile(stepwise_testdir):
2018-10-15 03:48:46 +08:00
result = stepwise_testdir.runpytest(
"-v", "--strict", "--stepwise", "--fail", "test_a.py"
)
assert not result.stderr.str()
2015-09-26 20:30:16 +08:00
stdout = result.stdout.str()
2018-10-15 03:48:46 +08:00
assert "test_fail_on_flag FAILED" in stdout
2015-09-26 20:30:16 +08:00
# Make sure the second test run starts from the beginning, since the
# test to continue from does not exist in testfile_b.
2018-10-15 03:48:46 +08:00
result = stepwise_testdir.runpytest("-v", "--strict", "--stepwise", "test_b.py")
assert not result.stderr.str()
2015-09-26 20:30:16 +08:00
stdout = result.stdout.str()
2018-10-15 03:48:46 +08:00
assert "test_success PASSED" in stdout
2015-09-26 20:30:16 +08:00
def test_stop_on_collection_errors(broken_testdir):
2018-10-15 03:48:46 +08:00
result = broken_testdir.runpytest(
"-v", "--strict", "--stepwise", "working_testfile.py", "broken_testfile.py"
)
2015-09-26 20:30:16 +08:00
stdout = result.stdout.str()
2018-10-15 04:58:30 +08:00
assert "errors during collection" in stdout