如何在pytest中测试类的继承方法 [英] How to test a class' inherited methods in pytest

查看:479
本文介绍了如何在pytest中测试类的继承方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

house.py:

class House:
    def is_habitable(self):
        return True

    def is_on_the_ground(self):
        return True

conftest.py:

import pytest
from house import House


@pytest.fixture(scope='class')
def house():
    return House()

test_house.py:

class TestHouse:
    def test_habitability(self, house):
        assert house.is_habitable()

    def test_groundedness(self, house):
        assert house.is_on_the_ground()

到目前为止,一切都在测试中.

Up to that point, everything is being tested.

现在我添加一个子类并覆盖house.py中的一个方法:

Now I add a subclass and override a method in house.py:

class House:
    def is_habitable(self):
        return True

    def is_on_the_ground(self):
        return True


class TreeHouse(House):
    def is_on_the_ground(self):
        return False

我还在 conftest.py 中为该类添加了一个新装置:

I also add a new fixture for that class in conftest.py:

import pytest
from house import House
from house import TreeHouse


@pytest.fixture(scope='class')
def house():
    return House()


@pytest.fixture(scope='class')
def tree_house():
    return TreeHouse()

我在 test_house.py 中为树屋添加了一个新的测试类:

I add a new test class for tree house in test_house.py:

class TestHouse:
    def test_habitability(self, house):
        assert house.is_habitable()

    def test_groundedness(self, house):
        assert house.is_on_the_ground()


class TestTreeHouse:
    def test_groundedness(self, tree_house):
        assert not tree_house.is_on_the_ground()

此时,代码可以工作,但有些情况没有经过测试.例如,为了完整起见,我需要再次测试从 TreeHouse 中的 House 继承的方法.

At that point, the code works, but there are cases that are not tested. For example, to be complete, I would need to test again the methods inherited from House in TreeHouse.

TestHouse 重写相同的测试不会是 DRY.

Rewriting the same tests from the TestHouse would not be DRY.

如何在不重复代码的情况下测试TreeHouse(在本例中为is_habitable)的继承方法?

How do I test the inherited method of TreeHouse (in this case is_habitable) without duplicating code?

我想用与它的超类相同的测试重新测试 TreeHouse 之类的东西,但不是针对新的或被覆盖的方法/属性.

I would like something like re-testing the TreeHouse with the same tests that its super class runs through, but not for the methods / properties that are new or overridden.

经过一些研究,我发现了矛盾的来源.在深入研究 pytest 文档后,我无法理解适用于这种情况的内容.

After some research I came across contradictory sources. And after digging in the pytest documentation I fail to understand what applies to this scenario.

我对 pytest 方法很感兴趣.请参考文档并解释如何在此处应用.

I am interested in the pytest way to do this. Please reference the docs and explain how that applies here.

推荐答案

一种方法是对所有测试方法使用夹具名称 house(即使它正在测试一个 TreeHouse) 和 覆盖其值在每个测试上下文中:

One way to do this would be to use the fixture name house for all test methods (even if it's testing a TreeHouse), and override its value in each test context:

class TestTreeHouse(TestHouse):
    @pytest.fixture
    def house(self, tree_house):
        return tree_house

    def test_groundedness(self, house):
        assert not house.is_on_the_ground()

还要注意 TestTreeHouse 继承自 TestHouse.由于 pytest 只是枚举类的方法(即没有使用 @pytest.test() 装饰器完成注册"),TestHouse 中定义的所有测试都将在它的子类中发现,无需任何进一步干预.

Also note TestTreeHouse inherits from TestHouse. Since pytest merely enumerates methods of classes (i.e. there is no "registration" done with, say, a @pytest.test() decorator), all tests defined in TestHouse will be discovered in subclasses of it, without any further intervention.

这篇关于如何在pytest中测试类的继承方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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