考虑以下包结构
project # Project root
package # Main package that one wants to distribute
... # Modules comprising the package
mockup # Mock up of a package structure used for testing
... # Modules comprising the mock up structure
tests # Testing package containing unittests
... # Modules comprising the unittests
setup.py # setup script
应该如何配置setup.py
脚本
- 在分发过程中仅包含
package
的源代码,但不包括tests
或mockup
文件夹 - 但在使用 tox 运行
tests
时包括mockup
文件夹
目前我指定了以下内容;它将包代码与测试代码区分开来,但在测试执行期间不使mockup
可用。
setup(
...
package = find_packages(exclude=['tests','mockup']), # Includes package code but not tests or mockup folders
test_suite ='tests', # Includes tests but not the mockup's
...
)
在MANIFEST.in
中指定mockup/**
将其包含在发行版中,就像在setup.py
中指定package_data
/include_package_data
一样。也许 Tox 允许模型结构,但我在文档中找不到这个。
tox 通过为您运行python setup.py sdist
来构建一个与setup.py
和MANIFEST.in
中指定的完全相同的包。没有办法构建一个特殊的软件包来测试毒素。这将破坏 tox 的目的,即您准确测试要发布的软件包。
如果您想使用不应成为版本一部分但属于项目一部分的文件/文件夹运行一些特殊测试(因此是您的tox.ini
所在的项目结构的一部分(,我建议您针对项目的开发安装运行这些测试,其中包含您需要的所有内容。您仍应针对其他测试测试软件包,但这些其他测试可以在不同的 tox 环境中完成。
非常简化,在一个包含不同文件夹的不同测试类型的项目中,tests
这可能看起来像这样(我是从pytest用户的角度写的 - 你必须将其转换为unittest - 或者只是使用pytest :)像这样运行你的单元测试(:
[tox]
envlist = tests,mockuptests
[testenv:tests]
description = runs the "normal" tests against the installed package
deps = pytest
commands = pytest tests/normal
[testenv:mockuptests]
description = runs the "mockup" tests against an editable installation
usedevelop = True
deps = pytest
commands = pytest tests/mockup
再三考虑,我认为针对模型测试包也应该以不同的方式进行,而不必将这些文件放入包中,具体取决于测试的结构以及测试运行器的工作方式。作为运行单元测试,我没有关于您的项目设置的足够详细信息,我不知道您在您的情况下如何做到这一点。