How to test a class' inherited methods in pytest

空扰寡人 提交于 2019-12-01 19:28:09

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()

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.

You can use pytest parameterization to pass multiple arguments to the same test, in this case, the argument would most likely be the class being tested.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!