【问题标题】:pytest django TestCase give strange failures with `--pdb`pytest django TestCase 使用 `--pdb` 给出奇怪的失败
【发布时间】:2018-01-24 23:35:37
【问题描述】:

我有一个非常简单的类,它在任何版本的 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
  • Python 2.7.11
  • pytest-3.3.1
  • py-1.5.2
  • pluggy-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.0pluggy-0.3.1,代码将毫无问题地执行。我的问题是,这是怎么回事?这可能是什么原因造成的?

好像 pytest 正在调用 test_that_client_exists,但没有调用 __call__,后者调用了 _pre_setup

有人见过这样的吗?

【问题讨论】:

  • 测试使用干净的 django 项目(Python 2.7.10、django==1.11.9pytest==3.3.2pytest-django==3.1.2)运行。那里一定有别的东西。
  • 所以如果我从测试调用中删除--pdb,这个问题就会消失。

标签: django pytest pdb pytest-django


【解决方案1】:

如果我没记错的话,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

【讨论】:

  • 感谢您的回复,不幸的是,这是一个包含许多此类类的共享代码库,我希望有人能阐明根本原因。为简单起见,我将它分解为我可以创建的最简单的类来举例说明这个问题。
  • 我不确定我能提供更多帮助。我唯一的建议是看看 pytest-django 是否有帮助:pytest-django.readthedocs.io/en/latest/tutorial.html
猜你喜欢
  • 2019-07-23
  • 2020-03-12
  • 1970-01-01
  • 2021-02-08
  • 2019-04-21
  • 2016-11-15
  • 1970-01-01
  • 1970-01-01
  • 2018-12-07
相关资源
最近更新 更多