我想使用来自 postgres localhost 数据库的数据(已经加载的数据)来测试我的视图。我将 tox 与 pytest 和 pytest-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
- 毒物==2.6.0