简体   繁体   中英

Python : ImportError in my unittests

This is my project structure 在此输入图像描述

Some I've written some unit tests in tommy/core/tests/test_tprotocol.py , and in this file, I've this line :

from tommy.core.tprotocol import TRequest

But when I run unittest in the tests folder :

 python -m  unittest tprotocol

I've an ImportError :

E
======================================================================
ERROR: tprotocol (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: tprotocol
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 153, in loadTestsFromName
    module = __import__(module_name)
ModuleNotFoundError: No module named 'tprotocol'


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

I always have this type of errors when I import a module from a subfolder... What is the best way for fix that ?

But everything works if I do python -m unittest tommy/core/tests/test_tprotocol.py

The error is not related to the line you have shown.

You are currently in the test folder so there's no way you could possibly run tprotocol from the terminal from that directory. And you should be using the test module not the original one:

python -m  unittest test_tprotocol

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM