Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 关于烘干的建议?_Python_Inheritance_Dry_Pytest - Fatal编程技术网

Python 关于烘干的建议?

Python 关于烘干的建议?,python,inheritance,dry,pytest,Python,Inheritance,Dry,Pytest,所以我正在编写一些代码来帮助探索集合论。我也在用py.test测试这段代码,我对py.test还相对缺乏经验(这也是我使用它的部分原因) 所以现在我正在研究关系,我有所有这些关系,它们都是抽象关系类的子类(例如自反关系、交换关系、传递关系等等)。如下文所示,这些关系中的许多都有类似的检验。我想浓缩一下,这样我就不必每次添加新关系时都重复这个过程。由于这种草率的安排,我已经遇到了错误 我已经考虑过一些不同的方法来实现这一点,但我不能完全确定它们是否与py.test体系结构相一致。我想知道一个更有经

所以我正在编写一些代码来帮助探索集合论。我也在用py.test测试这段代码,我对py.test还相对缺乏经验(这也是我使用它的部分原因)

所以现在我正在研究关系,我有所有这些关系,它们都是抽象关系类的子类(例如自反关系、交换关系、传递关系等等)。如下文所示,这些关系中的许多都有类似的检验。我想浓缩一下,这样我就不必每次添加新关系时都重复这个过程。由于这种草率的安排,我已经遇到了错误

我已经考虑过一些不同的方法来实现这一点,但我不能完全确定它们是否与py.test体系结构相一致。我想知道一个更有经验的py.test用户是否能为我提供一些关于这方面的建议(或者至少给我指出正确的手册阅读)


我认为这些参数化装置就是答案。谢谢@jme

 test_relations = [
        (relation.ReflexiveRelation, relation.ReflexiveSet),
        (relation.TransitiveRelation, relation.TransitiveSet),
        (relation.CommutativeRelation, relation.CommutativeSet)
    ]

    class TestRelation:
        __testing__ = "Relation"

        def test_abstactness(self):
            with pytest.raises(TypeError):
                relation.Relation(np.equal, '=')

        @pytest.mark.parametrize("ARelation, ASet", test_relations)
        def test_incorrect_set_creation(self, ARelation, ASet):
            with pytest.raises(relation.FalseRelationError) as fre: 
                ASet(1)
            assert fre.value.message == "Relation must be a " + ARelation.__name__

您可能需要研究参数化装置:是的,这看起来很有希望。我以前看过,但显然不够仔细。我现在发布答案(或者我认为应该是答案……我们拭目以待)。
 test_relations = [
        (relation.ReflexiveRelation, relation.ReflexiveSet),
        (relation.TransitiveRelation, relation.TransitiveSet),
        (relation.CommutativeRelation, relation.CommutativeSet)
    ]

    class TestRelation:
        __testing__ = "Relation"

        def test_abstactness(self):
            with pytest.raises(TypeError):
                relation.Relation(np.equal, '=')

        @pytest.mark.parametrize("ARelation, ASet", test_relations)
        def test_incorrect_set_creation(self, ARelation, ASet):
            with pytest.raises(relation.FalseRelationError) as fre: 
                ASet(1)
            assert fre.value.message == "Relation must be a " + ARelation.__name__