Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 包括用于测试但不用于分发的模块结构_Python_Setuptools_Tox - Fatal编程技术网

Python 包括用于测试但不用于分发的模块结构

Python 包括用于测试但不用于分发的模块结构,python,setuptools,tox,Python,Setuptools,Tox,考虑以下包结构 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

考虑以下包结构

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
脚本配置为

  • 在分发过程中,仅包括
    的源代码,但不包括
    测试
    模型
    文件夹
  • 但是,在使用tox运行
    测试时,请包括
    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\u数据一样。也许Tox允许实体模型结构,但我在文档中找不到这一点。

Tox构建的包与
setup.py
MANIFEST.in
中指定的包完全相同,方法是为您运行
python setup.py sdist
。没有办法为tox测试构建一个特殊的包。这将破坏tox的目的,即准确地测试要发布的包

如果您想对不应属于发行版的文件/文件夹运行一些特殊测试,但这些文件/文件夹是项目的一部分(因此是您的
tox.ini
所在的项目结构的一部分),我建议对您的项目的开发安装运行这些测试,其中包含您所需的所有内容。您仍然应该根据其他测试测试包,但是这些其他测试可以在不同的tox环境中完成

非常简单,在一个项目中,在
tests
下的不同文件夹中包含不同的测试类型,这看起来可能是这样的(我是从pytest用户的角度写这篇文章的-您必须将其转换为unittest-或者只需使用pytest:运行这样的unittests即可):


经过再三考虑,我认为,根据测试的结构和testrunner的工作方式,在不必将这些文件放入包的情况下,也可以用不同的方式对模型测试包。在运行unittest时,我没有关于您的项目设置的足够详细信息,我不知道您将如何在您的情况下执行此操作。

这解决了问题,谢谢。我为回复缓慢表示歉意我几乎立刻看到了你的帖子,但我花了一些时间才回复。我对
usedevelop=True
通过
pip install-e.
,或类似等效工具,将整个项目安装到测试虚拟环境的站点包中的理解是否正确?很高兴我能提供帮助:)是的,这是正确的-usedevelop在开发中安装包(或可编辑)在查看
usedevelop
上的tox文档时,我注意到还有一个
extras
参数。文档中并没有特别清楚地说明如何使用它,但我得到的印象是,它用更多的内容填充了虚拟环境。也许这提供了一种包含“模拟”包的替代方法?extras安装setup.py()的extras_require中定义的内容-这无助于解决此问题,因为您需要的不是包,而是项目中文件夹的内容。tox中的配置选项可以更详细地解释额外的require.Oh sweet,因此这是安装说明中经常看到的可选部分,例如
pip安装[extra]包中的
extras
。今天早上我在想这件事。
[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