4
我想使用來自postgres localhost數據庫的數據(已經加載數據)來測試我的視圖。我用pytest和pytest-django來使用tox。如何在數據庫中使用數據在django中運行測試?
我的問題: 如何設置/連接到本地數據庫以獲取所有數據模型模式和數據本身?或者,最好使用factory_boy?或者從.sql腳本加載整個數據(如果是,如何)?我測試的
例子:
def test_foo_view(custom_client_login):
response = custom_client_login.get('/foo/bar/123/')
assert response.status_code == 200
assert 'Transaction no. 123' in response.content
而是獲得狀態代碼200,我得到404,這點沒有數據的測試數據庫。但是當我午餐runserver
並去那個視圖('localhost:8000/foo/bar/123/')
我會得到狀態200和HTML網頁與一些數據。
請幫忙!
我使用:
- Django的== 1.7.11
- pytest == 3.0.6
- pytest,Django的== 3.1.2
- TOX == 2.6.0
使用測試運行器設置(def setUp)並創建一個站點以在您的視圖中使用測試 – Jingo
@Jingo您能否詳細說明一下您的答案? 不是'def setUp'使用unittest? – MichalTHEDUDE
https://docs.djangoproject.com/en/1.10/topics/testing/overview/看看 – Jingo