如何对目录中的所有测试运行扭曲试验?

发布于 2024-12-06 21:41:56 字数 335 浏览 0 评论 0原文

如何运行试用以便它执行目录中的所有测试?如果我单独对每个文件运行试用,那么我的所有单元测试都会通过,但是如果我在测试目录上尝试类似...

trial test/

的操作,它会给我一个“PASSED”,以及以下消息...

UserWarning:  (for module __init__) not in path importer cache (PEP 302 violation
    - check your local configuration).

而不是实际运行目录中的所有测试。

How do I run trial so that it executes all tests within a directory? All my unit tests pass if I run trial on each file individually, but if I try something like...

trial test/

on the test directory, it gives me one "PASSED", and the following message...

UserWarning:  (for module __init__) not in path importer cache (PEP 302 violation
    - check your local configuration).

rather than actually running all the tests within the directory.

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

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

发布评论

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

评论(1

柒夜笙歌凉 2024-12-13 21:41:56

首先:你不能调用你的顶级单元测试包test。这是 Python 单元测试的名称,因此您将永远无法在已安装的配置中运行测试,并且根据 python 的设置方式,您最终可能会导入 python 自己的测试而不是您自己的测试。

第二:sys.path 是一个巨大而微妙的谜团。

Trial 支持在文件和目录上运行作为快速入门技巧,但它永远不可能真正完全正确地使用路径名。正确的做法是传递一个模块(或包)名称,它可以作为 python 模块导入并检查。

因此,如果您的目录结构如下所示:

~/Projects/MyProject/
~/Projects/MyProject/myproject/
~/Projects/MyProject/myproject/__init__.py
~/Projects/MyProject/myproject/stuff.py
~/Projects/MyProject/myproject/test/
~/Projects/MyProject/myproject/test/__init__.py
~/Projects/MyProject/myproject/test/test_stuff.py

那么您应该像这样运行测试:

PYTHONPATH=$HOME/Projects/MyProject (cd /tmp; trial myproject.test)

换句话说,不要从项目目录中运行测试;这会在源代码中转储 _torial_temp 目录,混淆“我加载代码的位置”和“当前目录”,并且通常会造成各种事情的混乱,以后很难理清。

因此,使用您选择的路径管理工具设置您的 PYTHONPATHPATH组合器setup.py开发virtualenv – 或者只是将垃圾转储到您的~/.bashrc中 – 然后从某个临时位置在一个唯一命名的顶级 Python 包上运行 Trial,一切都会正常工作。

First of all: you can't call your top-level unit test package test. That's the name of Python's unit tests, so you will never be able to run your tests in an installed configuration, and depending on how your python is set up, you may end up importing python's own tests instead of your own.

Second: sys.path is a vast and subtle mystery.

trial supports running on files and directories as a quick getting-started hack, but it can never really be completely correct about using path names. The right thing to do is to pass trial a module (or package) name, that it can import as a python module and inspect.

So if your directory structure looks like:

~/Projects/MyProject/
~/Projects/MyProject/myproject/
~/Projects/MyProject/myproject/__init__.py
~/Projects/MyProject/myproject/stuff.py
~/Projects/MyProject/myproject/test/
~/Projects/MyProject/myproject/test/__init__.py
~/Projects/MyProject/myproject/test/test_stuff.py

then you should run your tests like this:

PYTHONPATH=$HOME/Projects/MyProject (cd /tmp; trial myproject.test)

in other words, don't run your tests from within your project's directory; this dumps _trial_temp directories all over your source code, confuses "the place I load my code from" and "the current directory" and generally makes a muddle of various things which can be difficult to untangle later.

So, set up your PYTHONPATH and PATH using the path-management tool of your choice: Combinator, setup.py develop, virtualenv – or just dumping junk into your ~/.bashrc – and then run trial from some temporary location, on a uniquely-named top-level Python package, and everything should work just fine.

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