引导测试并使用 Python 测试发现
一个问题是我继续让它“引导”我的测试。
我遇到的问题正是这个人所遇到的问题。
顶级解决方案讨论了创建“boostrap”脚本。我认为我必须枚举要运行的所有测试,或者使用 __all__
关键字在 __init__.py
文件中使用测试清单。但是,我注意到关于unittest的最新Python文档没有谈论__all__
不再了。
在 2.7 中,我们有名为“discovery”的 python 命令
python -m unittest discover
,它的效果更好。因为: 1)不需要鼻子 2)不需要测试清单
但它似乎没有办法“引导”
我需要使用另一个测试运行程序吗?一种允许引导和发现的系统?
我需要 py.test 吗?
http://pytest.org/
我需要引导的原因是 这个人有。基本上,如果我直接运行测试,我的导入语句将无法正常工作。我想从项目顶部执行我的测试套件,就像应用程序正常运行时一样。
毕竟,导入语句始终与其物理位置相关。 (顺便说一句,我认为这是Python的一个障碍)
定义:什么是Bootstrapping? 引导意味着我想在整个项目中运行任何测试之前进行一些设置。这有点像我要求在整个项目级别进行“测试设置”。
更新 这是关于同一件事的另一篇文章。使用这个2.7命令,我们可以避免Nose。但如何添加引导程序呢?
A problem I continue to have it "bootstrapping" my tests.
The problem that I have is exactly what this guy has.
The top solution talks about creating a "boostrap" script. I presume that I must then enumerate all of the tests to be run, or use test manifests in the __init__.py
files using the __all__
keyword. However, I noticed that the most recent Python documentation on unittest does not talk about __all__
anymore.
In 2.7, we have the python command called "discovery"
python -m unittest discover
That works even nicer. Because:
1) There's no need for Nose
2) There's no need for test manifests
But it doesn't seem to have a way to "bootstrap"
Do I need to use another test runner? One that allows bootstrapping AND discovery?
Do I need py.test?
http://pytest.org/
The reason that I need bootstrapping, is the problem that this guy has. Basically, my import statements don't work right if I run the test directly. I want to execute my suite of tests from the top of my project, just like the app would when it runs normally.
After all, import statements are always relative to their physical location. (BTW, I think this is a hindrance in Python)
Definition: What is Bootstrapping?
Bootstrapping means that I want to do some setup before running any tests at all in the entire project. This is sort of like me asking for a "test setup" at the whole project level.
Update
Here is another posting about the same thing. Using this 2.7 command, we can avoid Nose. But how does one add bootstrapping?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我得到了它!
使用我编写的这个脚本并将其称为“runtests.py”并放置在我的项目根目录中,我能够“引导”,即运行一些初始化代码并使用发现。哇!
就我而言,“引导”代码是两行:
谢谢!
I got it!
Using this one script that I wrote and called it "runtests.py" and placed in my project root, I was able to "bootstrap" that is to run some initialization code AND use discovery. Woot!
In my case, the "bootstrap" code is the two lines that say:
Thanks!
这就是我所做的,我认为效果很好。对于与此类似的文件/目录结构:
组织 run_tests.py 文件来引导测试相当容易。首先,每个带有测试的文件(
test_module1.py
等)应该实现一个生成测试套件的函数。类似于:在测试代码的末尾。然后,在 run_tests.py 文件中,将它们聚合到一个附加的 test_suite 中,并运行它:
然后要从命令行运行所有这些测试,只需运行
Here's what I do, and I think it works quite well. For a file/directory structure similar to this:
It's fairly easy to organize your run_tests.py file to bootstrap the tests. First every file with test (
test_module1.py
, etc.) should implement a function that generates a test suite. Something like:at the end of your test code. Then, in the run_tests.py file, you aggregate these into an additional test_suite, and run that:
Then to run all of these tests, from the command line, simply run