Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.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 Pytest:在超类中运行所有测试_Python_Pytest - Fatal编程技术网

Python Pytest:在超类中运行所有测试

Python Pytest:在超类中运行所有测试,python,pytest,Python,Pytest,我想运行从另一个类继承的所有测试方法。有没有办法做到这一点?例如,对于下面的代码,我希望pytest测试继承自test的TestTwo和testseven的所有测试方法 类测试: 定义初始化(self,n): self.n=n def测试_偶数(自): 断言自身。n%2==0 def测试不大(自身): 断言自我。n

我想运行从另一个类继承的所有测试方法。有没有办法做到这一点?例如,对于下面的代码,我希望
pytest
测试继承自
test
TestTwo
testseven
的所有测试方法

类测试:
定义初始化(self,n):
self.n=n
def测试_偶数(自):
断言自身。n%2==0
def测试不大(自身):
断言自我。n<100
第二类测试(测试):
定义初始化(自):
super(TestTwo,self)。\uuuuu init\uuuuuuuu(2)
第十二类(测试):
定义初始化(自):
超级(testleven,self).\uuuu初始化(12)
但是,
pytest
抱怨存在一个
\uuuu init\uuu
构造函数。我想要一个绕过这个的方法


总体目标是使我的测试更加模块化,以便我可以在多个类上测试类似的问题。

重用测试方法的正确用法是使用。这是一个和一个


感谢@hoefling在评论中指出
\uuuuuu init\uuuu
不利于测试。

不要重写
\uuuuuuu init\uuuu
方法(这里是一个示例)。您试图实现的是测试参数化
pytest
提供了一种通过装饰器参数化测试的优雅方法。查看更多详细信息。