简体   繁体   English

打包python模块时我应该把测试放在哪里?

[英]Where should I put tests when packaging python modules?

I have a module that sits in a namespace.我有一个位于命名空间中的模块。 Should tests and data the tests rely on go in the namespace or in the top level where setup.py sites?测试所依赖的测试和数据应该放在命名空间还是 setup.py 站点的顶层?

./company/__init__.py
./company/namespace/__init__.py
./company/namespace/useful.py
./company/namespace/test_useful.py
./company/namespace/test_data/useful_data.xml
./setup.py

or或者

./company/__init__.py
./company/namespace/__init__.py
./company/namespace/useful.py
./test_useful.py
./test_data/useful_data.xml
./setup.py

Does the question amount to whether tests should be installed or not?问题是否等于是否应该安装测试?

The Sample Project stores the tests outside the module.示例项目将测试存储在模块之外。

The directory structure looks like this:目录结构如下所示:

├── data
│   └── data_file
├── MANIFEST.in
├── README.rst
├── sample
│   ├── __init__.py
│   └── package_data.dat
├── setup.cfg
├── setup.py
└── tests
    ├── __init__.py
    └── test_simple.py

Related: The Packing Guide: https://packaging.python.org/en/latest/相关:包装指南: https : //packaging.python.org/en/latest/

Hint: Don't follow the "The Hitchhiker's Guide to Packaging".提示:不要遵循“The Hitchhiker's Guide to Packaging”。 It has not been updated since 2010!自2010年以来一直没有更新!

(do not confuse both pages. The "The Hitchhiker's Guide to Python" is a very solid book) (不要混淆两页。“The Hitchhiker's Guide to Python”是一本非常扎实的书)

You should put your test module inside the module it tests according to The Hitchhiker's Guide to Packaging .您应该根据The Hitchhiker's Guide to Packaging将测试模块放在它测试的模块中。

Here is their example:这是他们的例子:

TowelStuff/
    bin/
    CHANGES.txt
    docs/
    LICENSE.txt
    MANIFEST.in
    README.txt
    setup.py
    towelstuff/
        __init__.py
        location.py
        utils.py
        test/
            __init__.py
            test_location.py
            test_utils.py

This way your module will be distributed with its tests and users can use them to verify that it works with their set up.通过这种方式,您的模块将与其测试一起分发,用户可以使用它们来验证它是否适用于他们的设置。

See http://the-hitchhikers-guide-to-packaging.readthedocs.org/en/latest/creation.html .请参阅http://the-hitchhikers-guide-to-packaging.readthedocs.org/en/latest/creation.html

I personally create a single tests package as a sub package of the main package for a few reasons:我个人创建了一个单独的tests包作为主包的子包,原因如下:

  • If tests is in parallel with the root package there's an off chance you, or a user may misconfigure setup.py and accidentally expose a global package named tests that will cause a great deal of confusion and headache until you realize what has happened.如果tests与根包并行,则您或用户可能会错误配置setup.py并意外暴露名为tests的全局包,这将导致大量混乱和头痛,直到您意识到发生了什么。 Putting it in the main module solves this as it's now under a (hopefully) globally unique namespace.将它放在主模块中可以解决这个问题,因为它现在位于(希望)全局唯一的命名空间下。

  • I don't like putting a test module within user package because test runners have to search through production code.我不喜欢在用户包中放置测试模块,因为测试运行者必须搜索生产代码。 This is probably not a problem for most.这对大多数人来说可能不是问题。 But, if you happen to be a hardware test engineer, you probably use the word 'test' a lot in your production code and don't want the unit test runner to pick that stuff up.但是,如果您碰巧是一名硬件测试工程师,您可能会在生产代码中大量使用“测试”这个词,并且不希望单元测试运行器选择这些东西。 It's much easier if all the tests are in one place separate from the production code.如果所有测试都在一个与生产代码分开的地方,那就容易多了。

  • I can further subdivide my tests folder into the types of tests, such as unit , functional and integration .我可以进一步将我的测试文件夹细分为测试类型,例如unitfunctionalintegration My functional tests tend to have dependencies on weird proprietary hardware, data or are slow.我的功能测试往往依赖于奇怪的专有硬件、数据或速度缓慢。 So it's easy for me to continuously run just the fast unit test folder as I develop.因此,我很容易在开发过程中持续运行快速单元测试文件夹。

  • It can sometimes be convenient to have the tests be inside of the same package hierarchy as what it is testing.有时将测试放在与测试内容相同的包层次结构中会很方便。

Overall though, I think it's important to think for yourself about what's best for your particular problem domain after taking everyone's advice into account.总的来说,我认为在考虑了每个人的建议之后,自己思考什么最适合您的特定问题领域是很重要的。 'Best practices' are great starting points, not end points, for developing a process. “最佳实践”是开发流程的良好起点,而不是终点。

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

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