简体   繁体   English

Python:我的单元测试中的ImportError

[英]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 : 有些我在tommy / core / tests / test_tprotocol.py中编写了一些单元测试,在这个文件中,我有这一行:

from tommy.core.tprotocol import TRequest

But when I run unittest in the tests folder : 但是当我在tests文件夹中运行unittest时:

 python -m  unittest tprotocol

I've an ImportError : 我有一个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 但是如果我做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. 您当前位于测试文件夹中,因此您无法从该目录中的终端运行tprotocol And you should be using the test module not the original one: 你应该使用测试模块而不是原始测试模块:

python -m  unittest test_tprotocol

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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