Skip to content

Commit 8c2e871

Browse files
committed
tests: revisit test_cacheprovider
1 parent 1abb08d commit 8c2e871

File tree

1 file changed

+54
-149
lines changed

1 file changed

+54
-149
lines changed

testing/test_cacheprovider.py

Lines changed: 54 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import shutil
33
import stat
44
import sys
5-
import textwrap
65

76
import py
87

@@ -65,13 +64,7 @@ def test_cache_failure_warns(self, testdir):
6564
mode = os.stat(cache_dir)[stat.ST_MODE]
6665
testdir.tmpdir.ensure_dir(".pytest_cache").chmod(0)
6766
try:
68-
testdir.makepyfile(
69-
"""
70-
def test_error():
71-
raise Exception
72-
73-
"""
74-
)
67+
testdir.makepyfile("def test_error(): raise Exception")
7568
result = testdir.runpytest("-rw")
7669
assert result.ret == 1
7770
# warnings from nodeids, lastfailed, and stepwise
@@ -178,12 +171,7 @@ def test_cache_reportheader_external_abspath(testdir, tmpdir_factory):
178171
"test_cache_reportheader_external_abspath_abs"
179172
)
180173

181-
testdir.makepyfile(
182-
"""
183-
def test_hello():
184-
pass
185-
"""
186-
)
174+
testdir.makepyfile("def test_hello(): pass")
187175
testdir.makeini(
188176
"""
189177
[pytest]
@@ -192,7 +180,6 @@ def test_hello():
192180
abscache=external_cache
193181
)
194182
)
195-
196183
result = testdir.runpytest("-v")
197184
result.stdout.fnmatch_lines(
198185
["cachedir: {abscache}".format(abscache=external_cache)]
@@ -256,41 +243,31 @@ def test_lastfailed_usecase(self, testdir, monkeypatch):
256243
monkeypatch.setattr("sys.dont_write_bytecode", True)
257244
p = testdir.makepyfile(
258245
"""
259-
def test_1():
260-
assert 0
261-
def test_2():
262-
assert 0
263-
def test_3():
264-
assert 1
265-
"""
246+
def test_1(): assert 0
247+
def test_2(): assert 0
248+
def test_3(): assert 1
249+
"""
266250
)
267-
result = testdir.runpytest()
251+
result = testdir.runpytest(str(p))
268252
result.stdout.fnmatch_lines(["*2 failed*"])
269-
p.write(
270-
textwrap.dedent(
271-
"""\
272-
def test_1():
273-
assert 1
274-
275-
def test_2():
276-
assert 1
277-
278-
def test_3():
279-
assert 0
280-
"""
281-
)
253+
p = testdir.makepyfile(
254+
"""
255+
def test_1(): assert 1
256+
def test_2(): assert 1
257+
def test_3(): assert 0
258+
"""
282259
)
283-
result = testdir.runpytest("--lf")
260+
result = testdir.runpytest(str(p), "--lf")
284261
result.stdout.fnmatch_lines(["*2 passed*1 desel*"])
285-
result = testdir.runpytest("--lf")
262+
result = testdir.runpytest(str(p), "--lf")
286263
result.stdout.fnmatch_lines(
287264
[
288265
"collected 3 items",
289266
"run-last-failure: no previously failed tests, not deselecting items.",
290267
"*1 failed*2 passed*",
291268
]
292269
)
293-
result = testdir.runpytest("--lf", "--cache-clear")
270+
result = testdir.runpytest(str(p), "--lf", "--cache-clear")
294271
result.stdout.fnmatch_lines(["*1 failed*2 passed*"])
295272

296273
# Run this again to make sure clear-cache is robust
@@ -300,22 +277,8 @@ def test_3():
300277
result.stdout.fnmatch_lines(["*1 failed*2 passed*"])
301278

302279
def test_failedfirst_order(self, testdir):
303-
testdir.tmpdir.join("test_a.py").write(
304-
textwrap.dedent(
305-
"""\
306-
def test_always_passes():
307-
assert 1
308-
"""
309-
)
310-
)
311-
testdir.tmpdir.join("test_b.py").write(
312-
textwrap.dedent(
313-
"""\
314-
def test_always_fails():
315-
assert 0
316-
"""
317-
)
318-
)
280+
testdir.makepyfile(**{"test_a.py": "def test_always_passes(): pass"})
281+
testdir.makepyfile(**{"test_b.py": "def test_always_fails(): assert 0"})
319282
result = testdir.runpytest()
320283
# Test order will be collection order; alphabetical
321284
result.stdout.fnmatch_lines(["test_a.py*", "test_b.py*"])
@@ -326,14 +289,8 @@ def test_always_fails():
326289
def test_lastfailed_failedfirst_order(self, testdir):
327290
testdir.makepyfile(
328291
**{
329-
"test_a.py": """\
330-
def test_always_passes():
331-
assert 1
332-
""",
333-
"test_b.py": """\
334-
def test_always_fails():
335-
assert 0
336-
""",
292+
"test_a.py": "def test_always_passes(): assert 1",
293+
"test_b.py": "def test_always_fails(): assert 0",
337294
}
338295
)
339296
result = testdir.runpytest()
@@ -347,16 +304,13 @@ def test_always_fails():
347304
def test_lastfailed_difference_invocations(self, testdir, monkeypatch):
348305
monkeypatch.setattr("sys.dont_write_bytecode", True)
349306
testdir.makepyfile(
350-
test_a="""\
351-
def test_a1():
352-
assert 0
353-
def test_a2():
354-
assert 1
355-
""",
356-
test_b="""\
357-
def test_b1():
358-
assert 0
307+
**{
308+
"test_a": """
309+
def test_a1(): assert 0
310+
def test_a2(): assert 1
359311
""",
312+
"test_b": "def test_b1(): assert 0",
313+
}
360314
)
361315
p = testdir.tmpdir.join("test_a.py")
362316
p2 = testdir.tmpdir.join("test_b.py")
@@ -365,14 +319,8 @@ def test_b1():
365319
result.stdout.fnmatch_lines(["*2 failed*"])
366320
result = testdir.runpytest("--lf", p2)
367321
result.stdout.fnmatch_lines(["*1 failed*"])
368-
p2.write(
369-
textwrap.dedent(
370-
"""\
371-
def test_b1():
372-
assert 1
373-
"""
374-
)
375-
)
322+
323+
testdir.makepyfile(test_b="def test_b1(): assert 1")
376324
result = testdir.runpytest("--lf", p2)
377325
result.stdout.fnmatch_lines(["*1 passed*"])
378326
result = testdir.runpytest("--lf", p)
@@ -381,20 +329,9 @@ def test_b1():
381329
def test_lastfailed_usecase_splice(self, testdir, monkeypatch):
382330
monkeypatch.setattr("sys.dont_write_bytecode", True)
383331
testdir.makepyfile(
384-
"""\
385-
def test_1():
386-
assert 0
387-
"""
332+
"def test_1(): assert 0", **{"test_something.py": "def test_2(): assert 0"}
388333
)
389334
p2 = testdir.tmpdir.join("test_something.py")
390-
p2.write(
391-
textwrap.dedent(
392-
"""\
393-
def test_2():
394-
assert 0
395-
"""
396-
)
397-
)
398335
result = testdir.runpytest()
399336
result.stdout.fnmatch_lines(["*2 failed*"])
400337
result = testdir.runpytest("--lf", p2)
@@ -436,18 +373,14 @@ def test_fail(val):
436373
def test_terminal_report_lastfailed(self, testdir):
437374
test_a = testdir.makepyfile(
438375
test_a="""
439-
def test_a1():
440-
pass
441-
def test_a2():
442-
pass
376+
def test_a1(): pass
377+
def test_a2(): pass
443378
"""
444379
)
445380
test_b = testdir.makepyfile(
446381
test_b="""
447-
def test_b1():
448-
assert 0
449-
def test_b2():
450-
assert 0
382+
def test_b1(): assert 0
383+
def test_b2(): assert 0
451384
"""
452385
)
453386
result = testdir.runpytest()
@@ -492,10 +425,8 @@ def test_b2():
492425
def test_terminal_report_failedfirst(self, testdir):
493426
testdir.makepyfile(
494427
test_a="""
495-
def test_a1():
496-
assert 0
497-
def test_a2():
498-
pass
428+
def test_a1(): assert 0
429+
def test_a2(): pass
499430
"""
500431
)
501432
result = testdir.runpytest()
@@ -542,7 +473,6 @@ def rlf(fail_import, fail_run):
542473
assert list(lastfailed) == ["test_maybe.py::test_hello"]
543474

544475
def test_lastfailed_failure_subset(self, testdir, monkeypatch):
545-
546476
testdir.makepyfile(
547477
test_maybe="""
548478
import os
@@ -560,6 +490,7 @@ def test_hello():
560490
env = os.environ
561491
if '1' == env['FAILIMPORT']:
562492
raise ImportError('fail')
493+
563494
def test_hello():
564495
assert '0' == env['FAILTEST']
565496
@@ -613,8 +544,7 @@ def test_xfail_not_considered_failure(self, testdir):
613544
"""
614545
import pytest
615546
@pytest.mark.xfail
616-
def test():
617-
assert 0
547+
def test(): assert 0
618548
"""
619549
)
620550
result = testdir.runpytest()
@@ -626,8 +556,7 @@ def test_xfail_strict_considered_failure(self, testdir):
626556
"""
627557
import pytest
628558
@pytest.mark.xfail(strict=True)
629-
def test():
630-
pass
559+
def test(): pass
631560
"""
632561
)
633562
result = testdir.runpytest()
@@ -641,8 +570,7 @@ def test_failed_changed_to_xfail_or_skip(self, testdir, mark):
641570
testdir.makepyfile(
642571
"""
643572
import pytest
644-
def test():
645-
assert 0
573+
def test(): assert 0
646574
"""
647575
)
648576
result = testdir.runpytest()
@@ -655,8 +583,7 @@ def test():
655583
"""
656584
import pytest
657585
@pytest.{mark}
658-
def test():
659-
assert 0
586+
def test(): assert 0
660587
""".format(
661588
mark=mark
662589
)
@@ -694,18 +621,14 @@ def test_cache_cumulative(self, testdir):
694621
# 1. initial run
695622
test_bar = testdir.makepyfile(
696623
test_bar="""
697-
def test_bar_1():
698-
pass
699-
def test_bar_2():
700-
assert 0
624+
def test_bar_1(): pass
625+
def test_bar_2(): assert 0
701626
"""
702627
)
703628
test_foo = testdir.makepyfile(
704629
test_foo="""
705-
def test_foo_3():
706-
pass
707-
def test_foo_4():
708-
assert 0
630+
def test_foo_3(): pass
631+
def test_foo_4(): assert 0
709632
"""
710633
)
711634
testdir.runpytest()
@@ -717,10 +640,8 @@ def test_foo_4():
717640
# 2. fix test_bar_2, run only test_bar.py
718641
testdir.makepyfile(
719642
test_bar="""
720-
def test_bar_1():
721-
pass
722-
def test_bar_2():
723-
pass
643+
def test_bar_1(): pass
644+
def test_bar_2(): pass
724645
"""
725646
)
726647
result = testdir.runpytest(test_bar)
@@ -735,10 +656,8 @@ def test_bar_2():
735656
# 3. fix test_foo_4, run only test_foo.py
736657
test_foo = testdir.makepyfile(
737658
test_foo="""
738-
def test_foo_3():
739-
pass
740-
def test_foo_4():
741-
pass
659+
def test_foo_3(): pass
660+
def test_foo_4(): pass
742661
"""
743662
)
744663
result = testdir.runpytest(test_foo, "--last-failed")
@@ -752,10 +671,8 @@ def test_foo_4():
752671
def test_lastfailed_no_failures_behavior_all_passed(self, testdir):
753672
testdir.makepyfile(
754673
"""
755-
def test_1():
756-
assert True
757-
def test_2():
758-
assert True
674+
def test_1(): pass
675+
def test_2(): pass
759676
"""
760677
)
761678
result = testdir.runpytest()
@@ -777,10 +694,8 @@ def test_2():
777694
def test_lastfailed_no_failures_behavior_empty_cache(self, testdir):
778695
testdir.makepyfile(
779696
"""
780-
def test_1():
781-
assert True
782-
def test_2():
783-
assert False
697+
def test_1(): pass
698+
def test_2(): assert 0
784699
"""
785700
)
786701
result = testdir.runpytest("--lf", "--cache-clear")
@@ -1022,22 +937,12 @@ def check_readme(self, testdir):
1022937
return readme.is_file()
1023938

1024939
def test_readme_passed(self, testdir):
1025-
testdir.makepyfile(
1026-
"""
1027-
def test_always_passes():
1028-
assert 1
1029-
"""
1030-
)
940+
testdir.makepyfile("def test_always_passes(): pass")
1031941
testdir.runpytest()
1032942
assert self.check_readme(testdir) is True
1033943

1034944
def test_readme_failed(self, testdir):
1035-
testdir.makepyfile(
1036-
"""
1037-
def test_always_fails():
1038-
assert 0
1039-
"""
1040-
)
945+
testdir.makepyfile("def test_always_fails(): assert 0")
1041946
testdir.runpytest()
1042947
assert self.check_readme(testdir) is True
1043948

0 commit comments

Comments
 (0)