![](/img/trans.png)
[英]How to run django tests in Eclipse to make debugging possible, but on test database
[英]Django/Graphene seems to clean database between individual tests in a test run
我正在測試 Django/Graphene,看看它是否能滿足我的需求,但我在單元測試中遇到了麻煩。
我正在使用內存中的 SQLite 數據庫,其中包含以下代碼 (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": []
}
}
測試輸出如下:
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)
它運行第一個測試正常,然后第二個測試正常,添加用戶。 當它運行第 3 次時,它返回一個與前一個 ID 相同的不同用戶,就好像先前突變的結果已從數據庫中刪除一樣,由於未滿足預期 ID (2) 而失敗。 執行第 4 個測試時,它顯示 DB 有 0 個用戶。 預期結果應該是具有兩個不同用戶的數據庫。 為什么每次測試后似乎都會擦除數據庫? 我究竟做錯了什么? 當我通過 --keepdb 時,它不會將數據庫存儲在任何地方。
測試必須彼此隔離,默認情況下它們是隔離的。
Django 將每個測試包裝在一個事務中,該事務在測試執行后回滾,因此數據在該測試方法之外是不可見的。 您不應該依賴任何其他測試副作用,也不應該依賴它們的執行順序(即測試可以並行運行)。
因此,您必須在此方法中專門為test04_query_users
設置測試數據。 通過 ie User.objects.create()
或像Factory Boy
這樣的庫創建一些用戶,然后為他們查詢和斷言。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.