django - 如何避免 django 测试方法相互干扰
问题描述
我目前正在为我的 app1 编写一些 django 测试并遇到一个奇怪的错误。我有一个测试方法,它只是执行一个 get 请求并检查响应的 status_code。该测试运行良好,但只要在我的测试运行中包含此测试,应用程序 2 中的测试就会突然失败。我在其他套装中有这类获取请求,它们工作正常且不会引起任何问题。
取出测试方法,或者简单地获取请求再次解决问题。我尝试调用不同的 url,结果总是相同的错误。我还尝试给测试方法起不同的名称,但没有成功。
测试文件夹
|_ init .py
|_ base.py
|_ test_app_1
| |_ test_view1.py
| |_初始化.py
|_ test_app_2
|_ test_view_2.py
|_初始化.py
# test_folder/test_app_1/test_view1.py
from tests_folder.base import BaseTestCase
class CaseOneGIPTestCase(BaseTestCase):
def setUp(self):
self.client.login(username='temporary', password='temporary')
def test_app1(self):
resp = self.client.get(reverse('planning1'))
self.assertIs(resp.status_code, 200)
def test_true(self):
self.assertTrue(True)
# test_folder/base.py
from django.test import TestCase
from django.contrib.auth import get_user_model
class BaseTestCase(TestCase):
@classmethod
def setUpTestData(cls):
User = get_user_model()
user = User.objects.create_user('temporary', 'temporary')
这是一个失败测试的例子
# test_folder/test_app_2/test_view2.py
from tests_folder.base import BaseTestCase
class FailingTestCase(BaseTestCase):
def test_load_nearest_assets(self):
response = self.client.get(reverse('urlblabla'),
{'data': "{\"key1\": \"value\", \"key2\": \"value\", \"key3\": \"value\"}"},
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
content = response.json()
self.assertTrue(type(content['key4']['key5']) == list)
仅当存在测试方法 test_app1 并且该方法中包含获取请求时,才会出现以下回溯:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/test/testcases.py", line 209, in __call__
self._post_teardown()
File "/usr/local/lib/python3.5/dist-packages/django/test/testcases.py", line 908, in _post_teardown
self._fixture_teardown()
File "/usr/local/lib/python3.5/dist-packages/django/test/testcases.py", line 1056, in _fixture_teardown
connections[db_name].check_constraints()
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/postgresql/base.py", line 245, in check_constraints
self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE')
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 83, in _execute
return self.cursor.execute(sql)
django.db.utils.IntegrityError: insert or update on table "users_customuser" violates foreign key constraint "users_customuser_create_by_id_6250cba4_fk_users_customuser_id"
DETAIL: Key (create_by_id)=(47) is not present in table "users_customuser".
解决方案
推荐阅读
- visual-studio-code - 可以在 Emmet 自动完成上更改类标签名称吗?
- r - ggplot2热图没有从高到低排列值
- gnuplot - brew install gnuplot --with-qt --with-x11 不工作
- typescript - Typescript 中的泛型函数图
- node.js - Typescript instanceof 在我的 API 网关上不起作用
- laravel - Laravel MethodNotAllowedHttpException on put 动作
- javascript - 需要用 IF 语句更新 custom.js 中的 href
- java - SFTP 将文件上传到同一远程服务器上的多个目录
- c# - 自动完成字段和多项选择
- html - 应用 google web 字体后,部分背景被移除。我应该怎么办?