如何避免Django测试方法相互干扰

时间:2019-05-17 11:53:53

标签: django django-tests

我目前正在为我的app1编写一些Django测试,并遇到一个奇怪的错误。我有一个测试方法,该方法仅执行获取请求并检查响应的status_code。该测试工作正常,但是只要我的测试运行中包含此测试,应用程序2中的测试就会突然失败。我在其他西装中都有这些类型的get请求,它们工作正常且不会造成任何问题。

采用测试方法,或者简单地通过get请求再次解决问题。我尝试调用不同的URL,从而始终导致相同的错误。我还尝试给测试方法使用不同的名称,但没有成功。

test_folder
| _ init .py
| _ base.py
| _ test_app_1
| | _ test_view1.py
| | _ init .py
| _ test_app_2
 | _ test_view_2.py
 | _ init .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且该方法中包含get请求时,才存在以下追溯:

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".

0 个答案:

没有答案