1

我有一个非常简单的类,它在任何版本的 pytest>3.0.0 上都失败了。当我使用--pdb.

from django.test import TestCase


class TestTestCase(TestCase):
    """Tests for the TestCase class."""

    def test_that_client_exists(self):
        """Assert that the class has a client."""
        assert self.client

我正在使用以下版本:

  • 平台 Linux
  • 蟒蛇 2.7.11
  • pytest-3.3.1
  • py-1.5.2
  • 插件-0.6.0
  • django-2.9.2

我收到以下错误:

self = <myproject.tests.test_test_case.TestTestCase testMethod=test_that_client_exists>

    def test_that_client_exists(self):
        """Assert that the class has a client."""
>       assert self.client
E       AttributeError: 'TestTestCase' object has no attribute 'client'

但是,如果我降级到pytest==3.0.0and pluggy-0.3.1,代码将毫无问题地执行。我的问题是,这是怎么回事?这可能是什么原因造成的?

就好像 pytest 正在调用 ,test_that_client_exists但没有调用__call__which 调用_pre_setup

有没有人见过这样的事情?

4

1 回答 1

0

如果我没记错的话,pytest 不会按照预期调用标准 django 测试的方式设置类。

我会删除 TestCase 继承并手动添加客户端。

from django.test import Client


class TestTestCase():
    """Tests for the TestCase class."""

    client = Client()

    def test_that_client_exists(self):
        """Assert that the class has a client."""
        assert self.client
于 2018-01-24T23:53:37.103 回答