Good afternoon.

I'm testing api based on django-rest-framework using pytest. As far as I knew at the beginning of the test, py.test creates a duplicate database with the prefix test_. But the pattern during writing tests noticed that it is not receiving data from the database. That is a duplicate of this supposedly empty. A simple example:

@pytest.mark.django_db
def test_db():
    qs = Category.objects.get(id=4)
    assert qs['id'] = 4

Its returns this error below, but in a database, object with id=4 exists.

>       assert qs['id'] == 4
E       assert [] == 4

I am new to testing, may miss something, help please.

有帮助吗?

解决方案

Like Daniel commented, test DB is initially empty. You can add any data you want for testing by using one of two methods:

  1. Django Fixtures - You can add data using a JSON file just like initial data.
  2. Mock data using a library such as django dynamic fixture during run time.

These are your best bets.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top