习惯性伸手党区 请教:JAVA 的 testNg 有根据测试数据自动生成 Case 的方法吗?

leo · 2016年11月14日 · 840 次阅读

需求:根据测试数据自动生成 case

如:测试登录的时候,有

  • 用户名未输入
  • 密码未输入
  • 验证码未输入
  • 用户名不存在
  • 密码错误
  • 验证码错误

等操作步骤一致,但是验证点不一样的用例。这些用户名密码等都保存在 excel 或 yaml 文件中,自动化测试中动态从 excel 或 yaml 中获取测试数据生成一个 case,目前知道 Python 中可以通过 setattr 魔法方法实现,

python 下的实现方法:
# 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

问题:JAVA 的 testNg 中有同样的方法吗?

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册