Home > Back-end >  Python unittest - ModuleNotFoundError
Python unittest - ModuleNotFoundError

Time:03-10

trying to get these unittest scripts to run, but on each of them I get the ModuleNotFoundError listed in the title. Specifically, all 3 return

'No module named 'pan_tilt_unit''

I am running this application in a Docker container.

The structure of my project is:

src/
    pan_tilt_unit/
        __init__.py
        device_handler/
            __init__.py
            other_files.py
        messages/
        mqtt/
    tests/
        test1.py
        test2.py
        test3.py
    run.py

And in a test.py file for example I have at the top:

from unittest import TestCase, main
from typing import List
from pan_tilt_unit.device_handler import *

And it fails on the third import statement. Does not matter if I run the file locally or inside the container itself, I always get this error.

Things I have tried:

  • Added a init.py file to the /tests folder

I am running an individual test file using the command

python3 -m unittest <filename>.py

CodePudding user response:

You need to run the unittest command from inside src/ like: python3 -m unittest ./tests/<filename>.py or add pan_tilt_unit to pythonpath when running tests from inside the tests folder. I'd go with the former.

  • Related