所以我正在研究一些代码来帮助探索集合论。我也用py.test测试这个代码,我仍然相对缺乏经验(这也是我使用它的原因之一)。
所以现在我正在研究关系,我有所有这些关系从一个抽象关系类中继承(例如ReflexiveRelation,CommutativeRelation,TransitiveRelation等)。其中许多关系都有类似的测试,如下所示。我想强调这一点,这样我每次添加新关系时都不必重复这个过程。由于这种草率的安排,我已经遇到了错误。
我已经考虑了一些不同的方法,但是我不完全确定它们是否与py.test架构紧密相关。我想知道一个更有经验的py.test用户是否能够提供一些关于此的智慧的话(或者至少指向正确的手册阅读)。
class TestRelation:
__testing__ = "Relation"
def test_abstactness(self):
with pytest.raises(TypeError):
relation.Relation(np.equal, '=')
class TestReflexiveRelation(TestRelation):
def test_set_creation(self):
with pytest.raises(relation.FalseRelationError) as fre:
relation.ReflexiveSet(1)
assert fre.value.message == "Relation must be a ReflexiveRelation"
def test_subclassing(self):
assert issubclass(relation.ReflexiveRelation, relation.Relation),\
"ReflexiveRelation should be a subclass of Relation. It is not."
class TestCommutativeRelation:
def test_set_creation(self):
with pytest.raises(relation.FalseRelationError) as fre:
relation.CommutativeSet(1)
assert fre.value.message == "Relation must be a CommutativeRelation"
def test_subclassing(self):
assert issubclass(relation.CommutativeRelation, relation.Relation),\
"ReflexiveRelation should be a subclass of Relation. It is not."
答案 0 :(得分:2)
我认为这些参数化灯具就是答案。谢谢@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__