如何在 pyUnit 中创建类范围的测试装置?
我正在对 Mercurial 集成进行单元测试,并且有一个测试类,该类当前在其 setUp 方法中创建一个包含文件和该存储库克隆的存储库,并在其 tearDown 方法中删除它们。
正如您可能想象的那样,这会非常快地提高性能,特别是如果我必须单独为每个测试执行此操作。
所以我想做的是创建文件夹并在加载类时初始化它们以供 Mercurial 使用,这样 TestCase 类中的每个单元测试都可以使用这些存储库。然后,当所有测试都运行时,我想删除它们。我的 setUp 和 tearDown 方法唯一需要注意的是两个存储库在每次测试之间处于相同的状态。
基本上我正在寻找的是 JUnit 的 @BeforeClass
和 @AfterClass
< 的 python 等价物/em> 注释。
I am unit testing mercurial integration and have a test class which currently creates a repository with a file and a clone of that repository in its setUp method and removes them in its tearDown method.
As you can probably imagine, this gets quite performance heavy very fast, especially if I have to do this for every test individually.
So what I would like to do is create the folders and initialize them for mercurial on loading the class, so each and every unittest in the TestCase class can use these repositories. Then when all the tests are run, I'd like to remove them. The only thing my setUp and tearDown methods then have to take care of is that the two repositories are in the same state between each test.
Basically what I'm looking for is a python equivalent of JUnit's @BeforeClass
and @AfterClass
annotations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我现在通过子类化 TestSuite 类来完成此操作,因为标准加载器将所有测试方法包装在定义它们的 TestCase 实例中,并将它们放在 TestSuite 中。我让 TestSuite 调用第一个 TestCase 的 before() 和 after() 方法。这当然意味着您无法初始化 TestCase 对象的任何值,但无论如何您可能希望在 setup 中执行此操作。
TestSuite 如下所示:
对于一些更细粒度的控制,我还创建了自定义 TestLoader,它确保 BeforeAfterSuite 仅用于包装测试方法 TestCase 对象,如下所示:
这里可能缺少一个 try/ except 块前后的前后可能会使套件中的所有测试用例或其他测试用例失败。
I've now done it by subclassing the TestSuite class, since the standard loader wraps all the test methods in an instance of the TestCase in which they're defined and puts them together in a TestSuite. I have the TestSuite call the before() and after() methods of the first TestCase. This of course means that you can't initialize any values to your TestCase object, but you probably want to do this in your setUp anyway.
The TestSuite looks like this:
For some slightly more finegrained control I also created the custom TestLoader which makes sure the BeforeAfterSuite is only used to wrap test-method-TestCase objects in, which looks like this:
Probably missing here is a try/except block around the before and after which could fail all the testcases in the suite or something.
来自 Python 单元测试文档:
setUpClass():
在单个类中的测试运行之前调用的类方法。 setUpClass 是用类作为唯一参数来调用的,并且必须装饰为 classmethod():
2.7 版中的新增功能。
tearDownClass() :
在单个类中的测试运行后调用的类方法。调用tearDownClass时将类作为唯一参数,并且必须装饰为classmethod():
from the Python unittest documentation :
setUpClass() :
A class method called before tests in an individual class run. setUpClass is called with the class as the only argument and must be decorated as a classmethod():
New in version 2.7.
tearDownClass() :
A class method called after tests in an individual class have run. tearDownClass is called with the class as the only argument and must be decorated as a classmethod():