简体   繁体   English

测试名称如何影响Unittest中测试之间的变量持久性?

[英]How test name impact variable persistence between tests in Unittest?

How do I persist changes made within the same object inheriting from TestCase in unitttest? 如何在unitttest中继承从TestCase继承的同一对象内所做的更改? I've referred to Persist variable changes between tests in unittest? 在单元测试中提到过持久变量更改吗? . And the following codes works well. 并且以下代码运行良好。

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

However, If I change the test name "test_a" to "test_u", the code will be failed. 但是,如果我将测试名称“ test_a”更改为“ test_u”,则代码将失败。 Anyone can tell how could this happen? 任何人都可以告诉这怎么发生? thanks. 谢谢。

Resolved. 解决。 I ignored the case execution order 我忽略了案件执行顺序

As you already noticed, the execution order is important here. 正如您已经注意到的,执行顺序在这里很重要。 But, in fact, the deeper problem is, that your tests are not independent from each other. 但是,实际上,更深层次的问题是,您的测试不是彼此独立的。 Ideally, each test behaves like a complete program of its own, starting from a defined clean starting state, and cleaning up after execution. 理想情况下,每个测试的行为就像其自身的完整程序一样,从定义的干净启动状态开始,然后在执行后进行清理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM