2
2
import shutil
3
3
import stat
4
4
import sys
5
- import textwrap
6
5
7
6
import py
8
7
@@ -65,13 +64,7 @@ def test_cache_failure_warns(self, testdir):
65
64
mode = os .stat (cache_dir )[stat .ST_MODE ]
66
65
testdir .tmpdir .ensure_dir (".pytest_cache" ).chmod (0 )
67
66
try :
68
- testdir .makepyfile (
69
- """
70
- def test_error():
71
- raise Exception
72
-
73
- """
74
- )
67
+ testdir .makepyfile ("def test_error(): raise Exception" )
75
68
result = testdir .runpytest ("-rw" )
76
69
assert result .ret == 1
77
70
# warnings from nodeids, lastfailed, and stepwise
@@ -178,12 +171,7 @@ def test_cache_reportheader_external_abspath(testdir, tmpdir_factory):
178
171
"test_cache_reportheader_external_abspath_abs"
179
172
)
180
173
181
- testdir .makepyfile (
182
- """
183
- def test_hello():
184
- pass
185
- """
186
- )
174
+ testdir .makepyfile ("def test_hello(): pass" )
187
175
testdir .makeini (
188
176
"""
189
177
[pytest]
@@ -192,7 +180,6 @@ def test_hello():
192
180
abscache = external_cache
193
181
)
194
182
)
195
-
196
183
result = testdir .runpytest ("-v" )
197
184
result .stdout .fnmatch_lines (
198
185
["cachedir: {abscache}" .format (abscache = external_cache )]
@@ -256,41 +243,31 @@ def test_lastfailed_usecase(self, testdir, monkeypatch):
256
243
monkeypatch .setattr ("sys.dont_write_bytecode" , True )
257
244
p = testdir .makepyfile (
258
245
"""
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
+ """
266
250
)
267
- result = testdir .runpytest ()
251
+ result = testdir .runpytest (str ( p ) )
268
252
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
+ """
282
259
)
283
- result = testdir .runpytest ("--lf" )
260
+ result = testdir .runpytest (str ( p ), "--lf" )
284
261
result .stdout .fnmatch_lines (["*2 passed*1 desel*" ])
285
- result = testdir .runpytest ("--lf" )
262
+ result = testdir .runpytest (str ( p ), "--lf" )
286
263
result .stdout .fnmatch_lines (
287
264
[
288
265
"collected 3 items" ,
289
266
"run-last-failure: no previously failed tests, not deselecting items." ,
290
267
"*1 failed*2 passed*" ,
291
268
]
292
269
)
293
- result = testdir .runpytest ("--lf" , "--cache-clear" )
270
+ result = testdir .runpytest (str ( p ), "--lf" , "--cache-clear" )
294
271
result .stdout .fnmatch_lines (["*1 failed*2 passed*" ])
295
272
296
273
# Run this again to make sure clear-cache is robust
@@ -300,21 +277,9 @@ def test_3():
300
277
result .stdout .fnmatch_lines (["*1 failed*2 passed*" ])
301
278
302
279
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" ,
318
283
)
319
284
result = testdir .runpytest ()
320
285
# Test order will be collection order; alphabetical
@@ -325,16 +290,8 @@ def test_always_fails():
325
290
326
291
def test_lastfailed_failedfirst_order (self , testdir ):
327
292
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" ,
338
295
)
339
296
result = testdir .runpytest ()
340
297
# Test order will be collection order; alphabetical
@@ -347,16 +304,11 @@ def test_always_fails():
347
304
def test_lastfailed_difference_invocations (self , testdir , monkeypatch ):
348
305
monkeypatch .setattr ("sys.dont_write_bytecode" , True )
349
306
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
359
310
""" ,
311
+ test_b = "def test_b1(): assert 0" ,
360
312
)
361
313
p = testdir .tmpdir .join ("test_a.py" )
362
314
p2 = testdir .tmpdir .join ("test_b.py" )
@@ -365,14 +317,8 @@ def test_b1():
365
317
result .stdout .fnmatch_lines (["*2 failed*" ])
366
318
result = testdir .runpytest ("--lf" , p2 )
367
319
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" )
376
322
result = testdir .runpytest ("--lf" , p2 )
377
323
result .stdout .fnmatch_lines (["*1 passed*" ])
378
324
result = testdir .runpytest ("--lf" , p )
@@ -381,20 +327,9 @@ def test_b1():
381
327
def test_lastfailed_usecase_splice (self , testdir , monkeypatch ):
382
328
monkeypatch .setattr ("sys.dont_write_bytecode" , True )
383
329
testdir .makepyfile (
384
- """\
385
- def test_1():
386
- assert 0
387
- """
330
+ "def test_1(): assert 0" , test_something = "def test_2(): assert 0"
388
331
)
389
332
p2 = testdir .tmpdir .join ("test_something.py" )
390
- p2 .write (
391
- textwrap .dedent (
392
- """\
393
- def test_2():
394
- assert 0
395
- """
396
- )
397
- )
398
333
result = testdir .runpytest ()
399
334
result .stdout .fnmatch_lines (["*2 failed*" ])
400
335
result = testdir .runpytest ("--lf" , p2 )
@@ -436,18 +371,14 @@ def test_fail(val):
436
371
def test_terminal_report_lastfailed (self , testdir ):
437
372
test_a = testdir .makepyfile (
438
373
test_a = """
439
- def test_a1():
440
- pass
441
- def test_a2():
442
- pass
374
+ def test_a1(): pass
375
+ def test_a2(): pass
443
376
"""
444
377
)
445
378
test_b = testdir .makepyfile (
446
379
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
451
382
"""
452
383
)
453
384
result = testdir .runpytest ()
@@ -492,10 +423,8 @@ def test_b2():
492
423
def test_terminal_report_failedfirst (self , testdir ):
493
424
testdir .makepyfile (
494
425
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
499
428
"""
500
429
)
501
430
result = testdir .runpytest ()
@@ -542,7 +471,6 @@ def rlf(fail_import, fail_run):
542
471
assert list (lastfailed ) == ["test_maybe.py::test_hello" ]
543
472
544
473
def test_lastfailed_failure_subset (self , testdir , monkeypatch ):
545
-
546
474
testdir .makepyfile (
547
475
test_maybe = """
548
476
import os
@@ -560,6 +488,7 @@ def test_hello():
560
488
env = os.environ
561
489
if '1' == env['FAILIMPORT']:
562
490
raise ImportError('fail')
491
+
563
492
def test_hello():
564
493
assert '0' == env['FAILTEST']
565
494
@@ -613,8 +542,7 @@ def test_xfail_not_considered_failure(self, testdir):
613
542
"""
614
543
import pytest
615
544
@pytest.mark.xfail
616
- def test():
617
- assert 0
545
+ def test(): assert 0
618
546
"""
619
547
)
620
548
result = testdir .runpytest ()
@@ -626,8 +554,7 @@ def test_xfail_strict_considered_failure(self, testdir):
626
554
"""
627
555
import pytest
628
556
@pytest.mark.xfail(strict=True)
629
- def test():
630
- pass
557
+ def test(): pass
631
558
"""
632
559
)
633
560
result = testdir .runpytest ()
@@ -641,8 +568,7 @@ def test_failed_changed_to_xfail_or_skip(self, testdir, mark):
641
568
testdir .makepyfile (
642
569
"""
643
570
import pytest
644
- def test():
645
- assert 0
571
+ def test(): assert 0
646
572
"""
647
573
)
648
574
result = testdir .runpytest ()
@@ -655,8 +581,7 @@ def test():
655
581
"""
656
582
import pytest
657
583
@pytest.{mark}
658
- def test():
659
- assert 0
584
+ def test(): assert 0
660
585
""" .format (
661
586
mark = mark
662
587
)
@@ -694,18 +619,14 @@ def test_cache_cumulative(self, testdir):
694
619
# 1. initial run
695
620
test_bar = testdir .makepyfile (
696
621
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
701
624
"""
702
625
)
703
626
test_foo = testdir .makepyfile (
704
627
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
709
630
"""
710
631
)
711
632
testdir .runpytest ()
@@ -717,10 +638,8 @@ def test_foo_4():
717
638
# 2. fix test_bar_2, run only test_bar.py
718
639
testdir .makepyfile (
719
640
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
724
643
"""
725
644
)
726
645
result = testdir .runpytest (test_bar )
@@ -735,10 +654,8 @@ def test_bar_2():
735
654
# 3. fix test_foo_4, run only test_foo.py
736
655
test_foo = testdir .makepyfile (
737
656
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
742
659
"""
743
660
)
744
661
result = testdir .runpytest (test_foo , "--last-failed" )
@@ -752,10 +669,8 @@ def test_foo_4():
752
669
def test_lastfailed_no_failures_behavior_all_passed (self , testdir ):
753
670
testdir .makepyfile (
754
671
"""
755
- def test_1():
756
- assert True
757
- def test_2():
758
- assert True
672
+ def test_1(): pass
673
+ def test_2(): pass
759
674
"""
760
675
)
761
676
result = testdir .runpytest ()
@@ -777,10 +692,8 @@ def test_2():
777
692
def test_lastfailed_no_failures_behavior_empty_cache (self , testdir ):
778
693
testdir .makepyfile (
779
694
"""
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
784
697
"""
785
698
)
786
699
result = testdir .runpytest ("--lf" , "--cache-clear" )
@@ -1022,22 +935,12 @@ def check_readme(self, testdir):
1022
935
return readme .is_file ()
1023
936
1024
937
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" )
1031
939
testdir .runpytest ()
1032
940
assert self .check_readme (testdir ) is True
1033
941
1034
942
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" )
1041
944
testdir .runpytest ()
1042
945
assert self .check_readme (testdir ) is True
1043
946
0 commit comments