[英]How to avoid duplicating test cases in Django?
我已經將測試用例寫在兩個單獨的測試文件中(例如test_1和test_2)。 在我測試模型的兩個測試案例中,由於過程相似,我都有代碼重復。
例如,我需要登錄用戶並測試證書。
代碼示例:
import test_data
from django.test import TestCase
from UserData.models import MyModel
from django.contrib.auth.models import User
class UserDataMyModelTestCalls(TestCase):
@classmethod
def setUpTestData(cls):
cls.test_user = User.objects.create_user(test_data.test_user_data['user_name'],
test_data.test_user_data['email'],
test_data.test_user_data['password'])
def test_faulty_login_credentials(self):
self.client.login(username=test_data.faulty_user_data['user_name'], password=test_data.faulty_user_data['password'])
response = self.client.get('/userdata/mymodelurl/', {})
self.assertEqual(response.status_code, 403)
我正在使用帶有用戶憑據的單獨文件來避免重復。
test_data文件樣本:
test_user_data = {'id': u'1',
'user_name': 'tempUsername',
'password': 'tempPassword',
'email': 'tempEmaily@test.com'}
更新:添加要用作所有測試用例的通用類的UserTests類。 我正在定義並通過test_1.py調用測試,如下所示:
import UserTests
from django.test import TestCase
class UserDataWayPointTestCalls(TestCase):
testCasesObject = UserTests.UserDataTestCalls()
test_user = testCasesObject.setUpTestData()
response = testCasesObject.test_faulty_login_credentials()
我的UserDataTestCalls類的定義如下:
import test_data
from django.test import Client
from django.test import TestCase
from django.contrib.auth.models import User
class UserDataTestCalls(TestCase):
def __init__(self):
self.test_user = None
self.faulty_login_response = None
def setUpTestData(self):
self.client = User.objects.create_user(test_data.test_user_data['user_name'],
test_data.test_user_data['email'],
test_data.test_user_data['password'])
self.client = Client()
return self.client
def test_faulty_login_credentials(self):
self.client.login(username=test_data.faulty_user_data['user_name'],
password=test_data.faulty_user_data['password'])
response = self.client.get('/userdata/mymodelurl/', {})
return response
當我執行上面的代碼時,我收到IntegrityError: (1062, "Duplicate entry 'tempUsername' for key 'username'")
。 暫時我修改username
值以繼續進行操作,並收到以下錯誤AttributeError: 'UserDataTestCalls' object has no attribute '_testMethodName'
。
我試圖用名稱創建一個單獨的類,例如UserDataTestCalls,並包括測試用例的常見部分,例如User.objects.create_user
, self.client.login
等。
不幸的是,盡管它說Destroying test database for alias 'default'...
但我最終還是得到了數據庫錯誤Destroying test database for alias 'default'...
在下一次運行時,我得到了用戶名重復,例如Duplicate entry 'tempUsername' for key 'username'
等...
當我嘗試通過更改用戶名以進行測試來解決此問題時,我遇到了另一個問題'NoneType' object has no attribute 'login'
。
它指出self.client
變量未與我正在創建的test_user綁定。
我試圖在線搜索並找到有關如何解決問題的文檔,但是所有文檔都指向針對您的測試分別使用單獨的腳本,如果您有不同的測試用例,我可以理解。 就我而言,我的測試用例中有90%完全相同。
因此,我確定有一種方法可以在單獨的類中創建用戶,並在該類中也創建所有測試用例,因此我可以在需要時從單獨的測試文件中調用它們。
有人可以為我指出正確的方向,還是提供一些可以閱讀的示例/文檔的鏈接?
預先感謝您的時間和精力。
嘗試創建一個通用的測試類。
class CreateUserTestCase(TestCase):
def setUpTestData(self):
self.user = User.objects.create_user(
test_data.test_user_data['user_name'],
test_data.test_user_data['email'],
test_data.test_user_data['password'],
)
您要將新用戶分配給self.user
。 不要替換應為測試客戶端而不是用戶的self.client
。 您不需要做self.client = Client()
,Django測試用例將為您解決這一問題。
然后對測試用例進行子類化並添加測試。
class UserDataTestCalls(CreateUserTestCase):
def test_faulty_login_credentials(self):
self.client.login(
username=test_data.faulty_user_data['user_name'],
password=test_data.faulty_user_data['password'],
)
response = self.client.get('/userdata/mymodelurl/', {})
return response
從您的問題來看,我不確定每個類的test_data
是否不同。 如果是這樣,則必須對此稍作更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.