6

我想使用来自 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
4

1 回答 1

4

刚刚找到方法了!它比我想象的要简单!无需编写任何自定义 TestRunner 等。

答案在第 5 章 -> 示例 -> 使用只读数据库的pytest-django 文档中。

查看其他示例,这些示例在这种情况下非常方便。

谢谢!

于 2017-03-03T13:33:36.160 回答