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