Nose:测试给定包中的所有模块

发布于 2024-12-04 02:18:10 字数 738 浏览 1 评论 0原文

我有一个包含多个子包的包,其中一个用于测试(名为 tests)。由于子包名称清楚地表明所包含的模块是测试模块,因此我不希望使用测试名称模式污染模块名称,nose 希望包含它们以进行测试。这就是我正在考虑的设置:

- foo
  - __init__.py
  ...
  - bar
    - __init__.py
    ...
  - baz
    - __init__.py
    ...
  - tests
    - __init__.py
    - something.py

现在,默认情况下,nose 不会运行 foo.tests.something 中找到的测试。我知道nose接受-i选项来定义正则表达式,以搜索测试中的其他内容。因此nose -i some在这里完成了这项工作。但是,我在 tests 包中有一堆模块,并且不想明确命名它们。 nose -itests\..* 不起作用,看起来鼻子只与模块基本名称匹配。作为最后的手段,我可​​以运行 nose --all-modules,但这也会检查 foo.barfoo.baz - 我'我想避免这种情况。

那么,我如何指示nose在给定包内的所有模块中查找测试(在我的例子中是tests)?我可以为这项任务编写一个鼻子插件,但我正在寻找一个标准解决方案。

I have a package with several sub-packages, one of them for tests (named tests). Since the sub-package name makes it clear that contained modules are test modules, I prefer not to pollute module names with the test-name pattern nose expects to include them for testing. That's the setup I'm thinking of:

- foo
  - __init__.py
  ...
  - bar
    - __init__.py
    ...
  - baz
    - __init__.py
    ...
  - tests
    - __init__.py
    - something.py

Now, by default nose does not run tests found in foo.tests.something. I know nose accepts the -i option to define regular expressions for additional stuff to search tests in. So nose -i something does the job here. However, I have a bunch of modules in the tests package and do not want to name them explicitely. nose -i tests\..* does not work, it looks like nose only matches against module base-names. As a last resort I could run nose --all-modules, but this also inspects foo.bar and foo.baz -- I'd like to avoid this.

So, how could I instruct nose to look for tests in all modules within a given package (tests in my case)? I could write a nose plugin for this task, but I'm looking for a standard solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兲鉂ぱ嘚淚 2024-12-11 02:18:10

如果您在测试中使用 test_ 前缀命名文件(即将 something.py 重命名为 test_something.py),运行鼻子应该通过 。

你说“我不想用测试名称模式污染模块名称,鼻子希望包含它们进行测试”,但“某事”不是文件的描述性,因为该文件测试了某事有什么问题 使用非混淆的,命名测试的标准方法?

If you name the files in tests with a test_ prefix (i.e. rename something.py to test_something.py, running nose should pick them up by default.

You say "I prefer not to pollute module names with the test-name pattern nose expects to include them for testing", but "something" isn't descriptive of the file, because the file tests that something. What's the problem with using the non-confusing, standard way of naming your tests?

注定孤独终老 2024-12-11 02:18:10

您应该能够将这些文件导入到 __init__.py 中并获取它们。我有这样的设置:

- app:
  - __init__.py
  - models.py
  - views.py
  - tests:
    - __init__.py
    - models.py # contain tests
    - views.py # contain tests

tests/__init__.py 文件中,我有以下内容:

from app.tests.models import *
from app.tests.views import *

这破坏了使用正则表达式查找测试的好处之一(鼻子的目标)但有效。

如果您想避免命名方法来匹配正则表达式,您还可以使用装饰器 @istest 将单个 def 标记为测试。我没有尝试对与正则表达式不匹配的模块(py 文件)执行此操作,但我怀疑如果没有上述导入,它是否会起作用。

请注意,我已经放弃了 __init__.py 中的导入,只需在我的测试方法和文件名前添加 test_ 前缀,并在我的类后添加 Test 即可。我发现这使得代码更具自描述性,因为即使在测试类中也可能有设置方法和辅助方法(例如生成器)。

You should be able to import the files into your __init__.py and have them picked up. I had this setup:

- app:
  - __init__.py
  - models.py
  - views.py
  - tests:
    - __init__.py
    - models.py # contain tests
    - views.py # contain tests

In the tests/__init__.py file I had the following:

from app.tests.models import *
from app.tests.views import *

Which defeats one of the benefits of using a regex to find tests (a goal of nose) but worked.

You can also use the decorator @istest to flag an individual def as a test if you want to avoid the naming of methods to match the regex. I've not tried to do this for modules (py files) that don't also match the regex but I doubt it would work without the above import.

Note, I've moved away from the import in __init__.py and just prefix my test methods and filenames with test_ and postfix my classes with Test. I find this makes the code more self-describing as even in a Test class there may be setup methods and helper methods (e.g. generators).

醉态萌生 2024-12-11 02:18:10

使用 Nose:

nosetests --all-modules

使用 py.test 可以通过将其放入 setup.cfg(配置文件)中来完成:

[pytest]
python_files=*.py

检查此文档:pytest 文档

with Nose:

nosetests --all-modules

With py.test it can be done by put this in setup.cfg (configure file):

[pytest]
python_files=*.py

check this doc: pytest docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文