Python 在单元测试中的测试之间保持变量更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21447740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:52:33  来源:igfitidea点击:

Persist variable changes between tests in unittest?

pythonunit-testingpersistencetestcasepython-unittest

提问by A T

How do I persist changes made within the same object inheriting from TestCasein unitttest?

如何保留从TestCaseunitttest继承的同一对象中所做的更改?

from unittest import TestCase, main as unittest_main


class TestSimpleFoo(TestCase):
    foo = 'bar'

    def setUp(self):
        pass

    def test_a(self):
        self.assertEqual(self.foo, 'bar')
        self.foo = 'can'

    def test_f(self):
        self.assertEqual(self.foo, 'can')


if __name__ == '__main__':
    unittest_main()

I.e.: I want those two tests above to pass

即:我希望上面的两个测试通过

采纳答案by rdp

As some comments have echoed, structuring your tests in this manner is probably a design flaw in the tests themselves and you should consider restructuring them. However, if you want to do this and rely on the fact that the test runner you are using executes them in an alphabetical (seemingly) order then I suggest the following.

正如一些评论所回应的那样,以这种方式构建测试可能是测试本身的设计缺陷,您应该考虑重新构建它们。但是,如果您想这样做并依赖于您正在使用的测试运行器按字母顺序(看似)执行它们的事实,那么我建议如下。

Similar to what @Matthias was saying but I would do one thing differently for the cases where you may decide to inherit from the class at a later date.

与@Matthias 所说的类似,但对于您可能决定在以后从类继承的情况,我会做不同的事情。

from unittest import TestCase, main as unittest_main


class TestSimpleFoo(TestCase):
    foo = 'bar'

    def setUp(self):
        pass

    def test_a(self):
        self.assertEqual(self.__class__.foo, 'bar')
        self.__class__.foo = 'can'

    def test_f(self):
        self.assertEqual(self.__class__.foo, 'can')


if __name__ == '__main__':
    unittest_main()

The difference between this answer and @Matthias's answer you accepted is the explicit declaration of the class versus the lookup of said class reference.

此答案与您接受的@Matthias 的答案之间的区别在于类的显式声明与对所述类引用的查找。

TestSimpleFoo vs self.__class__

I prefer the dynamicness so I can inherit the tests later and run both test classes back to back and not have any cross over between the two. Because if you would choose to inherit from this class, explicitly naming the class reference would cause both test classes to run against that reference rather than their own respective classes.

我更喜欢动态性,因此我可以稍后继承测试并背靠背运行两个测试类,并且两者之间没有任何交叉。因为如果您选择从这个类继承,显式命名类引用将导致两个测试类都针对该引用而不是它们各自的类运行。

回答by A T

Couldn't figure it out; so ended up hacking it out with multiple non test_prefixed functions:

想不通;所以最终用多个非test_前缀函数来破解它:

def test_password_credentials_grant(self):
    for user in self.user_mocks:
        self.register(user)
        self.login(user)
        self.access_token(user, self.assertEqual)  # Ensures access_token is generated+valid
        self.logout(user)
        self.access_token(user, self.assertNotEqual)  # Ensures access_token is now invalid
        self.unregister(user)

回答by Matthias

I like your own answer for the simplicity of it, but if you want to keep distinct unit tests:

我喜欢你自己的简单答案,但如果你想保持不同的单元测试:

Apparently unittest runs separate tests with fresh instances of the TestCase. Well, just bind the objects to be persisted to something else but self. For example:

显然,unittest 使用新的 TestCase 实例运行单独的测试。好吧,只需将要持久化的对象绑定到除 self 之外的其他东西。例如:

from unittest import TestCase, main as unittest_main


class TestSimpleFoo(TestCase):

    def setUp(self):
        pass

    def test_a(self):
        TestSimpleFoo.foo = 'can'

    def test_f(self):
        self.assertEqual(TestSimpleFoo.foo, 'can')


if __name__ == '__main__':
    unittest_main()

You might be interesed in setUpClass and tearDownClass too: https://docs.python.org/3/library/unittest.html#setupclass-and-teardownclass

您可能也对 setUpClass 和 tearDownClass 感兴趣:https://docs.python.org/3/library/unittest.html#setupclass-and-teardownclass

Also take care about the execution order of your unit tests: https://docs.python.org/2/library/unittest.html#unittest.TestLoader.sortTestMethodsUsing

还要注意单元测试的执行顺序:https: //docs.python.org/2/library/unittest.html#unittest.TestLoader.sortTestMethodsUsing

回答by Eugene

Besides what others said, the data share among test methods shouldn't be implemented this way, use setup() is recommended.

除了其他人所说的,测试方法之间的数据共享不应该以这种方式实现,建议使用 setup() 。

The test method itself should be isolated.

测试方法本身应该是孤立的。