Python Django中测试断言的可重用帮助器
我正在为Django 1.4应用程序编写单元测试。在我的tests.py中,我希望有一个可以在测试类中使用的helper函数。辅助对象的定义如下: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}) 下面是
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)