简体   繁体   English

运行所有使用..import导入的unittest.TestCase *

[英]Running all unittest.TestCase imported using from .. import *

I have the following package: 我有以下软件包:

  • tests.py
  • tests 测试
    • __init__.py
    • test_module_a.py
    • test_module_b.py

In my tests.py file I do the following: 在我的tests.py文件中,执行以下操作:

import unittest

from tests import *

if __name__ == "__main__":
    unittest.main()

In my tests/__init__.py the following: 在我的tests/__init__.py ,以下内容:

__all__ = ["test_module_a", "test_module_b"]

In my tests/test_module_a.py and tests/test_module_b.py files, I have the following: 在我的tests/test_module_a.pytests/test_module_b.py文件中,我具有以下内容:

import unittest

class TestMyModule(unittest.TestCase):

  def test_something(self):
    self.assertTrue(True)

When I run python tests.py , the submodules seem to be imported but my unittest.TestCase 's are not run. 当我运行python tests.py ,似乎导入了子模块,但是我的unittest.TestCase却没有运行。 Why? 为什么? Thank. 谢谢。

Use a test loader and import explicitly every test case (to be more readable) : 使用测试加载器并显式导入每个测试用例(以提高可读性):

import unittest

from test_module_a import TestMyModule1
from test_module_b import TestMyModule2

if __name__ == "__main__":
    loader = unittest.TestLoader()
    suite = unittest.TestSuite((loader.loadTestsFromTestCase(TestMyModule1),
                                loader.loadTestsFromTestCase(TestMyModule2),
                                )
                               )
    runner = unittest.TextTestRunner(verbosity=2)
    runner.run(suite)

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

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