当前,我的代码按以下树结构组织:
src/
module1.py
module2.py
test_module1.py
test_module2.py
subpackage1/
__init__.py
moduleA.py
moduleB.py
test_moduleA.py
test_moduleB.py
其中,module*.py
文件包含源代码,test_module*.py
包含相关模块的TestCase
。
使用以下comands,我可以运行单个文件中包含的测试,例如:
$ cd src
$ nosetests test_filesystem.py
..................
----------------------------------------------------------------------
Ran 18 tests in 0.390s
OK
如何运行所有测试?我试过用nosetests -m 'test_.*'
但没用。
$cd src
$ nosetests -m 'test_.*'
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
谢谢
如果它们都以
test
开头,那么nosetest
就应该起作用。Nose自动搜索以“test”开头的任何文件。我不知道nosetest,但是你可以用标准的unittest模块来实现。您只需要在根目录下创建一个
test_all.py
文件,然后导入所有测试模块。就你而言:每个模块都应该提供以下函数(例如一个具有两个单元测试的模块:
Class1
和Class2
):无论您是分离还是混合测试和模块可能是一个品味的问题,尽管我强烈建议将它们分开(设置原因、代码统计等)。
使用nosetests时,请确保所有包含测试的目录都是真实的包:
这样,您就可以在顶层目录中运行
nosetests
,所有测试都将被找到。但是,您需要确保src/
位于PYTHONPATH
上,否则所有测试都将由于缺少导入而失败。相关问题 更多 >
编程相关推荐