Python Unit test module throws "ModuleNotFoundError: No module named 'tests.test_file'"
Question:
I’m trying to execute a test case for a project I’ve been working on. I used to successfully execute the unit tests earlier but it errors out now. I know for sure that there have been no updates to any libraries or change in the Path. I tried to look at the source code and figure out why it’s erroring out but no luck yet. Any help on this would be appreciated.
Python version – 3.7.1
Sample Code below
import unittest
class MyTestCase(unittest.TestCase):
def test_dummy(self):
self.assertEqual(2+2,4)
I used the following command in cmd to execute the test.
C:UsersYadadaDesktoprepomwemwe>python -m unittest teststest_file.py
My folder structure is
MWE -|
|_tests - |
|_test_file.py
The expected output is the test being executing successfully because it’s a straightforward one. But I end up getting the following error
strclass
ERROR: test_file (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_file
Traceback (most recent call last):
File "C:UsersyadadaAppDataLocalContinuumanaconda3libunittestloader.py", line 156, in loadTestsFromName
module = __import__(module_name)
ModuleNotFoundError: No module named 'tests.test_file'
----------------------------------------------------------------------
Ran 1 test in 0.001s
Answers:
The issue got resolved after placing an empty __init__.py
file in the tests folder.
For a better explanation about why it worked, refer to What is __init__.py for?
Thanks, @aws_apprentice for the help.
With PyCharm 2020.2 the "ModuleNotFoundError: No module named" error can also happen when running a unit test in a sub folder which imports a module from a parent folder under test. PyCharm is taking by default the script path for executing the test which fails e.g. with this error:
ModuleNotFoundError: No module named '/home/foobar/projects/foo/mypythonproject/moduleundertest'
The solution is to edit the run configuration of the unit test and change the Unittests Target from "Script path" to "Module name".
The solution for me was to add:
if __name__ == '__main__':
unittest.main()
to the bottom of my test file.
I was then able to run:
python -m <test-file>
Removing the .py
suffix from my python -m
command was also required:
See this answer for details.
The reason is some library is using the package name already, see:
this stack overflow question about reserved package names
To understand the poblem: rename the tests
package to tests_package
package. (test
is also a reserved package name)
Before:
MWE -|
|- tests -|
|- test_file.py
After:
MWE -|
|- tests_package -|
|- test_file.py
(Since python 3.3 the __init__.py
is not required.)
Then running python -m unittest tests_packagetest_file.py
Using a "venv" would solve it for tests
package name but not for test
Probably an even more pythonic solution would be to not have your project run in "root" by giving it a unique module name, releasing you from the requirement to have a package name unused by python libraries for the tests
directory.
example:
GitFolderOfProject -|
|- MWE -|
|- tests -|
|- test_file.py
Then running the tests from the "GitFolderOfProject" as python -m unittest MWEteststest_file.py
This is a simple setup magic.
First things first – in PyCharm:
-
Right-click on folder (in the project tree) with tests – mark this folder as "tests folder".
-
Right-click on folder (in the project tree) with sources – mark this folder as "sources folder".
-
Remove bad "Run configuration" (or don’t remove, you choose).
-
Right click on on folder with tests (already marked as the "tests folder") – choose "run".
This is all. Really simple. Logical. It took me about an hour to find it out.
I’m trying to execute a test case for a project I’ve been working on. I used to successfully execute the unit tests earlier but it errors out now. I know for sure that there have been no updates to any libraries or change in the Path. I tried to look at the source code and figure out why it’s erroring out but no luck yet. Any help on this would be appreciated.
Python version – 3.7.1
Sample Code below
import unittest
class MyTestCase(unittest.TestCase):
def test_dummy(self):
self.assertEqual(2+2,4)
I used the following command in cmd to execute the test.
C:UsersYadadaDesktoprepomwemwe>python -m unittest teststest_file.py
My folder structure is
MWE -|
|_tests - |
|_test_file.py
The expected output is the test being executing successfully because it’s a straightforward one. But I end up getting the following error
strclass
ERROR: test_file (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_file
Traceback (most recent call last):
File "C:UsersyadadaAppDataLocalContinuumanaconda3libunittestloader.py", line 156, in loadTestsFromName
module = __import__(module_name)
ModuleNotFoundError: No module named 'tests.test_file'
----------------------------------------------------------------------
Ran 1 test in 0.001s
The issue got resolved after placing an empty __init__.py
file in the tests folder.
For a better explanation about why it worked, refer to What is __init__.py for?
Thanks, @aws_apprentice for the help.
With PyCharm 2020.2 the "ModuleNotFoundError: No module named" error can also happen when running a unit test in a sub folder which imports a module from a parent folder under test. PyCharm is taking by default the script path for executing the test which fails e.g. with this error:
ModuleNotFoundError: No module named '/home/foobar/projects/foo/mypythonproject/moduleundertest'
The solution is to edit the run configuration of the unit test and change the Unittests Target from "Script path" to "Module name".
The solution for me was to add:
if __name__ == '__main__':
unittest.main()
to the bottom of my test file.
I was then able to run:
python -m <test-file>
Removing the .py
suffix from my python -m
command was also required:
See this answer for details.
The reason is some library is using the package name already, see:
this stack overflow question about reserved package names
To understand the poblem: rename the tests
package to tests_package
package. (test
is also a reserved package name)
Before:
MWE -|
|- tests -|
|- test_file.py
After:
MWE -|
|- tests_package -|
|- test_file.py
(Since python 3.3 the __init__.py
is not required.)
Then running python -m unittest tests_packagetest_file.py
Using a "venv" would solve it for tests
package name but not for test
Probably an even more pythonic solution would be to not have your project run in "root" by giving it a unique module name, releasing you from the requirement to have a package name unused by python libraries for the tests
directory.
example:
GitFolderOfProject -|
|- MWE -|
|- tests -|
|- test_file.py
Then running the tests from the "GitFolderOfProject" as python -m unittest MWEteststest_file.py
This is a simple setup magic.
First things first – in PyCharm:
-
Right-click on folder (in the project tree) with tests – mark this folder as "tests folder".
-
Right-click on folder (in the project tree) with sources – mark this folder as "sources folder".
-
Remove bad "Run configuration" (or don’t remove, you choose).
-
Right click on on folder with tests (already marked as the "tests folder") – choose "run".
This is all. Really simple. Logical. It took me about an hour to find it out.