使用典型的测试目录结构运行 unittest [英] Running unittest with typical test directory structure
问题描述
对于一个简单的 Python 模块来说,非常常见的目录结构似乎是将单元测试分离到它们自己的 test
目录中:
The very common directory structure for even a simple Python module seems to be to separate the unit tests into their own test
directory:
new_project/
antigravity/
antigravity.py
test/
test_antigravity.py
setup.py
etc.
例如,请参阅此 Python 项目操作指南.
我的问题很简单实际运行测试的常用方法是什么?我怀疑这对除了我之外的每个人都很明显,但你不能只运行 python test_antigravity.py
来自测试目录,因为它的 import antigravity
将失败,因为模块不在路径上.
My question is simply What's the usual way of actually running the tests? I suspect this is obvious to everyone except me, but you can't just run python test_antigravity.py
from the test directory as its import antigravity
will fail as the module is not on the path.
我知道我可以修改 PYTHONPATH 和其他与搜索路径相关的技巧,但我不敢相信这是最简单的方法 - 如果您是开发人员,这很好,但如果您的用户只是想检查,则期望他们使用是不现实的测试通过.
I know I could modify PYTHONPATH and other search path related tricks, but I can't believe that's the simplest way - it's fine if you're the developer but not realistic to expect your users to use if they just want to check the tests are passing.
另一种选择只是将测试文件复制到另一个目录中,但这似乎有点愚蠢,并且没有将它们放在单独的目录中开始.
The other alternative is just to copy the test file into the other directory, but it seems a bit dumb and misses the point of having them in a separate directory to start with.
那么,如果您刚刚将源代码下载到我的新项目中,您将如何运行单元测试?我更喜欢让我对用户说的答案:要运行单元测试,请执行 X."
So, if you had just downloaded the source to my new project how would you run the unit tests? I'd prefer an answer that would let me say to my users: "To run the unit tests do X."
推荐答案
我认为最好的解决方案是使用 unittest
命令行界面 它将目录添加到 sys.path
所以你没有到(在 TestLoader
类中完成).
The best solution in my opinion is to use the unittest
command line interface which will add the directory to the sys.path
so you don't have to (done in the TestLoader
class).
例如对于这样的目录结构:
For example for a directory structure like this:
new_project
├── antigravity.py
└── test_antigravity.py
你可以直接运行:
$ cd new_project
$ python -m unittest test_antigravity
对于像您这样的目录结构:
For a directory structure like yours:
new_project
├── antigravity
│ ├── __init__.py # make it a package
│ └── antigravity.py
└── test
├── __init__.py # also make test a package
└── test_antigravity.py
并且在 test
包内的测试模块中,您可以照常导入 antigravity
包及其模块:
And in the test modules inside the test
package, you can import the antigravity
package and its modules as usual:
# import the package
import antigravity
# import the antigravity module
from antigravity import antigravity
# or an object inside the antigravity module
from antigravity.antigravity import my_object
运行单个测试模块:
要运行单个测试模块,在本例中为 test_antigravity.py
:
To run a single test module, in this case test_antigravity.py
:
$ cd new_project
$ python -m unittest test.test_antigravity
只需以与导入相同的方式引用测试模块即可.
Just reference the test module the same way you import it.
运行单个测试用例或测试方法:
您也可以运行单个 TestCase
或单个测试方法:
Also you can run a single TestCase
or a single test method:
$ python -m unittest test.test_antigravity.GravityTestCase
$ python -m unittest test.test_antigravity.GravityTestCase.test_method
运行所有测试:
您还可以使用 测试发现,它会发现并为您运行所有测试,它们必须是名为 test*.py
的模块或包(可以使用 -p, --pattern
标志更改):
You can also use test discovery which will discover and run all the tests for you, they must be modules or packages named test*.py
(can be changed with the -p, --pattern
flag):
$ cd new_project
$ python -m unittest discover
$ # Also works without discover for Python 3
$ # as suggested by @Burrito in the comments
$ python -m unittest
这将运行 test
包中的所有 test*.py
模块.
This will run all the test*.py
modules inside the test
package.
这篇关于使用典型的测试目录结构运行 unittest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!