如何在django中使用数据库运行测试数据? [英] How to run tests in django using database with data?
问题描述
我想使用postgres localhost数据库中的数据(已加载数据)来测试我的视图。我使用pytest和pytest-django的毒素。
我的问题:
如何设置/连接到本地数据库以获取所有的数据模型模式和数据本身?或者也许最好使用factory_boy?或者从.sql脚本加载整个数据(如果是,如何)?
我的测试示例:
<$ pre $
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等。
答案是在 pytest-django docs 在第5章 - >示例 - >使用只读数据库。
检查其他例子,在这种情况下真的很方便。
谢谢!
I want to test my views using data from postgres localhost database (with already loaded data). I'm using tox with pytest and pytest-django.
My question: How to set up / connect to local database to get all the data model schema and data itself? Or maybe it is better to use factory_boy? Or to load whole data from .sql script (if yes, how)?
Example of my test:
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
But instead of getting status code 200 I get 404, which points that no data is in test database. But when I lunch runserver
and go to that view ('localhost:8000/foo/bar/123/')
I will get status 200 and html webpage with some data.
Please help!
I'm using:
- Django==1.7.11
- pytest==3.0.6
- pytest-django==3.1.2
- tox==2.6.0
Just found a way! It was simpler then I thought! Without writing any custom TestRunners etc.
The answer is in pytest-django docs in Chapter 5 -> Examples -> Use a read only database.
Check out other examples, which are really handy in such situations.
Thanks!
这篇关于如何在django中使用数据库运行测试数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!