Python 每次运行测试用例时,获取或创建一个新对象
我正在测试我的api,每次运行测试用例时都使用python manage.py test api.users.tests--keepdb它每次都在创建一个新的语言对象,相反,它应该获取现有的语言对象本身,因为我没有破坏数据库Python 每次运行测试用例时,获取或创建一个新对象,python,django,web-api-testing,Python,Django,Web Api Testing,我正在测试我的api,每次运行测试用例时都使用python manage.py test api.users.tests--keepdb它每次都在创建一个新的语言对象,相反,它应该获取现有的语言对象本身,因为我没有破坏数据库 class ProfileTest(TestCase): @classmethod @override_settings(DEBUG=True) def setUpTestData(cls): instance, is_create
class ProfileTest(TestCase):
@classmethod
@override_settings(DEBUG=True)
def setUpTestData(cls):
instance, is_created = Language.objects.get_or_create(value='English', locale='en')
url = '/api/v1/auth/login/'
data = {'mobile_number': '9899137678', 'country_code': '91', 'device_id': '123'}
response = Client().post(path=url, data=data)
json_response = response.json()
url = '/api/v1/auth/verify/'
data = {'code': json_response['otp'],
'verification_id': json_response['verification_id'],
'language': instance.id,
'registration_id': '123',
'type': '1',
'device_id': json_response['device_id']}
response = Client().post(path=url, data=data)
json_response = response.json()
token = json_response['token']['access']
token = 'Token ' + token
cls.headers = {
'HTTP_AUTHORIZATION': token,
}
def test_get_user_profile(self):
url = '/api/v1/users/profile/'
response = self.client.get(path=url, **self.headers)
print('\n', response.content, '\n')
self.assertEquals(response.status_code, status.HTTP_200_OK, 'Couldn\'t fetch profile data.')
你确定用
True
检查断言是否已创建
@YugandharChaudhari我已经用print()语句检查过了。它每次都打印一个新的对象id。