unittest.main() 到 运行 包中的所有测试模块
unittest.main() to run all test modules in a package
我在\tests 中保存了几个测试模块。然后我通过指定
在 运行 主程序之前将它们加载到主 class 中
from tests.ClassTests1 import *
from tests.ClassTests2 import *
...
unittest.main()
有什么方法可以指示 unittest.main() 运行 \tests 中的所有文件,而无需像上面那样无休止地重复导入它们?例如。我尝试了 from tests import *
但它不起作用。
编辑:我是在程序化发现之后,而不是通过命令行发现。
非常感谢!
来自 https://docs.python.org/2/library/unittest.html,第 25.3.3 节。测试发现:
Unittest supports simple test discovery. In order to be compatible with test discovery, all of the test files must be modules or packages importable from the top-level directory of the project (this means that their filenames must be valid identifiers).
Test discovery is implemented in TestLoader.discover(), but can also be used from the command line. The basic command-line usage is:
cd project_directory
python -m unittest discover
感谢 John Gordon 提供的链接。这就是它对我有用的原因:
from tests.ClassTests1 import *
# Run test cases first
suite = unittest.TestLoader().discover('tests', pattern='ClassTests*.py')
result = unittest.TextTestRunner(verbosity=2).run(suite)
但是,我仍然需要导入至少一个测试模块,而且我不确定我这样做是否正确,因为它会发现 tests
的整个测试层次结构。
我在\tests 中保存了几个测试模块。然后我通过指定
在 运行 主程序之前将它们加载到主 class 中from tests.ClassTests1 import *
from tests.ClassTests2 import *
...
unittest.main()
有什么方法可以指示 unittest.main() 运行 \tests 中的所有文件,而无需像上面那样无休止地重复导入它们?例如。我尝试了 from tests import *
但它不起作用。
编辑:我是在程序化发现之后,而不是通过命令行发现。
非常感谢!
来自 https://docs.python.org/2/library/unittest.html,第 25.3.3 节。测试发现:
Unittest supports simple test discovery. In order to be compatible with test discovery, all of the test files must be modules or packages importable from the top-level directory of the project (this means that their filenames must be valid identifiers).
Test discovery is implemented in TestLoader.discover(), but can also be used from the command line. The basic command-line usage is:
cd project_directory
python -m unittest discover
感谢 John Gordon 提供的链接。这就是它对我有用的原因:
from tests.ClassTests1 import *
# Run test cases first
suite = unittest.TestLoader().discover('tests', pattern='ClassTests*.py')
result = unittest.TextTestRunner(verbosity=2).run(suite)
但是,我仍然需要导入至少一个测试模块,而且我不确定我这样做是否正确,因为它会发现 tests
的整个测试层次结构。