45

使用单元测试构建 Python 包的最常见方法如下:

package/
    __init__.py
    module_1.py
    module_2.py
    module_n.py
    test/
        __init__.py
        test_module_1.py
        test_module_2.py
        test_module_n.py

我想区分单元测试(方法和函数)和集成测试(使用整个包并可能涉及其他资源)。也许这些测试应该在不同的包中,有不同的文件名,和/或包含某些文档字符串注释。

是否有这样做的标准约定?

4

2 回答 2

26

在我们的项目中,我们在每个包中都有单元测试,与您的情况相同,集成测试,系统测试,作为顶层的单独包,即:

package_1/
  __init__.py
  module_1.py
  module_n.py
  test/
    __init__.py
    test_module_1.py
    test_module_n.py
package_n/
  __init__.py
  module_1.py
  module_n.py
  test/
    __init__.py
    test_module_1.py
    test_module_n.py
systemtest/
  __init__.py
  systemtest_1.py
  systemtest_n.py

即使您在项目中只有一个包,我也会使用此约定。但是我不确定这是否是标准约定。

于 2013-04-08T14:22:50.800 回答
3

我只是为自己研究了这个,发现这个建议很有帮助:

project/
│
├── my_app/
│   └── __init__.py
│
└── tests/
    |
    └── unit/
    |   ├── __init__.py
    |   └── test_sum.py
    |
    └── integration/
        |
        ├── example_data/
        |   ├── test_basic.json
        |   └── test_complex.json
        |
        ├── __init__.py
        └── test_integration.py
于 2019-02-18T02:10:49.607 回答