python - 如何以一种可以在单个命令中运行所有测试的方式组织 python 测试?

标签 python unit-testing python-nose

目前我的代码是按以下树结构组织的:

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

使用以下命令,我可以运行包含在单个文件中的测试,例如:

$ 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

谢谢

最佳答案

将测试和模块分开或混合可能是个人喜好问题,尽管我强烈主张将它们分开(设置原因、代码统计信息等)。

当你使用 nosetests 时,确保所有包含测试的目录都是真实的包:

src/
    module1.py
    module2.py
    subpackage1/
        __init__.py
        moduleA.py
        moduleB.py
tests/
    __init__.py
    test_module1.py
    test_module2.py
    subpackage1/
        __init__.py
        test_moduleA.py
        test_moduleB.py

这样,您只需在顶层目录中运行 nosetests 即可找到所有测试。但是,您需要确保 src/PYTHONPATH 上,否则所有测试都将因缺少导入而失败。

关于python - 如何以一种可以在单个命令中运行所有测试的方式组织 python 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/366720/

相关文章:

python - HTTPForbidden 上下文路由未触发

python - 统一码编码错误 : 'gbk' codec can't encode character: illegal multibyte sequence

java - 如何使用 Java split 获得与 Python split 相同的结果

python - 如果通过 strcpy() 获取用户输入,用户输入是否会被复制到超出空格或空字符的堆栈上

android - RxJava 和改造的单元测试

android - Jacoco 无法读取 .ec 文件

unit-testing - 我如何模拟grails domain.refresh()

nose - 使用nose.main运行doctest插件

python - 用 Nose 测试 python 多处理池代码