简体   繁体   中英

Django/Graphene seems to clean database between individual tests in a test run

I'm testing Django/Graphene to see if it can fulfill what I need, but I'm getting trouble in unit testing.

I'm using the in-memory SQLite db, with the following code (tests.py):

import json

from graphene_django.utils.testing import GraphQLTestCase

from graphene.test import Client

from users.schema import UserType

class APITestCase(GraphQLTestCase):
    def test01_query_users(self):
        print("Running test01_query_users")
        response = self.query(
            '''
            query {
                users {
                    id
                    username
                    email
                }
            }
            '''
        )
        print (response)
        content = json.loads(response.content)
        self.assertResponseNoErrors(response)
        print (content)
        assert content == {
            "data": {
                "users": []
            }
        }

    def test02_mutation_addUser(self):
        print("Running test02_mutation_addUser")
        response = self.query(
            '''
            mutation {
                createUser (username: "testuser", email: "testemail@testserver.com", password: "123456") {
                    user {
                        id
                        username
                        email
                    }
                }
            }
            '''
        )
        print (response)

        content = json.loads(response.content)
        self.assertResponseNoErrors(response)
        print (content)
        
        assert content == {
            "data": {
                "createUser": {
                    "user": {
                        "id": "1",
                        "username": "testuser",
                        "email": "testemail@testserver.com"
                    }
                }
            }
        }

    def test03_mutation_addUser(self):
        print("Running test03_mutation_addUser")
        response = self.query(
            '''
            mutation {
                createUser (username: "testuser2", email: "testemail2@testserver.com", password: "123456") {
                    user {
                        id
                        username
                        email
                    }
                }
            }
            '''
        )
        print (response)

        content = json.loads(response.content)
        self.assertResponseNoErrors(response)
        print (content)
        
        assert content == {
            "data": {
                "createUser": {
                    "user": {
                        "id": "2",
                        "username": "testuser2",
                        "email": "testemail2@testserver.com"
                    }
                }
            }
        }

    def test04_query_users(self):
        print("Running test04_query_users")
        response = self.query(
            '''
            query {
                users {
                    id
                    username
                    email
                }
            }
            '''
        )
        print (response)
        content = json.loads(response.content)
        self.assertResponseNoErrors(response)
        print (content)
        assert content == {
            "data": {
                "users": []
            }
        }

The test output is as follows:

Using existing test database for alias 'default'...
System check identified no issues (0 silenced).
Running test01_query_users
<HttpResponse status_code=200, "application/json">
{'data': {'users': []}}
.Running test02_mutation_addUser
<HttpResponse status_code=200, "application/json">
{'data': {'createUser': {'user': {'id': '1', 'username': 'testuser', 'email': 'testemail@testserver.com'}}}}
.Running test03_mutation_addUser
<HttpResponse status_code=200, "application/json">
{'data': {'createUser': {'user': {'id': '1', 'username': 'testuser2', 'email': 'testemail2@testserver.com'}}}}
FRunning test04_query_users
<HttpResponse status_code=200, "application/json">
{'data': {'users': []}}
.
======================================================================
FAIL: test03_mutation_addUser (polls.tests.tests.APITestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\myuser\Projects\Python\virtual-environments\myproject\tests\tests.py", line 89, in test03_mutation_addUser
    assert content == {
AssertionError

----------------------------------------------------------------------
Ran 4 tests in 0.980s

FAILED (failures=1)

It runs first test just ok, then the 2nd one ok, adding a user. When it runs the 3rd it returns a different user with the same ID of the previous one, as if the previous mutation's results was wiped out from the DB, failing because the expected ID (2) is not met. When the 4th test is executed it shows the DB has 0 users. The expected result should be a DB with two different users. Why it seems to wipe the DB after each test? What am I doing wrong? When I pass --keepdb it doesn't store the DB anywhere.

The tests must be isolated from each other, and they are by default.

Django wraps every test in a transaction that is rolled back after the test executes, so the data is not visible outside that test method. You should neither rely on any other test side-effects, nor on the order of their execution (ie tests can be run in parallel).

So, you have to set up the test data for test04_query_users specifically inside this method. Create some users via ie User.objects.create() or some library like Factory Boy and then query and assert for them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM