引导测试并使用 Python 测试发现

发布于 2024-12-04 21:37:09 字数 1220 浏览 0 评论 0原文

一个问题是我继续让它“引导”我的测试。

我遇到的问题正是这个人所遇到的问题

顶级解决方案讨论了创建“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 技术交流群。

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

发布评论

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

评论(2

记忆で 2024-12-11 21:37:09

我得到了它!

使用我编写的这个脚本并将其称为“runtests.py”并放置在我的项目根目录中,我能够“引导”,即运行一些初始化代码并使用发现。哇!

就我而言,“引导”代码是两行:

import sys
sys.path.insert(0, 'lib.zip')

谢谢!

#!/usr/bin/python

import unittest
import sys
sys.path.insert(0, 'lib.zip')

if __name__ == "__main__":
    all_tests = unittest.TestLoader().discover('.')
    unittest.TextTestRunner().run(all_tests)

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:

import sys
sys.path.insert(0, 'lib.zip')

Thanks!

#!/usr/bin/python

import unittest
import sys
sys.path.insert(0, 'lib.zip')

if __name__ == "__main__":
    all_tests = unittest.TestLoader().discover('.')
    unittest.TextTestRunner().run(all_tests)
不再见 2024-12-11 21:37:09

这就是我所做的,我认为效果很好。对于与此类似的文件/目录结构:

main_code.py
run_tests.py
    /Modules
        __init__.py
        some_module1.py
        some_module2.py
    /Tests
        __init__.py
        test_module1.py
        test_module2.py

组织 run_tests.py 文件来引导测试相当容易。首先,每个带有测试的文件(test_module1.py等)应该实现一个生成测试套件的函数。类似于:

def suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(Test_Length))
    suite.addTest(unittest.makeSuite(Test_Sum))
    return suite

在测试代码的末尾。然后,在 run_tests.py 文件中,将它们聚合到一个附加的 test_suite 中,并运行它:

import unittest
import Tests.test_module1 as test_module1
import Tests.test_module2 as test_module2

module1_test_suite = test_module1.suite()
module2_test_suite = test_module2.suite()

aggregate_suite = unittest.TestSuite()
aggregate_suite.addTest(module1_test_suite)
aggregate_suite.addTest(module2_test_suite)

unittest.TextTestsRunner(verbosity = 2).run(aggregate_suite

然后要从命令行运行所有这些测试,只需运行

python run_tests.py

Here's what I do, and I think it works quite well. For a file/directory structure similar to this:

main_code.py
run_tests.py
    /Modules
        __init__.py
        some_module1.py
        some_module2.py
    /Tests
        __init__.py
        test_module1.py
        test_module2.py

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:

def suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(Test_Length))
    suite.addTest(unittest.makeSuite(Test_Sum))
    return suite

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:

import unittest
import Tests.test_module1 as test_module1
import Tests.test_module2 as test_module2

module1_test_suite = test_module1.suite()
module2_test_suite = test_module2.suite()

aggregate_suite = unittest.TestSuite()
aggregate_suite.addTest(module1_test_suite)
aggregate_suite.addTest(module2_test_suite)

unittest.TextTestsRunner(verbosity = 2).run(aggregate_suite

Then to run all of these tests, from the command line, simply run

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