包括用于测试但不用于分发的模块结构
Including module structure for testing but not for distribution
考虑以下包结构
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
文件夹
- 但在 运行
tests
和 tox 时包含 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 构建了一个与 setup.py
和 MANIFEST.in
中指定的完全相同的包,由 运行ning python setup.py sdist
为您提供。没有办法构建一个特殊的包来用 tox 进行测试。这会破坏 tox 的目的,即您准确测试要发布的包。
如果你想 运行 一些特殊的 files/folders 测试不应该是发布的一部分,但它们是项目的一部分(所以你的项目结构的一部分 tox.ini
lives) 我建议 运行 这些针对项目开发安装的测试,其中包含所有你需要的。您仍然应该根据其他测试来测试该包,但这些其他测试可以在不同的毒性环境中完成。
非常简单,在 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
再三考虑,我认为根据模型测试包也应该可以以不同的方式进行,而不必将这些文件放入包中,这取决于您的测试结构和测试方式运行 纳尔做事。由于 运行 unittest 并且我没有关于您的项目设置的足够详细信息,我不知道您将如何执行此操作。
考虑以下包结构
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
文件夹 - 但在 运行
tests
和 tox 时包含
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 构建了一个与 setup.py
和 MANIFEST.in
中指定的完全相同的包,由 运行ning python setup.py sdist
为您提供。没有办法构建一个特殊的包来用 tox 进行测试。这会破坏 tox 的目的,即您准确测试要发布的包。
如果你想 运行 一些特殊的 files/folders 测试不应该是发布的一部分,但它们是项目的一部分(所以你的项目结构的一部分 tox.ini
lives) 我建议 运行 这些针对项目开发安装的测试,其中包含所有你需要的。您仍然应该根据其他测试来测试该包,但这些其他测试可以在不同的毒性环境中完成。
非常简单,在 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
再三考虑,我认为根据模型测试包也应该可以以不同的方式进行,而不必将这些文件放入包中,这取决于您的测试结构和测试方式运行 纳尔做事。由于 运行 unittest 并且我没有关于您的项目设置的足够详细信息,我不知道您将如何执行此操作。