如:测试登录的时候,有
等操作步骤一致,但是验证点不一样的用例。这些用户名密码等都保存在 excel 或 yaml 文件中,自动化测试中动态从 excel 或 yaml 中获取测试数据生成一个 case,目前知道 Python 中可以通过 setattr 魔法方法实现,
# coding:utf-8
import unittest
class TestDemo(unittest.TestCase):
def setUp(self):
print("This is setup!")
def demo_func(self, i):
print(i)
def tearDown(self):
print("This is tearDown!")
def get_demo_func(i):
def func(self):
self.demo_func(i)
return func
def __test_demo():
for i in range(5):
setattr(TestDemo, "test_demo_no_%s" % i, TestDemo.get_demo_func(i))
__test_demo()
if __name__ == "__main__":
unittest.main()
This is setup!
0
This is tearDown!
.This is setup!
1
This is tearDown!
.This is setup!
2
This is tearDown!
.This is setup!
3
This is tearDown!
.This is setup!
4
This is tearDown!
.
----------------------------------------------------------------------
Ran 5 tests in 0.031s
OK