如何 运行 在 django 中使用带有数据的数据库进行测试?
How to run tests in django using database with data?
我想使用来自 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
- tox==2.6.0
找到方法了!这比我想的要简单!无需编写任何自定义 TestRunners 等
答案在第 5 章 pytest-django docs -> 示例 -> 使用只读数据库中。
查看其他示例,这些示例在这种情况下非常有用。
谢谢!
我想使用来自 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
- tox==2.6.0
找到方法了!这比我想的要简单!无需编写任何自定义 TestRunners 等
答案在第 5 章 pytest-django docs -> 示例 -> 使用只读数据库中。
查看其他示例,这些示例在这种情况下非常有用。
谢谢!