django-pytest setup_method database issue

喜欢而已 提交于 2019-12-05 06:08:14

Unfortunately, setup_X methods does not play nice with pytest fixtures. pytest-django's database setup is based on pytest fixtures, and therefore it does not work.

I recommend that you make your setup_method an autouse fixture, that requests the db fixture:

@pytest.mark.django_db
class TestExperiment(object):

    @pytest.fixture(autouse=True)
    def setup_stuff(self, db):
        Question.objects.create(question_text="How are you?")

    def test_something(self):
        assert Question.objects.filter(question_text="How are you?").exists()

The error message given by pytest-django is confusing and misleading, I've opened an issue to track/fix this: https://github.com/pytest-dev/pytest-django/issues/297

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!