简体   繁体   English

Python unittest在运行时发现

[英]Python unittest discover at runtime

I'm trying to run my unit tests for a python module from a separate test script. 我正在尝试从单独的测试脚本运行python模块的单元测试。 Here is my file structure 这是我的文件结构

- root
  |- modules
     |- a_module.py
  |- test
     |- test_a_module.py
  |- main.py

The main.py looks like this: main.py看起来像这样:

import unittest

loader = unittest.TestLoader()
suite = loader.discover(start_dir='./test', pattern='test_*.py')

runner = unittest.TextTestRunner()
runner.run(suite)

And here are a_module.py and test_a_module.py : 这是a_module.pytest_a_module.py

# a_module.py
def something():
    return True

# test_a_module.py
import unittest

from ..modules.a_module import something

class TestSomething(unittest.TestCase):
    def test_something(self):
        self.assertTrue(something)

When running python3 main.py I get the following error. 运行python3 main.py出现以下错误。

======================================================================
ERROR: test_a_module (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_a_module
Traceback (most recent call last):
  File "/usr/lib/python3.7/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "/usr/lib/python3.7/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/home/schu_max/root/test/test_a_module.py", line 3, in <module>
    from ..modules.a_module import something
ImportError: attempted relative import with no known parent package


----------------------------------------------------------------------

I'm relatively new to python and don't have a clue on how to solve this issue. 我对python比较陌生,并且不知道如何解决这个问题。 And other questions/answers on SO weren't that helpful either. 关于SO的其他问题/答案也没有那么有用。 So how can I get this to work? 那我怎么能让它运作起来呢?

There are several ways to make your tests run. 有几种方法可以让您的测试运行。 Now that they are import packages try 现在他们是导入包试试

python -m unittest -v in the root package folder, after removing the two dots from the relative import path in test/test_a_module.py . test/test_a_module.py的相对导入路径中删除两个点后,根包文件夹中的python -m unittest -v Will work too with python main.py from the same folder. 也可以使用同一文件夹中的python main.py If you issue python -m unittest -v outside root and you prefix the relative path with root it will work too. 如果在root之外发出python -m unittest -v并在相对路径前加上root那么它也会起作用。 There are probably other ways. 可能还有其他方法。

Just beaware that the relative import paths in the test files are affected by the folder from where you run your test script. 请注意,测试文件中的相对导入路径会受到运行测试脚本的文件夹的影响。

To stay coherent, run your tests always from the same place, and put all your tests inside test/ where you can refer all you root subpackages by relative path, without the need to mention the `root* package name, which is nice because you might want to rename it in the future. 为了保持连贯,始终从同一个地方运行测试,并将所有测试放在test/ ,你可以通过相对路径引用所有根子包,而不需要提到`root *包名,这很好,因为你可能想在将来重命名它。

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

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