生菜看起來像是一個非常好的用於Django應用程序的BDD測試框架;但是,我還沒有找到任何示例或文檔如何使用它來測試模型。有什麼可用的嗎?用生菜測試Django模型?
1
A
回答
0
嗯,我正在尋找相同的,但找不到任何適當的文檔或教程。 只是爲了測試模型上的驗證和檢查關係的完整性,我試着放置並從場景中提取值並驗證它們。多數驗證可能需要我從模型驗證我猜。
0
似乎這篇文章是在一段時間之前,雖然它是我的結果的頂部,所以這裏是我的發現。
生菜有@before.runserver
和@after.runserver
Django的裝飾器,可用於在terrain.py文件中實現測試數據庫。
我正在使用SQLite數據庫爲這個例子,我也使用南[:(]所以我有一個額外的測試,以確保SOUTH_TESTS_MIGRATE
已被設置爲False
。我有一個local_settings_test.py文件,覆蓋設置與具體到我的測試情況下,那些與收穫命令,像這樣打電話:
python manage.py harvest --settings=local_settings_test
這裏是我的設置和破壞調用當然你也可以與其他裝飾實現這些,如果你更願意復位。例如數據庫中的每個功能,場景或步驟,請參閱http://lettuce.it/reference/terrain.html瞭解有關可用功能的更多信息。
from lettuce import *
from django.conf import settings
from django.core.management.base import CommandError
from django.core.management import call_command
def assert_test_database():
"""
Raises a CommandError in the event that the database name does not contain
any reference to testing.
Also checks South settings to ensure migrations are not implemented.
"""
if not '-test' in settings.DATABASES['default']['NAME']:
raise CommandError('You must run harvest with a test database')
if getattr(settings, 'SOUTH_TESTS_MIGRATE', True):
raise CommandError('SOUTH_TESTS_MIGRATE should be set to False')
@before.runserver
def create_database(server):
"""
Asserts the database name is correct and creates initial structure, loading
in any test_data fixtures which may have been created.
"""
assert_test_database()
call_command('syncdb', interactive=False, verbosity=0)
call_command('loaddata', 'test_data', interactive=False, verbosity=0)
@after.runserver
def flush_database(server):
"""
Asserts the database name is correct and flushes the database.
"""
assert_test_database()
call_command('flush', interactive=False, verbosity=0)
一旦你在你的地形文件添加了這些步驟,你可以,你會在單元測試中調用模型的步驟。
相關問題
- 1. Django模型關係測試
- 2. Django模型單元測試
- 3. django模型單元測試
- 4. Django:測試抽象模型
- 5. Django測試模型屬性
- 6. 使用FileField測試Django模型
- 7. 用一個測試用例測試所有的django模型
- 8. 使用外鍵測試django模型到另一個模型
- 9. 測試Django加上芹菜
- 10. 在Django中測試特定模型
- 11. 測試的Django模型清潔方法
- 12. Django - 測試模型字段更改
- 13. 測試Django和模型方法
- 14. 測試在Django模型「類元」
- 15. 內部測試模型 - Django 1.7問題
- 16. 如何測試django模型表單?
- 17. 在測試時訪問Django模型
- 18. Django的芹菜單元測試pycharm「無模塊名爲芹菜」
- 19. 用django測試設置生菜的錯誤
- 20. rspec在生成模型時未創建測試模型
- 21. django和硒生菜測試沒有在窗戶上運行
- 22. Django模型:動態生成模型場
- 23. 測試arima模型
- 24. Rails模型測試
- 25. Sailjs測試模型
- 26. Django模塊化測試
- 27. Django的模型派生類
- 28. 從xml生成django模型
- 29. 在Django中進行內容類型模型的單元測試
- 30. 一起使用django-nose和django-芹菜 - 單元測試