我有跟随通过py.test运行的unittest代码.仅运行构造函数会在运行py.test -v -s时跳过整个类
收集0件/ 1跳过
任何人都可以向我解释py.test的这种行为吗?
我有兴趣了解py.test行为,我知道不需要构造函数.
谢谢,Zdenek
class TestClassName(object):
def __init__(self):
pass
def setup_method(self, method):
print "setup_method called"
def teardown_method(self, method):
print "teardown_method called"
def test_a(self):
print "test_a called"
assert 1 == 1
def test_b(self):
print "test_b called"
assert 1 == 1
Run Code Online (Sandbox Code Playgroud) 我在Ubuntu 14.04上进行了以下设置:
以下测试代码:
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
Run Code Online (Sandbox Code Playgroud)
虽然该类被标记为使用django数据库,但构造函数中创建的数据到达主(生产)数据库(名称取自settings.py)
将django_db装饰器放在上面setup_method并没有任何区别
在setup_method创建该数据保留在主数据库,不回滚如果数据创建调用是在做他们应该和他们是test_case方法
当测试单独运行时会发生此行为.在测试套件中运行时,setup_method db调用失败,并显示:Failed:不允许数据库访问,使用django_db标记启用虽然装饰器显然在那里(这意味着此错误消息不是100%可信的btw).
pytest是一个很棒的框架,如果数据库调用是从django_db标记的测试用例方法发生的,那么django-pytest很有用.
它看起来像没有数据库交互应该永远存在于特殊的pytest方法,如setup_method, …