小编Zde*_*axa的帖子

如果定义了构造函数,py.test将跳过测试类

我有跟随通过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)

python pytest

43
推荐指数
2
解决办法
2万
查看次数

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

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

  3. 在setup_method创建该数据保留在主数据库,不回滚如果数据创建调用是在做他们应该和他们是test_case方法

  4. 当测试单独运行时会发生此行为.在测试套件中运行时,setup_method db调用失败,并显示:Failed:不允许数据库访问,使用django_db标记启用虽然装饰器显然在那里(这意味着此错误消息不是100%可信的btw).

pytest是一个很棒的框架,如果数据库调用是从django_db标记的测试用例方法发生的,那么django-pytest很有用.

它看起来像没有数据库交互应该永远存在于特殊的pytest方法,如setup_method, …

python django unit-testing pytest pytest-django

6
推荐指数
1
解决办法
2846
查看次数

标签 统计

pytest ×2

python ×2

django ×1

pytest-django ×1

unit-testing ×1