简体   繁体   English

我如何使用unittest setUpClass方法()?

[英]How do I use the unittest setUpClass method()?

I'm looking for some basic examples of the python 2.7 unittest setUpClass() method. 我正在寻找python 2.7 unittest setUpClass()方法的一些基本示例。 I'm trying to test some class methods in my module, and I've gotten as far as: 我正在尝试在我的模块中测试一些类方法,并且我已经达到了:

import unittest  
import sys  

import mymodule  

class BookTests(unittest.TestCase):  
    @classmethod  
    def setUpClass(cls):  
        cls._mine = mymodule.myclass('test_file.txt', 'baz')  

But I have no idea how to proceed from here to write tests utilising the object I just created. 但我不知道如何从这里开始利用我刚刚创建的对象编写测试。

In your test methods you can now access the global class atribute _mine through self. 在您的测试方法中,您现在可以通过self访问全局类_mine So you can do something like this: 所以你可以这样做:

def test_something(self):
    self.assertEqual(self._mine.attribute, 'myAttribute')

If you're using Django 1.8+, you should use the setUpTestData method instead. 如果您使用的是Django 1.8+,则应使用setUpTestData方法。

You can then set any attributes on the cls object, and access them via the individual test methods on the self object. 然后,您可以在cls对象上设置任何属性,并通过self对象上的各个测试方法访问它们。

More information is in the Django docs . 更多信息在Django文档中

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

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