Merge pull request #1520 from omarkohl/invalid_test_module_name
Raise CollectError if import test module fails
This commit is contained in:
		
						commit
						fdee88f086
					
				|  | @ -43,7 +43,9 @@ | ||||||
|   parametrize. |   parametrize. | ||||||
|   Thanks `@palaviv`_ for the complete PR (`#1474`_). |   Thanks `@palaviv`_ for the complete PR (`#1474`_). | ||||||
| 
 | 
 | ||||||
| * | * Fix `#1426`_ Make ImportError during collection more explicit by reminding | ||||||
|  |   the user to check the name of the test module/package(s). | ||||||
|  |   Thanks `@omarkohl`_ for the complete PR (`#1520`_). | ||||||
| 
 | 
 | ||||||
| .. _@milliams: https://github.com/milliams | .. _@milliams: https://github.com/milliams | ||||||
| .. _@novas0x2a: https://github.com/novas0x2a | .. _@novas0x2a: https://github.com/novas0x2a | ||||||
|  | @ -53,6 +55,7 @@ | ||||||
| .. _@palaviv: https://github.com/palaviv | .. _@palaviv: https://github.com/palaviv | ||||||
| .. _@omarkohl: https://github.com/omarkohl | .. _@omarkohl: https://github.com/omarkohl | ||||||
| 
 | 
 | ||||||
|  | .. _#1426: https://github.com/pytest-dev/pytest/issues/1426 | ||||||
| .. _#1428: https://github.com/pytest-dev/pytest/pull/1428 | .. _#1428: https://github.com/pytest-dev/pytest/pull/1428 | ||||||
| .. _#1444: https://github.com/pytest-dev/pytest/pull/1444 | .. _#1444: https://github.com/pytest-dev/pytest/pull/1444 | ||||||
| .. _#1441: https://github.com/pytest-dev/pytest/pull/1441 | .. _#1441: https://github.com/pytest-dev/pytest/pull/1441 | ||||||
|  | @ -61,6 +64,7 @@ | ||||||
| .. _#1468: https://github.com/pytest-dev/pytest/pull/1468 | .. _#1468: https://github.com/pytest-dev/pytest/pull/1468 | ||||||
| .. _#1474: https://github.com/pytest-dev/pytest/pull/1474 | .. _#1474: https://github.com/pytest-dev/pytest/pull/1474 | ||||||
| .. _#1502: https://github.com/pytest-dev/pytest/pull/1502 | .. _#1502: https://github.com/pytest-dev/pytest/pull/1502 | ||||||
|  | .. _#1520: https://github.com/pytest-dev/pytest/pull/1520 | ||||||
| .. _#372: https://github.com/pytest-dev/pytest/issues/372 | .. _#372: https://github.com/pytest-dev/pytest/issues/372 | ||||||
| 
 | 
 | ||||||
| 2.9.2.dev1 | 2.9.2.dev1 | ||||||
|  |  | ||||||
|  | @ -636,6 +636,14 @@ class Module(pytest.File, PyCollector): | ||||||
|                 "unique basename for your test file modules" |                 "unique basename for your test file modules" | ||||||
|                  % e.args |                  % e.args | ||||||
|             ) |             ) | ||||||
|  |         except ImportError: | ||||||
|  |             exc_class, exc, _ = sys.exc_info() | ||||||
|  |             raise self.CollectError( | ||||||
|  |                 "ImportError while importing test module '%s'.\n" | ||||||
|  |                 "Original error message:\n'%s'\n" | ||||||
|  |                 "Make sure your test modules/packages have valid Python names." | ||||||
|  |                 % (self.fspath, exc or exc_class) | ||||||
|  |             ) | ||||||
|         #print "imported test module", mod |         #print "imported test module", mod | ||||||
|         self.config.pluginmanager.consider_module(mod) |         self.config.pluginmanager.consider_module(mod) | ||||||
|         return mod |         return mod | ||||||
|  |  | ||||||
|  | @ -117,7 +117,8 @@ class TestGeneralUsage: | ||||||
|         result = testdir.runpytest(p) |         result = testdir.runpytest(p) | ||||||
|         result.stdout.fnmatch_lines([ |         result.stdout.fnmatch_lines([ | ||||||
|             #XXX on jython this fails:  ">   import import_fails", |             #XXX on jython this fails:  ">   import import_fails", | ||||||
|             "E   ImportError: No module named *does_not_work*", |             "ImportError while importing test module*", | ||||||
|  |             "'No module named *does_not_work*", | ||||||
|         ]) |         ]) | ||||||
|         assert result.ret == 1 |         assert result.ret == 1 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -5,14 +5,16 @@ from textwrap import dedent | ||||||
| import _pytest._code | import _pytest._code | ||||||
| import py | import py | ||||||
| import pytest | import pytest | ||||||
| from _pytest.main import EXIT_NOTESTSCOLLECTED | from _pytest.main import ( | ||||||
|  |     Collector, | ||||||
|  |     EXIT_NOTESTSCOLLECTED | ||||||
|  | ) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class TestModule: | class TestModule: | ||||||
|     def test_failing_import(self, testdir): |     def test_failing_import(self, testdir): | ||||||
|         modcol = testdir.getmodulecol("import alksdjalskdjalkjals") |         modcol = testdir.getmodulecol("import alksdjalskdjalkjals") | ||||||
|         pytest.raises(ImportError, modcol.collect) |         pytest.raises(Collector.CollectError, modcol.collect) | ||||||
|         pytest.raises(ImportError, modcol.collect) |  | ||||||
| 
 | 
 | ||||||
|     def test_import_duplicate(self, testdir): |     def test_import_duplicate(self, testdir): | ||||||
|         a = testdir.mkdir("a") |         a = testdir.mkdir("a") | ||||||
|  | @ -60,6 +62,16 @@ class TestModule: | ||||||
|         modcol = testdir.getmodulecol("pytest_plugins='xasdlkj',") |         modcol = testdir.getmodulecol("pytest_plugins='xasdlkj',") | ||||||
|         pytest.raises(ImportError, lambda: modcol.obj) |         pytest.raises(ImportError, lambda: modcol.obj) | ||||||
| 
 | 
 | ||||||
|  |     def test_invalid_test_module_name(self, testdir): | ||||||
|  |         a = testdir.mkdir('a') | ||||||
|  |         a.ensure('test_one.part1.py') | ||||||
|  |         result = testdir.runpytest("-rw") | ||||||
|  |         result.stdout.fnmatch_lines([ | ||||||
|  |             "ImportError while importing test module*test_one.part1*", | ||||||
|  |             "Make sure your test modules/packages have valid Python names.", | ||||||
|  |         ]) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| class TestClass: | class TestClass: | ||||||
|     def test_class_with_init_warning(self, testdir): |     def test_class_with_init_warning(self, testdir): | ||||||
|         testdir.makepyfile(""" |         testdir.makepyfile(""" | ||||||
|  |  | ||||||
|  | @ -176,7 +176,8 @@ class TestPrunetraceback: | ||||||
|         assert "__import__" not in result.stdout.str(), "too long traceback" |         assert "__import__" not in result.stdout.str(), "too long traceback" | ||||||
|         result.stdout.fnmatch_lines([ |         result.stdout.fnmatch_lines([ | ||||||
|             "*ERROR collecting*", |             "*ERROR collecting*", | ||||||
|             "*mport*not_exists*" |             "ImportError while importing test module*", | ||||||
|  |             "'No module named *not_exists*", | ||||||
|         ]) |         ]) | ||||||
| 
 | 
 | ||||||
|     def test_custom_repr_failure(self, testdir): |     def test_custom_repr_failure(self, testdir): | ||||||
|  |  | ||||||
|  | @ -42,7 +42,7 @@ class SessionTests: | ||||||
|         reprec = testdir.inline_run(tfile) |         reprec = testdir.inline_run(tfile) | ||||||
|         l = reprec.getfailedcollections() |         l = reprec.getfailedcollections() | ||||||
|         assert len(l) == 1 |         assert len(l) == 1 | ||||||
|         out = l[0].longrepr.reprcrash.message |         out = str(l[0].longrepr) | ||||||
|         assert out.find('does_not_work') != -1 |         assert out.find('does_not_work') != -1 | ||||||
| 
 | 
 | ||||||
|     def test_raises_output(self, testdir): |     def test_raises_output(self, testdir): | ||||||
|  |  | ||||||
|  | @ -276,8 +276,8 @@ class TestCollectonly: | ||||||
|         assert result.ret == 1 |         assert result.ret == 1 | ||||||
|         result.stdout.fnmatch_lines(_pytest._code.Source(""" |         result.stdout.fnmatch_lines(_pytest._code.Source(""" | ||||||
|             *ERROR* |             *ERROR* | ||||||
|             *import Errlk* |  | ||||||
|             *ImportError* |             *ImportError* | ||||||
|  |             *No module named *Errlk* | ||||||
|             *1 error* |             *1 error* | ||||||
|         """).strip()) |         """).strip()) | ||||||
| 
 | 
 | ||||||
|  | @ -665,8 +665,8 @@ class TestGenericReporting: | ||||||
|         testdir.makepyfile("import xyz\n") |         testdir.makepyfile("import xyz\n") | ||||||
|         result = testdir.runpytest(*option.args) |         result = testdir.runpytest(*option.args) | ||||||
|         result.stdout.fnmatch_lines([ |         result.stdout.fnmatch_lines([ | ||||||
|             "?   import xyz", |             "ImportError while importing*", | ||||||
|             "E   ImportError: No module named *xyz*", |             "'No module named *xyz*", | ||||||
|             "*1 error*", |             "*1 error*", | ||||||
|         ]) |         ]) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue