@@ -642,3 +642,114 @@ def test___repr__():
642642 """ )
643643 reprec = testdir .inline_run ("-k repr" )
644644 reprec .assertoutcome (passed = 1 , failed = 0 )
645+
646+
647+ COLLECTION_ERROR_PY_FILES = dict (
648+ test_01_failure = """
649+ def test_1():
650+ assert False
651+ """ ,
652+ test_02_import_error = """
653+ import asdfasdfasdf
654+ def test_2():
655+ assert True
656+ """ ,
657+ test_03_import_error = """
658+ import asdfasdfasdf
659+ def test_3():
660+ assert True
661+ """ ,
662+ test_04_success = """
663+ def test_4():
664+ assert True
665+ """ ,
666+ )
667+
668+ def test_exit_on_collection_error (testdir ):
669+ """Verify that all collection errors are collected and no tests executed"""
670+ testdir .makepyfile (** COLLECTION_ERROR_PY_FILES )
671+
672+ res = testdir .runpytest ()
673+ assert res .ret == 2
674+
675+ res .stdout .fnmatch_lines ([
676+ "collected 2 items / 2 errors" ,
677+ "*ERROR collecting test_02_import_error.py*" ,
678+ "*No module named 'asdfa*" ,
679+ "*ERROR collecting test_03_import_error.py*" ,
680+ "*No module named 'asdfa*" ,
681+ ])
682+
683+
684+ def test_exit_on_collection_with_maxfail_smaller_than_n_errors (testdir ):
685+ """
686+ Verify collection is aborted once maxfail errors are encountered ignoring
687+ further modules which would cause more collection errors.
688+ """
689+ testdir .makepyfile (** COLLECTION_ERROR_PY_FILES )
690+
691+ res = testdir .runpytest ("--maxfail=1" )
692+ assert res .ret == 2
693+
694+ res .stdout .fnmatch_lines ([
695+ "*ERROR collecting test_02_import_error.py*" ,
696+ "*No module named 'asdfa*" ,
697+ "*Interrupted: stopping after 1 failures*" ,
698+ ])
699+
700+ assert 'test_03' not in res .stdout .str ()
701+
702+
703+ def test_exit_on_collection_with_maxfail_bigger_than_n_errors (testdir ):
704+ """
705+ Verify the test run aborts due to collection errors even if maxfail count of
706+ errors was not reached.
707+ """
708+ testdir .makepyfile (** COLLECTION_ERROR_PY_FILES )
709+
710+ res = testdir .runpytest ("--maxfail=4" )
711+ assert res .ret == 2
712+
713+ res .stdout .fnmatch_lines ([
714+ "collected 2 items / 2 errors" ,
715+ "*ERROR collecting test_02_import_error.py*" ,
716+ "*No module named 'asdfa*" ,
717+ "*ERROR collecting test_03_import_error.py*" ,
718+ "*No module named 'asdfa*" ,
719+ ])
720+
721+
722+ def test_continue_on_collection_errors (testdir ):
723+ """
724+ Verify tests are executed even when collection errors occur when the
725+ --continue-on-collection-errors flag is set
726+ """
727+ testdir .makepyfile (** COLLECTION_ERROR_PY_FILES )
728+
729+ res = testdir .runpytest ("--continue-on-collection-errors" )
730+ assert res .ret == 1
731+
732+ res .stdout .fnmatch_lines ([
733+ "collected 2 items / 2 errors" ,
734+ "*1 failed, 1 passed, 2 error*" ,
735+ ])
736+
737+
738+ def test_continue_on_collection_errors_maxfail (testdir ):
739+ """
740+ Verify tests are executed even when collection errors occur and that maxfail
741+ is honoured (including the collection error count).
742+ 4 tests: 2 collection errors + 1 failure + 1 success
743+ test_4 is never executed because the test run is with --maxfail=3 which
744+ means it is interrupted after the 2 collection errors + 1 failure.
745+ """
746+ testdir .makepyfile (** COLLECTION_ERROR_PY_FILES )
747+
748+ res = testdir .runpytest ("--continue-on-collection-errors" , "--maxfail=3" )
749+ assert res .ret == 2
750+
751+ res .stdout .fnmatch_lines ([
752+ "collected 2 items / 2 errors" ,
753+ "*Interrupted: stopping after 3 failures*" ,
754+ "*1 failed, 2 error*" ,
755+ ])
0 commit comments