我最近修改了我的Django項目的設置,因爲當我運行測試代替PostgreSQL時,它使用SQLite3,以便我的測試運行得更快。但是,一旦我這樣做了,我的一個負面單元測試將檢查以驗證Django將在我嘗試創建用戶名超過30個字符的用戶開始失敗時發出錯誤。直到這個時候,測試總是通過。SQLite3沒有捕捉太長的用戶名
如果我使用PostgreSQL運行以下測試,會引發異常(特別是DatabaseError)並通過測試。但是,如果我使用SQLite3,則不會引發異常,並且測試失敗。我仔細檢查了SQLite3 auth_user模式並確認用戶名是varchar(30)字段。有沒有其他人經歷過這個?謝謝。
from django.test import TestCase
from django.contrib.auth.models import User
class SimpleTest(TestCase):
def test_simple(self):
exception_raised = True # Exception should be raised since username > 30 chars
try:
user = User.objects.create_user(username='testusertestusertestusertestuser')
except Exception as e:
exception_raised = False
assert not exception_raised, "Exception wasn't raised"