如何在 pyUnit 中创建类范围的测试装置?

发布于 2024-10-15 16:45:42 字数 483 浏览 5 评论 0原文

我正在对 Mercurial 集成进行单元测试,并且有一个测试类,该类当前在其 setUp 方法中创建一个包含文件和该存储库克隆的存储库,并在其 tearDown 方法中删除它们。

正如您可能想象的那样,这会非常快地提高性能,特别是如果我必须单独为每个测试执行此操作。
所以我想做的是创建文件夹并在加载类时初始化它们以供 Mercurial 使用,这样 TestCase 类中的每个单元测试都可以使用这些存储库。然后,当所有测试都运行时,我想删除它们。我的 setUptearDown 方法唯一需要注意的是两个存储库在每次测试之间处于相同的状态。

基本上我正在寻找的是 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 技术交流群。

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

发布评论

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

评论(2

谁把谁当真 2024-10-22 16:45:42

我现在通过子类化 TestSuite 类来完成此操作,因为标准加载器将所有测试方法包装在定义它们的 TestCase 实例中,并将它们放在 TestSuite 中。我让 TestSuite 调用第一个 TestCase 的 before() 和 after() 方法。这当然意味着您无法初始化 TestCase 对象的任何值,但无论如何您可能希望在 setup 中执行此操作。

TestSuite 如下所示:

class BeforeAfterSuite(unittest.TestSuite):
    def run(self, result):
        if len(self._tests) < 1:
            return unittest.TestSuite.run(self, result)

        first_test = self._tests[0]
        if "before" in dir(first_test):
            first_test.before()
        result = unittest.TestSuite.run(self, result)
        if "after" in dir(first_test):
            first_test.after()
        return result

对于一些更细粒度的控制,我还创建了自定义 TestLoader,它确保 BeforeAfterSuite 仅用于包装测试方法 TestCase 对象,如下所示:

class BeforeAfterLoader(unittest.TestLoader):
    def loadTestsFromTestCase(self, testCaseClass):
        self.suiteClass = BeforeAfterSuite
        suite = unittest.TestLoader.loadTestsFromTestCase(self, testCaseClass)
        self.suiteClass = unittest.TestLoader.suiteClass
        return suite

这里可能缺少一个 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:

class BeforeAfterSuite(unittest.TestSuite):
    def run(self, result):
        if len(self._tests) < 1:
            return unittest.TestSuite.run(self, result)

        first_test = self._tests[0]
        if "before" in dir(first_test):
            first_test.before()
        result = unittest.TestSuite.run(self, result)
        if "after" in dir(first_test):
            first_test.after()
        return result

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:

class BeforeAfterLoader(unittest.TestLoader):
    def loadTestsFromTestCase(self, testCaseClass):
        self.suiteClass = BeforeAfterSuite
        suite = unittest.TestLoader.loadTestsFromTestCase(self, testCaseClass)
        self.suiteClass = unittest.TestLoader.suiteClass
        return suite

Probably missing here is a try/except block around the before and after which could fail all the testcases in the suite or something.

烂人 2024-10-22 16:45:42

来自 Python 单元测试文档

setUpClass():

在单个类中的测试运行之前调用的类方法。 setUpClass 是用类作为唯一参数来调用的,并且必须装饰为 classmethod():

@classmethod
def setUpClass(cls):
...

2.7 版中的新增功能。

tearDownClass() :

在单个类中的测试运行后调用的类方法。调用tearDownClass时将类作为唯一参数,并且必须装饰为classmethod():

@classmethod
def tearDownClass(cls):
    ...

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():

@classmethod
def setUpClass(cls):
...

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():

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