Django Rest Framework:单元测试数据库问题 [英] Django rest framework: unit testing database issue

查看:158
本文介绍了Django Rest Framework:单元测试数据库问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对其余Apis进行单元测试。我正在使用django rest框架。
Apis正在将数据保存到数据库中以及从数据库中获取数据。这两个操作均不起作用,或者如果不起作用,则无法在数据库中看到。 Apis也正在使用django-fsm,因此我需要从db获得用于其他测试的相同数据。由于django-fsm,测试取决于先前的测试。 api总是会改变状态。但是现在我无法在测试运行期间看到数据库中的任何数据。不知道它将数据保存在哪里或在哪个数据库中。

I am doing unit testing of the rest Apis. I am using django rest framework. Apis are saving data into and getting data from the database. Both of the operations are not working or if it is working i am not able to see that in the databases. Apis are also using django-fsm, because of which i need same data from the db for the other tests. Tests depends on previous tests due to django-fsm. There is always state changing with the api. But now i am not able to see any data in database during test runs. Don't know where it is saving the data or in which database.

以下是我的测试设置:-

Below is my test settings:-

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': join(PROJECT_ROOT, 'run', 'db_for_testing.sqlite3'),
    'TEST': {
        'NAME': 'test_db_for_testing',
    },
 },
}

以下是我的api:-

class DummyView(CreateAPIView):
    def post(self, request, *args, **kwargs):
        data = request.data.copy()
        serializer = self.get_serializer(data=data)
        serializer.is_valid(raise_exception=True)
        order = self.model(book=serializer.data.get('book'))
        order.save()
        data = {
        'status_code': 200,
        'message': 'successfully.'
        }
        return Response(data, status=status.HTTP_200_OK)

作为我的测试取决于先前的测试将数据保存到db,因此其他测试也失败。我正在使用rest_framework的APITestCase。
伙计们。
提前表示感谢。

As my tests depends on the previous test saving the data to db, so the other tests also fails. I am using APITestCase of rest_framework. Help guys. thanks in advance.

推荐答案

TL; DR-解决方案:使用SimpleTestCase-请参见下面的示例

TL;DR - Solution: Use SimpleTestCase - See example below

问题是Django为测试提供了推荐的测试类涉及数据库查询的TransactionTestCase和TestCase子类将每个测试包装在事务中,以加快每个测试后重置数据库的过程。来源: Django TransactionTestCase文档

The thing is that the recommended test classes provided by Django for tests involving database queries, TransactionTestCase and the subclass TestCase, wraps every test in a transaction to speed up the process of resetting the database after each test. Source: Django TransactionTestCase docs

可以通过使用SimpleTestCase来避免此行为,SimpleTestCase是TransactionTestCase的父类。然后,您必须通过将allow_database_queries设置为True来明确指定要允许数据库查询-

It is possible to avoid this behaviour by using SimpleTestCase which is the parent class of TransactionTestCase. You then have to specify explicitly that you want to allow database queries by setting allow_database_queries to True-

还请注意,在执行此操作之后,您需要负责所有清理工作考试。您可以通过重写tearDownClass类方法来实现。同样,在运行测试之前,有一个setUpClass类方法可用于任何初始化。记住要调用超级方法。 在文档中查看完整详情

Also note that you are then responsible for any cleaning that needs to be done after the test. You can do that by overriding the tearDownClass class method. Similarly there's a setUpClass class method for any initialization prior to running the test. Remember to call the super methods. See full details in the docs

from django.test import SimpleTestCase

class MyTestCase(SimpleTestCase):
    allow_database_queries = True

    @classmethod
    def setUpClass(cls):
        # Do your pre test initialization here.
        super(MyTestCase, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        # Do your post test clean uphere.
        super(MyTestCase, cls).tearDownClass()

    def test_add_data_through_api(self):
        # Add 'dat data
        ...

    def test_work_with_data_from_previous_test(self):
        # Work 'dat data
        ...

这篇关于Django Rest Framework:单元测试数据库问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆