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

发布于 2024-12-01 14:31:36 字数 179 浏览 2 评论 0原文

我正在尝试用 python 2.3 编写单元测试。我已经让一切正常工作,接受类级别的设置和拆卸功能。我尝试在网上查找,但无法找到在 python 2.3 单元测试案例中定义这些的方法。

因此,是否可以有 setUpClass() & python 2.3 测试用例中的tearDownClass()?如果是,我该怎么做?

I am trying to write unittests in python 2.3. I've got everything working accept the class level setUp and tearDown function. I've tried to look online but am not able to find a way to define these in a python 2.3 unittest case.

Hence, is it possible to have setUpClass() & tearDownClass() in a python 2.3 testcase? If yes, how do I go about doing that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

提赋 2024-12-08 14:31:36

尝试使用 nose 或其他与 python 2.3 兼容的运行器; python 2.3 运行程序不知道此方法(setUpClass/tearDownClass)。

在 python 2.3 中,要装饰一个函数,您应该手动执行(没有语法糖),例如:

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

Try to use nose or other runner that is compatible with python 2.3; The python 2.3 runner didn't know about this methods (setUpClass/tearDownClass).

In python 2.3 to decorate a function you should do manually (no syntactic sugar) like:

class MyTest(unittest.TestCase):
    def setUpClass(cls): ...
    setUpClass = classmethod(setUpClass)
云柯 2024-12-08 14:31:36

根据 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'

如果您想要完成一些更具体的事情,您能描述一下吗?

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.

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?

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