简体   繁体   English

unittest.TestCase的setUp方法是否知道当前测试用例?

[英]Does setUp method from unittest.TestCase know the current test case?

Does the setUp method from the unittest.TestCase knows what is the next test case that will be executed? unittest.TestCase中的setUp方法是否知道下一个要执行的测试用例是什么? For example: 例如:

import unittest

class Tests(unittest.TestCase):
    def setUp(self):
        print "The next test will be: " + self.next_test_name()

    def test_01(self):
        pass

    def test_02(self):
        pass

if __name__ == '__main__':
    unittest.main()

Such code should produce upon execution: 此类代码应在执行时产生:

The next test will be: test_01
The next test will be: test_02

No, unittest makes no guarantee about the order of test execution. 不, unittest无法保证测试执行的顺序。

Moreover, you should structure your unit tests not to rely on any particular order. 此外,您应该构建单元测试,使其不依赖任何特定顺序。 If they require some state setup from another test method then you no longer have, by definition, a unit test. 如果他们需要通过其他测试方法进行某些状态设置,那么根据定义,您将不再具有单元测试。

The current test method name which will be executed lives in self._testMethodName , but use it at your own risk (accessing _private attributes is subject to breakage without warning). 当前将执行的测试方法名称位于self._testMethodName ,但使用该方法的风险由您自己承担(访问_private属性会遭到破坏,而不会发出警告)。 Do not use this to customise setUp based on the particular test method, prefer to split tests requiring a different setup off into separate test classes. 不要使用此方法基于特定的测试方法自定义setUp ,而希望将需要不同设置的测试拆分为单独的测试类。

class Tests(unittest.TestCase):
    def setUp(self):
        print "The next test will be: " + self.id()

    def test_01(self):
        pass

    def test_02(self):
        pass

if __name__ == '__main__':
    unittest.main()

Which will produce: 会产生:

The next test will be: __main__.Tests.test_01
The next test will be: __main__.Tests.test_02

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

相关问题 Selenium python如何使用来自unittest.TestCase的assertEqual方法“没有这样的测试方法 <class 'unittest.case.TestCase'> ” - Selenium python how to use assertEqual method from unittest.TestCase “No such test method in <class 'unittest.case.TestCase'>” 是否可以从 python unittest.TestCase 运行单个测试方法并引用该方法? - Is it possible to run a single test method from a python unittest.TestCase with a reference to the method? 使用unittest.TestCase子类Python的方法调用测试方法 - Invoking Test Methods with a Method for a unittest.TestCase Subclass Python 从unittest.TestCase继承以实现非测试功能 - Inheriting from unittest.TestCase for non-test functionality 通过命令行从 unittest.TestCase 运行单个测试 - Running a single test from unittest.TestCase via the command line 访问当前的unittest.TestCase实例 - access the current unittest.TestCase instance Django 测试运行器不尊重 unittest.TestCase? - Django test runner not respecting unittest.TestCase? 使用unittest.TestCase的运行方法 - Using run method of unittest.TestCase Python:在unittest.TestCase中基于TestCase修改SetUp - Python: Modify SetUp based on TestCase in unittest.TestCase 来自unittest.TestCase的tearDownClass的工作流程如何? - How is the workflow of tearDownClass from unittest.TestCase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM