Skip to content

Commit fdee88f

Browse files
committed
Merge pull request #1520 from omarkohl/invalid_test_module_name
Raise CollectError if import test module fails
2 parents 52babba + 5685589 commit fdee88f

File tree

7 files changed

+36
-10
lines changed

7 files changed

+36
-10
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
parametrize.
4444
Thanks `@palaviv`_ for the complete PR (`#1474`_).
4545

46-
*
46+
* Fix `#1426`_ Make ImportError during collection more explicit by reminding
47+
the user to check the name of the test module/package(s).
48+
Thanks `@omarkohl`_ for the complete PR (`#1520`_).
4749

4850
.. _@milliams: https://github.com/milliams
4951
.. _@novas0x2a: https://github.com/novas0x2a
@@ -53,6 +55,7 @@
5355
.. _@palaviv: https://github.com/palaviv
5456
.. _@omarkohl: https://github.com/omarkohl
5557

58+
.. _#1426: https://github.com/pytest-dev/pytest/issues/1426
5659
.. _#1428: https://github.com/pytest-dev/pytest/pull/1428
5760
.. _#1444: https://github.com/pytest-dev/pytest/pull/1444
5861
.. _#1441: https://github.com/pytest-dev/pytest/pull/1441
@@ -61,6 +64,7 @@
6164
.. _#1468: https://github.com/pytest-dev/pytest/pull/1468
6265
.. _#1474: https://github.com/pytest-dev/pytest/pull/1474
6366
.. _#1502: https://github.com/pytest-dev/pytest/pull/1502
67+
.. _#1520: https://github.com/pytest-dev/pytest/pull/1520
6468
.. _#372: https://github.com/pytest-dev/pytest/issues/372
6569

6670
2.9.2.dev1

_pytest/python.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,14 @@ def _importtestmodule(self):
636636
"unique basename for your test file modules"
637637
% e.args
638638
)
639+
except ImportError:
640+
exc_class, exc, _ = sys.exc_info()
641+
raise self.CollectError(
642+
"ImportError while importing test module '%s'.\n"
643+
"Original error message:\n'%s'\n"
644+
"Make sure your test modules/packages have valid Python names."
645+
% (self.fspath, exc or exc_class)
646+
)
639647
#print "imported test module", mod
640648
self.config.pluginmanager.consider_module(mod)
641649
return mod

testing/acceptance_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ def test_this():
117117
result = testdir.runpytest(p)
118118
result.stdout.fnmatch_lines([
119119
#XXX on jython this fails: "> import import_fails",
120-
"E ImportError: No module named *does_not_work*",
120+
"ImportError while importing test module*",
121+
"'No module named *does_not_work*",
121122
])
122123
assert result.ret == 1
123124

testing/python/collect.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
import _pytest._code
66
import py
77
import pytest
8-
from _pytest.main import EXIT_NOTESTSCOLLECTED
8+
from _pytest.main import (
9+
Collector,
10+
EXIT_NOTESTSCOLLECTED
11+
)
912

1013

1114
class TestModule:
1215
def test_failing_import(self, testdir):
1316
modcol = testdir.getmodulecol("import alksdjalskdjalkjals")
14-
pytest.raises(ImportError, modcol.collect)
15-
pytest.raises(ImportError, modcol.collect)
17+
pytest.raises(Collector.CollectError, modcol.collect)
1618

1719
def test_import_duplicate(self, testdir):
1820
a = testdir.mkdir("a")
@@ -60,6 +62,16 @@ def test_module_considers_pluginmanager_at_import(self, testdir):
6062
modcol = testdir.getmodulecol("pytest_plugins='xasdlkj',")
6163
pytest.raises(ImportError, lambda: modcol.obj)
6264

65+
def test_invalid_test_module_name(self, testdir):
66+
a = testdir.mkdir('a')
67+
a.ensure('test_one.part1.py')
68+
result = testdir.runpytest("-rw")
69+
result.stdout.fnmatch_lines([
70+
"ImportError while importing test module*test_one.part1*",
71+
"Make sure your test modules/packages have valid Python names.",
72+
])
73+
74+
6375
class TestClass:
6476
def test_class_with_init_warning(self, testdir):
6577
testdir.makepyfile("""

testing/test_collection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ def test_collection_error(self, testdir):
176176
assert "__import__" not in result.stdout.str(), "too long traceback"
177177
result.stdout.fnmatch_lines([
178178
"*ERROR collecting*",
179-
"*mport*not_exists*"
179+
"ImportError while importing test module*",
180+
"'No module named *not_exists*",
180181
])
181182

182183
def test_custom_repr_failure(self, testdir):

testing/test_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_this():
4242
reprec = testdir.inline_run(tfile)
4343
l = reprec.getfailedcollections()
4444
assert len(l) == 1
45-
out = l[0].longrepr.reprcrash.message
45+
out = str(l[0].longrepr)
4646
assert out.find('does_not_work') != -1
4747

4848
def test_raises_output(self, testdir):

testing/test_terminal.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ def test_collectonly_error(self, testdir):
276276
assert result.ret == 1
277277
result.stdout.fnmatch_lines(_pytest._code.Source("""
278278
*ERROR*
279-
*import Errlk*
280279
*ImportError*
280+
*No module named *Errlk*
281281
*1 error*
282282
""").strip())
283283

@@ -665,8 +665,8 @@ def test_collect_fail(self, testdir, option):
665665
testdir.makepyfile("import xyz\n")
666666
result = testdir.runpytest(*option.args)
667667
result.stdout.fnmatch_lines([
668-
"? import xyz",
669-
"E ImportError: No module named *xyz*",
668+
"ImportError while importing*",
669+
"'No module named *xyz*",
670670
"*1 error*",
671671
])
672672

0 commit comments

Comments
 (0)