我的自动化框架使用pytest设置/测试,而不是固定装置的拆卸式。我也有类的几个层次:pytest:安装/拆卸方法的过程中获取参数多态值
BaseClass
- 最高的,所有测试inhriet从中
FeatureClass
- 中,涉及到程序的所有测试功能,从它继承
TestClass
- 保持实际测试
编辑,为例子起见,我改变DB调用一个简单的打印
我想在所有设置/拆卸中添加数据库报告。即我想,一般BaseClass
setup_method
将创建测试DB项,teardown_method
将改变的结果的条目。我已经试过,但我似乎无法在运行时脱身方法的当前运行的测试值。甚至可能吗?如果不是,我该怎么办呢?
样品: (在base.py)
class Base(object):
test_number = 0
def setup_method(self, method):
Base.test_number += 1
self.logger.info(color.Blue("STARTING TEST"))
self.logger.info(color.Blue("Current Test: {}".format(method.__name__)))
self.logger.info(color.Blue("Test Number: {}".format(self.test_number)))
# --->here i'd like to do something with the actual test parameters<---
self.logger.info("print parameters here")
def teardown_method(self, method):
self.logger.info(color.Blue("Current Test: {}".format(method.__name__)))
self.logger.info(color.Blue("Test Number: {}".format(self.test_number)))
self.logger.info(color.Blue("END OF TEST"))
(在my_feature.py)
class MyFeature(base.Base):
def setup_method(self, method):
# enable this feature in program
return True
(在test_my_feature.py)
class TestClass(my_feature.MyFeature):
@pytest.mark.parametrize("fragment_length", [1,5,10])
def test_my_first_test(self):
# do stuff that is changed based on fragment_length
assert verify_stuff(fragment_length)
所以我怎样才能获得setup_method的参数,基本父类的测试框架的?
是否使用'unittest.TestCase'?然后,您可以将'setup'中的值设置为'self'并在测试中检索它。需要更多详细信息才能回答此问题。 – Arunmozhi
@Arunmozhi我已经添加了一些基本的代码,希望它会澄清什么,我试图做 –