django-pytest setup_method数据库问题 [英] django-pytest setup_method database issue

查看:468
本文介绍了django-pytest setup_method数据库问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ubuntu 14.04中设置了以下设置:




  • python 2.7.6

  • django 1.7 [虽然我用
    django 1.9再现了同样的行为]

  • pytest-django 2.8.0 [也用2.9.1测试]

  • pytest 2.7.2 [也用2.8.3测试]



以下测试代码:

  import pytest 
from django.db import connection

import settings
from pollsapp.models import问题

original_db_name = settings.DATABASES [default] [NAME]

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

def setup_method(self,method):
#没有使用test_+ DATABASE_NAME!
assert connection.settings_dict [NAME] == \
settings.DATABASES [default] [NAME]
Question.objects.create(question_text =你好吗?)
#此数据保留在主数据库




  1. 虽然该类被标记为使用django数据库,但构造函数中创建的数据到达主(生产)数据库(名称取自settings.py)


  2. django_db 装饰器放在 setup_method 之上没有任何区别


  3. 在setup_method中创建的此数据保留在主数据库中,不会按原样回滚,如果在中进行数据创建调用, test_case 方法


  4. 当测试自己运行时,会发生此行为。当在测试套件中运行它时,setup_method数据库调用失败:失败:不允许数据库访问,使用 django_db 标记来启用
    虽然装饰器是明确的那里(这意味着这个错误信息不能被100%信任btw)。


pytest是一个非常棒的框架如果数据库调用发生在 django_db 标记的测试用例方法中,那么django-pytest将会很好。



看起来没有db应该在特殊的pytest方法中存在交互,例如 setup_method teardown_method 等。虽然文档没有说任何事情:



https://pytest-django.readthedocs.org/en/latest/database.html



我正在使用Django 1.7以及与1.9(最新稳定)。



这是整个链接测试模块: https://github.com/ zdenekmaxa / examples / blob / master / python / django-testing / tests / pytest_djangodb_only.py

解决方案

setup_X方法与pytest灯具不兼容。 pytest-django的数据库设置是基于pytest灯具,因此它不起作用。



我建议您将setup_method设置为一个自动灯具,它要求db灯具:

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

@pytest .fixture(autouse = True)
def setup_stuff(self,db):
Question.objects.create(question_text =你好吗?)

def test_something )
assert Question.objects.filter(question_text =你好吗?)。exists()

pytest-django提供的错误信息令人困惑和误导,我已经打开了一个问题来跟踪/修复这个问题: https://github.com/pytest-dev/pytest-django/issues/297


I have the following set up on Ubuntu 14.04:

  • python 2.7.6
  • django 1.7 [though I reproduced the same behaviour with django 1.9 too]
  • pytest-django 2.8.0 [also tested with 2.9.1]
  • pytest 2.7.2 [also tested with 2.8.3]

And the following test code:

import pytest
from django.db import connection

import settings
from pollsapp.models import Question

original_db_name = settings.DATABASES["default"]["NAME"]

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

    def setup_method(self, method):
        # it's not using the "test_" + DATABASE_NAME !
        assert connection.settings_dict["NAME"] == \ 
        settings.DATABASES["default"]["NAME"]
        Question.objects.create(question_text="How are you?")
        # this data remains in the main database

  1. Although the class is marked to use django database, the data created in the constructor arrive in the main (production) database (name taken from settings.py)

  2. Putting the django_db decorator above the setup_method does not make any difference

  3. This data created in the setup_method remain in the main database, are not rolled back as they should be and as they would be if the data creation call was made in the test_case method

  4. This behaviour happens when the test is run on its own. When running it within the test suite, the setup_method db calls fails with: Failed: Database access not allowed, use the django_db mark to enable although the decorator is clearly there (which means that this error message is not to be 100% trusted btw).

pytest is an awesome framework and django-pytest works great if database calls happen from django_db marked test case methods.

It looks like no db interaction should ever be present in special pytest methods such as setup_method, teardown_method, etc. Although the documentation doesn't say anything about it:

https://pytest-django.readthedocs.org/en/latest/database.html

I am getting this behaviour with both Django 1.7 as well as with 1.9 (latest stable).

Here is the link to the whole test module: https://github.com/zdenekmaxa/examples/blob/master/python/django-testing/tests/pytest_djangodb_only.py

解决方案

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

这篇关于django-pytest setup_method数据库问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆