简体   繁体   English

如何在Python 2.3中使用setUpClass和tearDownClass编写单元测试

[英]How to write a unittest with setUpClass & tearDownClass in Python 2.3

I am trying to write unittests in python 2.3. 我正在尝试在python 2.3中编写单元测试。 I've got everything working accept the class level setUp and tearDown function. 我已经完成了所有工作,接受了类级别的setUp和tearDown函数。 I've tried to look online but am not able to find a way to define these in a python 2.3 unittest case. 我试图在网上查看,但无法找到一种在python 2.3单元测试用例中定义它们的方法。

Hence, is it possible to have setUpClass() & tearDownClass() in a python 2.3 testcase? 因此,是否可以在python 2.3测试用例中使用setUpClass()和tearDownClass()? If yes, how do I go about doing that? 如果是,我该怎么做?

According to the Python 2.3 standard library documentation , unittest.TestCase subclassing should work in the same way it works in more recent versions of the language. 根据Python 2.3标准库文档 ,unittest.TestCase子类应该以与该语言的最新版本相同的方式工作。

import unittest

class MyTest(unittest.TestCase):

    def setUp(self):
        'Do stuff to set up before each test'

    def tearDown(self):
        'Do stuff to tear down after each test'

If there was something more specific that you're trying to accomplish, could you describe it? 如果您要完成一些更具体的事情,您能描述一下吗?

Try to use nose or other runner that is compatible with python 2.3; 尝试使用与python 2.3兼容的机鼻或其他转轮; The python 2.3 runner didn't know about this methods (setUpClass/tearDownClass). python 2.3运行程序不知道此方法(setUpClass / tearDownClass)。

In python 2.3 to decorate a function you should do manually (no syntactic sugar) like: 在python 2.3中,您需要手动完成装饰功能(无需语法糖),例如:

class MyTest(unittest.TestCase):
    def setUpClass(cls): ...
    setUpClass = classmethod(setUpClass)

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

相关问题 Python单元测试:不调用setUpClass(),tearDownClass(),setUpModule()和tearDownModule() - Python unittest: setUpClass(), tearDownClass(), setUpModule() and tearDownModule() are not called 如何在setUpClass中使python单元测试失败? - How to fail a python unittest in setUpClass? 如何编写 setUpClass() 和 tearDownClass() 创建和登录用户?(Django Rest FrameWork) - How do I write setUpClass() and tearDownClass() creating and logging in a user?(Django Rest FrameWork) 如果 setUpClass 抛出异常,如何使 python 单元测试失败 - How to fail a python unittest if the setUpClass throws exception 如何使用 Python Unittest TearDownClass with TestResult.wasSuccessful() - How to use Python Unittest TearDownClass with TestResult.wasSuccessful() unittest setUpClass替代python <2.7 - unittest setUpClass alternative for python < 2.7 当setUpClass失败时,如何清理Python UnitTest? - How can you cleanup a Python UnitTest when setUpClass fails? 来自unittest.TestCase的tearDownClass的工作流程如何? - How is the workflow of tearDownClass from unittest.TestCase? Python 单元测试中的 setUp() 和 setUpClass() 有什么区别? - What is the difference between setUp() and setUpClass() in Python unittest? 在python 2.6中未调用unittest中的setupclass - setupclass in unittest is not being called in python 2.6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM