Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django中测试断言的可重用帮助器_Python_Django - Fatal编程技术网

Python Django中测试断言的可重用帮助器

Python Django中测试断言的可重用帮助器,python,django,Python,Django,我正在为Django 1.4应用程序编写单元测试。在我的tests.py中,我希望有一个可以在测试类中使用的helper函数。辅助对象的定义如下: def error_outcome(self, response): self.assertEqual(response.status_code, 403) data = json.loads(response._get_content()) self.assertEquals(data, {'error': 1}) 下面是

我正在为Django 1.4应用程序编写单元测试。在我的tests.py中,我希望有一个可以在测试类中使用的helper函数。辅助对象的定义如下:

def error_outcome(self, response):
    self.assertEqual(response.status_code, 403)
    data = json.loads(response._get_content())
    self.assertEquals(data, {'error': 1})
下面是使用帮助器的示例测试类:

class SomeTest(TestCase):
    def test_foo(self):    
        request = RequestFactory().post('/someurl')
        response = view_method(request)
        error_outcome(self, response)

这是可行的,但是这并不好,因为助手不应该使用self,因为它是一个函数,而不是一个方法。你知道如何在没有自我的情况下完成这项工作吗?谢谢。

使用定义的
error\u outcome()
方法创建一个基本测试用例类:

class BaseTestCase(TestCase):
    def error_outcome(self, response):
        self.assertEqual(response.status_code, 403)
        data = json.loads(response._get_content())
        self.assertEquals(data, {'error': 1})

class SomeTest(BaseTestCase):
    def test_foo(self):    
        request = RequestFactory().post('/someurl')
        response = view_method(request)
        self.error_outcome(response)