tests: revisit test_cacheprovider

This commit is contained in:
Daniel Hahler 2019-11-15 23:41:12 +01:00
parent 1abb08d52f
commit 1b4623a6d1
1 changed files with 53 additions and 150 deletions

View File

@ -2,7 +2,6 @@ import os
import shutil
import stat
import sys
import textwrap
import py
@ -65,13 +64,7 @@ class TestNewAPI:
mode = os.stat(cache_dir)[stat.ST_MODE]
testdir.tmpdir.ensure_dir(".pytest_cache").chmod(0)
try:
testdir.makepyfile(
"""
def test_error():
raise Exception
"""
)
testdir.makepyfile("def test_error(): raise Exception")
result = testdir.runpytest("-rw")
assert result.ret == 1
# warnings from nodeids, lastfailed, and stepwise
@ -178,12 +171,7 @@ def test_cache_reportheader_external_abspath(testdir, tmpdir_factory):
"test_cache_reportheader_external_abspath_abs"
)
testdir.makepyfile(
"""
def test_hello():
pass
"""
)
testdir.makepyfile("def test_hello(): pass")
testdir.makeini(
"""
[pytest]
@ -192,7 +180,6 @@ def test_cache_reportheader_external_abspath(testdir, tmpdir_factory):
abscache=external_cache
)
)
result = testdir.runpytest("-v")
result.stdout.fnmatch_lines(
["cachedir: {abscache}".format(abscache=external_cache)]
@ -256,33 +243,23 @@ class TestLastFailed:
monkeypatch.setattr("sys.dont_write_bytecode", True)
p = testdir.makepyfile(
"""
def test_1():
assert 0
def test_2():
assert 0
def test_3():
assert 1
"""
def test_1(): assert 0
def test_2(): assert 0
def test_3(): assert 1
"""
)
result = testdir.runpytest()
result = testdir.runpytest(str(p))
result.stdout.fnmatch_lines(["*2 failed*"])
p.write(
textwrap.dedent(
"""\
def test_1():
assert 1
def test_2():
assert 1
def test_3():
assert 0
"""
)
p = testdir.makepyfile(
"""
def test_1(): assert 1
def test_2(): assert 1
def test_3(): assert 0
"""
)
result = testdir.runpytest("--lf")
result = testdir.runpytest(str(p), "--lf")
result.stdout.fnmatch_lines(["*2 passed*1 desel*"])
result = testdir.runpytest("--lf")
result = testdir.runpytest(str(p), "--lf")
result.stdout.fnmatch_lines(
[
"collected 3 items",
@ -290,7 +267,7 @@ class TestLastFailed:
"*1 failed*2 passed*",
]
)
result = testdir.runpytest("--lf", "--cache-clear")
result = testdir.runpytest(str(p), "--lf", "--cache-clear")
result.stdout.fnmatch_lines(["*1 failed*2 passed*"])
# Run this again to make sure clear-cache is robust
@ -300,21 +277,9 @@ class TestLastFailed:
result.stdout.fnmatch_lines(["*1 failed*2 passed*"])
def test_failedfirst_order(self, testdir):
testdir.tmpdir.join("test_a.py").write(
textwrap.dedent(
"""\
def test_always_passes():
assert 1
"""
)
)
testdir.tmpdir.join("test_b.py").write(
textwrap.dedent(
"""\
def test_always_fails():
assert 0
"""
)
testdir.makepyfile(
test_a="def test_always_passes(): pass",
test_b="def test_always_fails(): assert 0",
)
result = testdir.runpytest()
# Test order will be collection order; alphabetical
@ -325,16 +290,8 @@ class TestLastFailed:
def test_lastfailed_failedfirst_order(self, testdir):
testdir.makepyfile(
**{
"test_a.py": """\
def test_always_passes():
assert 1
""",
"test_b.py": """\
def test_always_fails():
assert 0
""",
}
test_a="def test_always_passes(): assert 1",
test_b="def test_always_fails(): assert 0",
)
result = testdir.runpytest()
# Test order will be collection order; alphabetical
@ -347,16 +304,11 @@ class TestLastFailed:
def test_lastfailed_difference_invocations(self, testdir, monkeypatch):
monkeypatch.setattr("sys.dont_write_bytecode", True)
testdir.makepyfile(
test_a="""\
def test_a1():
assert 0
def test_a2():
assert 1
""",
test_b="""\
def test_b1():
assert 0
test_a="""
def test_a1(): assert 0
def test_a2(): assert 1
""",
test_b="def test_b1(): assert 0",
)
p = testdir.tmpdir.join("test_a.py")
p2 = testdir.tmpdir.join("test_b.py")
@ -365,14 +317,8 @@ class TestLastFailed:
result.stdout.fnmatch_lines(["*2 failed*"])
result = testdir.runpytest("--lf", p2)
result.stdout.fnmatch_lines(["*1 failed*"])
p2.write(
textwrap.dedent(
"""\
def test_b1():
assert 1
"""
)
)
testdir.makepyfile(test_b="def test_b1(): assert 1")
result = testdir.runpytest("--lf", p2)
result.stdout.fnmatch_lines(["*1 passed*"])
result = testdir.runpytest("--lf", p)
@ -381,20 +327,9 @@ class TestLastFailed:
def test_lastfailed_usecase_splice(self, testdir, monkeypatch):
monkeypatch.setattr("sys.dont_write_bytecode", True)
testdir.makepyfile(
"""\
def test_1():
assert 0
"""
"def test_1(): assert 0", test_something="def test_2(): assert 0"
)
p2 = testdir.tmpdir.join("test_something.py")
p2.write(
textwrap.dedent(
"""\
def test_2():
assert 0
"""
)
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*2 failed*"])
result = testdir.runpytest("--lf", p2)
@ -436,18 +371,14 @@ class TestLastFailed:
def test_terminal_report_lastfailed(self, testdir):
test_a = testdir.makepyfile(
test_a="""
def test_a1():
pass
def test_a2():
pass
def test_a1(): pass
def test_a2(): pass
"""
)
test_b = testdir.makepyfile(
test_b="""
def test_b1():
assert 0
def test_b2():
assert 0
def test_b1(): assert 0
def test_b2(): assert 0
"""
)
result = testdir.runpytest()
@ -492,10 +423,8 @@ class TestLastFailed:
def test_terminal_report_failedfirst(self, testdir):
testdir.makepyfile(
test_a="""
def test_a1():
assert 0
def test_a2():
pass
def test_a1(): assert 0
def test_a2(): pass
"""
)
result = testdir.runpytest()
@ -542,7 +471,6 @@ class TestLastFailed:
assert list(lastfailed) == ["test_maybe.py::test_hello"]
def test_lastfailed_failure_subset(self, testdir, monkeypatch):
testdir.makepyfile(
test_maybe="""
import os
@ -560,6 +488,7 @@ class TestLastFailed:
env = os.environ
if '1' == env['FAILIMPORT']:
raise ImportError('fail')
def test_hello():
assert '0' == env['FAILTEST']
@ -613,8 +542,7 @@ class TestLastFailed:
"""
import pytest
@pytest.mark.xfail
def test():
assert 0
def test(): assert 0
"""
)
result = testdir.runpytest()
@ -626,8 +554,7 @@ class TestLastFailed:
"""
import pytest
@pytest.mark.xfail(strict=True)
def test():
pass
def test(): pass
"""
)
result = testdir.runpytest()
@ -641,8 +568,7 @@ class TestLastFailed:
testdir.makepyfile(
"""
import pytest
def test():
assert 0
def test(): assert 0
"""
)
result = testdir.runpytest()
@ -655,8 +581,7 @@ class TestLastFailed:
"""
import pytest
@pytest.{mark}
def test():
assert 0
def test(): assert 0
""".format(
mark=mark
)
@ -694,18 +619,14 @@ class TestLastFailed:
# 1. initial run
test_bar = testdir.makepyfile(
test_bar="""
def test_bar_1():
pass
def test_bar_2():
assert 0
def test_bar_1(): pass
def test_bar_2(): assert 0
"""
)
test_foo = testdir.makepyfile(
test_foo="""
def test_foo_3():
pass
def test_foo_4():
assert 0
def test_foo_3(): pass
def test_foo_4(): assert 0
"""
)
testdir.runpytest()
@ -717,10 +638,8 @@ class TestLastFailed:
# 2. fix test_bar_2, run only test_bar.py
testdir.makepyfile(
test_bar="""
def test_bar_1():
pass
def test_bar_2():
pass
def test_bar_1(): pass
def test_bar_2(): pass
"""
)
result = testdir.runpytest(test_bar)
@ -735,10 +654,8 @@ class TestLastFailed:
# 3. fix test_foo_4, run only test_foo.py
test_foo = testdir.makepyfile(
test_foo="""
def test_foo_3():
pass
def test_foo_4():
pass
def test_foo_3(): pass
def test_foo_4(): pass
"""
)
result = testdir.runpytest(test_foo, "--last-failed")
@ -752,10 +669,8 @@ class TestLastFailed:
def test_lastfailed_no_failures_behavior_all_passed(self, testdir):
testdir.makepyfile(
"""
def test_1():
assert True
def test_2():
assert True
def test_1(): pass
def test_2(): pass
"""
)
result = testdir.runpytest()
@ -777,10 +692,8 @@ class TestLastFailed:
def test_lastfailed_no_failures_behavior_empty_cache(self, testdir):
testdir.makepyfile(
"""
def test_1():
assert True
def test_2():
assert False
def test_1(): pass
def test_2(): assert 0
"""
)
result = testdir.runpytest("--lf", "--cache-clear")
@ -1022,22 +935,12 @@ class TestReadme:
return readme.is_file()
def test_readme_passed(self, testdir):
testdir.makepyfile(
"""
def test_always_passes():
assert 1
"""
)
testdir.makepyfile("def test_always_passes(): pass")
testdir.runpytest()
assert self.check_readme(testdir) is True
def test_readme_failed(self, testdir):
testdir.makepyfile(
"""
def test_always_fails():
assert 0
"""
)
testdir.makepyfile("def test_always_fails(): assert 0")
testdir.runpytest()
assert self.check_readme(testdir) is True