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