将我的Python单元测试安装在站点程序包中有意义吗

将我的Python单元测试安装在站点程序包中有意义吗

本文介绍了将我的Python单元测试安装在站点程序包中有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发我的第一个Python发行包.我的学习曲线Python封装似乎已经趋于平稳,但我仍在努力一些悬而未决的问题.一是我是否应该使我的单元测试成为安装在我的代码旁边.

I'm developing my first Python distribution package. My learning curve onPython packaging seems to be leveling off a bit, but I'm still wrestling witha few open questions. One is whether I should cause my unit tests to beinstalled alongside my code.

我了解在源分发中包含测试很重要.我想知道的是我是否应该实际配置它们以进行安装?

I understand it's important to include tests in a source distribution. What I'm wondering is whether I should actually configure them to be installed?

我已经看到至少一个流行的软件包似乎是故意这样做的( PyHamcrest ),至少还有一个似乎是无意中这样做的(行为).

I've seen at least one popular package that appears to do this on purpose(PyHamcrest), and at least one other that appears to do it by accident(behave).

所以我的(多部分)问题是这样的:

So my (multi-part) question is this:

  • 在我的软件包单元测试旁边安装我的软件包单元测试是否有意义打包代码?

  • Does it ever make sense to install my package unit tests alongside mypackage code?

如果是,用例是什么?谁会使用它们?用途是什么?那是谁会使用它们,并不十分乐意下载源代码分发并改为运行python setup.py test?

If so, what is the use case? Who would use them and for what? That is, whowould use them that wouldn't be perfectly happy to download the sourcedistribution and run python setup.py test instead?

以及他们将如何使用已安装的单元测试?像import test; test.run()之类的这样吗?

And how would they use installed unit tests? like import test; test.run() or somethinglike that?

推荐答案

在研究了这个问题之后,直到经验丰富的人有一分钟可以反驳的时候,我的理解是,简单的答案是:不,单位测试不应该安装,仅包含在源代码发行版"中.

After researching this issue, and until someone more experienced has a minute to weigh in to the contrary, my understanding is that the simple answer is: "No, unit tests should not be installed, only included in the source distribution".

在我发现安装测试的少数情况下,所有结果都是偶然,并且比没有想到的容易而没有注意到.

In the handful of cases I found where tests were installed, all turned out to be accidental and it's easier than one might think to make the mistake without noticing it.

这是怎么回事:

  1. setup.py中使用了packages=find_packages()参数,因此无需显式列出软件包即可找到它们.
  2. test文件夹被打包(通过添加__init__.py),因此测试可以使用相对命名(例如from .. import pkg.mod)引用测试的模块.
  3. setuptools与项目中的其他软件包一起将test作为单独的软件包安装.请注意,这意味着您可以在python解释器中执行import test,并且可以正常工作,几乎可以肯定不是您想要的,特别是由于很多其他人在测试目录中使用该名称:)
  1. The packages=find_packages() parameter is used in setup.py so packages can be found without having to list them out explicitly.
  2. The test folder is made into a package (by adding __init__.py) so tests can reference the modules they test using relative naming (like from .. import pkg.mod).
  3. setuptools installs test as a separate package, alongside the other(s) in the project. Note this means you can execute import test in the python interpreter and it works, almost certainly not what you intended, especially since a lot of other folks use that name for their test directory :)

解决方法是使用设置:packages=find_packages(exclude=['test'])防止安装测试目录.

The fix is to use the setting: packages=find_packages(exclude=['test']) to prevent your test directory from being installed.

这篇关于将我的Python单元测试安装在站点程序包中有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 11:43