Skip to content

Commit 1b4623a

Browse files
committed
tests: revisit test_cacheprovider
1 parent 1abb08d commit 1b4623a

File tree

1 file changed

+53
-150
lines changed

1 file changed

+53
-150
lines changed

testing/test_cacheprovider.py

Lines changed: 53 additions & 150 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,21 +277,9 @@ 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-
)
280+
testdir.makepyfile(
281+
test_a="def test_always_passes(): pass",
282+
test_b="def test_always_fails(): assert 0",
318283
)
319284
result = testdir.runpytest()
320285
# Test order will be collection order; alphabetical
@@ -325,16 +290,8 @@ def test_always_fails():
325290

326291
def test_lastfailed_failedfirst_order(self, testdir):
327292
testdir.makepyfile(
328-
**{
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-
""",
337-
}
293+
test_a="def test_always_passes(): assert 1",
294+
test_b="def test_always_fails(): assert 0",
338295
)
339296
result = testdir.runpytest()
340297
# Test order will be collection order; alphabetical
@@ -347,16 +304,11 @@ 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+
test_a="""
308+
def test_a1(): assert 0
309+
def test_a2(): assert 1
359310
""",
311+
test_b="def test_b1(): assert 0",
360312
)
361313
p = testdir.tmpdir.join("test_a.py")
362314
p2 = testdir.tmpdir.join("test_b.py")
@@ -365,14 +317,8 @@ def test_b1():
365317
result.stdout.fnmatch_lines(["*2 failed*"])
366318
result = testdir.runpytest("--lf", p2)
367319
result.stdout.fnmatch_lines(["*1 failed*"])
368-
p2.write(
369-
textwrap.dedent(
370-
"""\
371-
def test_b1():
372-
assert 1
373-
"""
374-
)
375-
)
320+
321+
testdir.makepyfile(test_b="def test_b1(): assert 1")
376322
result = testdir.runpytest("--lf", p2)
377323
result.stdout.fnmatch_lines(["*1 passed*"])
378324
result = testdir.runpytest("--lf", p)
@@ -381,20 +327,9 @@ def test_b1():
381327
def test_lastfailed_usecase_splice(self, testdir, monkeypatch):
382328
monkeypatch.setattr("sys.dont_write_bytecode", True)
383329
testdir.makepyfile(
384-
"""\
385-
def test_1():
386-
assert 0
387-
"""
330+
"def test_1(): assert 0", test_something="def test_2(): assert 0"
388331
)
389332
p2 = testdir.tmpdir.join("test_something.py")
390-
p2.write(
391-
textwrap.dedent(
392-
"""\
393-
def test_2():
394-
assert 0
395-
"""
396-
)
397-
)
398333
result = testdir.runpytest()
399334
result.stdout.fnmatch_lines(["*2 failed*"])
400335
result = testdir.runpytest("--lf", p2)
@@ -436,18 +371,14 @@ def test_fail(val):
436371
def test_terminal_report_lastfailed(self, testdir):
437372
test_a = testdir.makepyfile(
438373
test_a="""
439-
def test_a1():
440-
pass
441-
def test_a2():
442-
pass
374+
def test_a1(): pass
375+
def test_a2(): pass
443376
"""
444377
)
445378
test_b = testdir.makepyfile(
446379
test_b="""
447-
def test_b1():
448-
assert 0
449-
def test_b2():
450-
assert 0
380+
def test_b1(): assert 0
381+
def test_b2(): assert 0
451382
"""
452383
)
453384
result = testdir.runpytest()
@@ -492,10 +423,8 @@ def test_b2():
492423
def test_terminal_report_failedfirst(self, testdir):
493424
testdir.makepyfile(
494425
test_a="""
495-
def test_a1():
496-
assert 0
497-
def test_a2():
498-
pass
426+
def test_a1(): assert 0
427+
def test_a2(): pass
499428
"""
500429
)
501430
result = testdir.runpytest()
@@ -542,7 +471,6 @@ def rlf(fail_import, fail_run):
542471
assert list(lastfailed) == ["test_maybe.py::test_hello"]
543472

544473
def test_lastfailed_failure_subset(self, testdir, monkeypatch):
545-
546474
testdir.makepyfile(
547475
test_maybe="""
548476
import os
@@ -560,6 +488,7 @@ def test_hello():
560488
env = os.environ
561489
if '1' == env['FAILIMPORT']:
562490
raise ImportError('fail')
491+
563492
def test_hello():
564493
assert '0' == env['FAILTEST']
565494
@@ -613,8 +542,7 @@ def test_xfail_not_considered_failure(self, testdir):
613542
"""
614543
import pytest
615544
@pytest.mark.xfail
616-
def test():
617-
assert 0
545+
def test(): assert 0
618546
"""
619547
)
620548
result = testdir.runpytest()
@@ -626,8 +554,7 @@ def test_xfail_strict_considered_failure(self, testdir):
626554
"""
627555
import pytest
628556
@pytest.mark.xfail(strict=True)
629-
def test():
630-
pass
557+
def test(): pass
631558
"""
632559
)
633560
result = testdir.runpytest()
@@ -641,8 +568,7 @@ def test_failed_changed_to_xfail_or_skip(self, testdir, mark):
641568
testdir.makepyfile(
642569
"""
643570
import pytest
644-
def test():
645-
assert 0
571+
def test(): assert 0
646572
"""
647573
)
648574
result = testdir.runpytest()
@@ -655,8 +581,7 @@ def test():
655581
"""
656582
import pytest
657583
@pytest.{mark}
658-
def test():
659-
assert 0
584+
def test(): assert 0
660585
""".format(
661586
mark=mark
662587
)
@@ -694,18 +619,14 @@ def test_cache_cumulative(self, testdir):
694619
# 1. initial run
695620
test_bar = testdir.makepyfile(
696621
test_bar="""
697-
def test_bar_1():
698-
pass
699-
def test_bar_2():
700-
assert 0
622+
def test_bar_1(): pass
623+
def test_bar_2(): assert 0
701624
"""
702625
)
703626
test_foo = testdir.makepyfile(
704627
test_foo="""
705-
def test_foo_3():
706-
pass
707-
def test_foo_4():
708-
assert 0
628+
def test_foo_3(): pass
629+
def test_foo_4(): assert 0
709630
"""
710631
)
711632
testdir.runpytest()
@@ -717,10 +638,8 @@ def test_foo_4():
717638
# 2. fix test_bar_2, run only test_bar.py
718639
testdir.makepyfile(
719640
test_bar="""
720-
def test_bar_1():
721-
pass
722-
def test_bar_2():
723-
pass
641+
def test_bar_1(): pass
642+
def test_bar_2(): pass
724643
"""
725644
)
726645
result = testdir.runpytest(test_bar)
@@ -735,10 +654,8 @@ def test_bar_2():
735654
# 3. fix test_foo_4, run only test_foo.py
736655
test_foo = testdir.makepyfile(
737656
test_foo="""
738-
def test_foo_3():
739-
pass
740-
def test_foo_4():
741-
pass
657+
def test_foo_3(): pass
658+
def test_foo_4(): pass
742659
"""
743660
)
744661
result = testdir.runpytest(test_foo, "--last-failed")
@@ -752,10 +669,8 @@ def test_foo_4():
752669
def test_lastfailed_no_failures_behavior_all_passed(self, testdir):
753670
testdir.makepyfile(
754671
"""
755-
def test_1():
756-
assert True
757-
def test_2():
758-
assert True
672+
def test_1(): pass
673+
def test_2(): pass
759674
"""
760675
)
761676
result = testdir.runpytest()
@@ -777,10 +692,8 @@ def test_2():
777692
def test_lastfailed_no_failures_behavior_empty_cache(self, testdir):
778693
testdir.makepyfile(
779694
"""
780-
def test_1():
781-
assert True
782-
def test_2():
783-
assert False
695+
def test_1(): pass
696+
def test_2(): assert 0
784697
"""
785698
)
786699
result = testdir.runpytest("--lf", "--cache-clear")
@@ -1022,22 +935,12 @@ def check_readme(self, testdir):
1022935
return readme.is_file()
1023936

1024937
def test_readme_passed(self, testdir):
1025-
testdir.makepyfile(
1026-
"""
1027-
def test_always_passes():
1028-
assert 1
1029-
"""
1030-
)
938+
testdir.makepyfile("def test_always_passes(): pass")
1031939
testdir.runpytest()
1032940
assert self.check_readme(testdir) is True
1033941

1034942
def test_readme_failed(self, testdir):
1035-
testdir.makepyfile(
1036-
"""
1037-
def test_always_fails():
1038-
assert 0
1039-
"""
1040-
)
943+
testdir.makepyfile("def test_always_fails(): assert 0")
1041944
testdir.runpytest()
1042945
assert self.check_readme(testdir) is True
1043946

0 commit comments

Comments
 (0)