문제

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