29

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:\Users\Yadada\Desktop\repo\mwe\mwe>python -m unittest tests\test_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:\Users\yadada\AppData\Local\Continuum\anaconda3\lib\unittest\loader.py", line 156, in loadTestsFromName
    module = __import__(module_name)
ModuleNotFoundError: No module named 'tests.test_file'


----------------------------------------------------------------------
Ran 1 test in 0.001s
Bonifacio2
  • 2,978
  • 4
  • 33
  • 48
Bee
  • 754
  • 1
  • 6
  • 16
  • do you have an `__init__.py` file in the `tests` directory to make them discoverable as part of the module? – gold_cy Jun 26 '19 at 21:12
  • Nope I don't, I never had the `__init.py__` file earlier in the same `tests` directory but the tests worked fine. – Bee Jun 26 '19 at 21:20
  • 1
    @aws_apprentice - I put an empty `__init__.py` file in the tests folder and surprisingly the unit test works now. I wonder why? Can you give a brief explanation about this behavior if you could? If you post it as an answer, I will accept it. Thanks – Bee Jun 26 '19 at 21:25
  • https://stackoverflow.com/questions/448271/what-is-init-py-for will explain better than I can – gold_cy Jun 26 '19 at 21:30
  • 1
    I am having this error too, \_\_init\_\_.py is **not needed** in Python 3.3+ unless you need to run an installation script in that directory – boardtc Jan 29 '21 at 18:08

3 Answers3

34

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.

Bee
  • 754
  • 1
  • 6
  • 16
10

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".

k_o_
  • 4,014
  • 1
  • 27
  • 39
0

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.

tjheslin1
  • 1,196
  • 6
  • 16
  • 34