51Testing软件测试论坛
标题:
python ddt 数据驱动测试
[打印本页]
作者:
测试积点老人
时间:
2018-12-12 14:15
标题:
python ddt 数据驱动测试
# -*- coding: utf-8 -*-
import unittest
from ddt import ddt, data, unpack
import csv
from collections import namedtuple
def add(a,b):
c = a+b
return c
def addstr(a,b):
c = a + b
return c
def get_csv_data():
value_rows = []
with open('./mfile.csv') as f:
f_csv = csv.reader(f)
next(f_csv)
for r in f_csv:
value_rows.append(r)
return value_rows
@ddt
class Test(unittest.TestCase):
@data((1,1,2), (1,2,3))
@unpack
def test_addnum(self,a, b, expected_value):
self.assertEqual(add(a,b),expected_value)
@data(*get_csv_data())
@unpack
def test_addstr(self,a,b,expected_value):
self.assertEqual(addstr(a,b), expected_value)
if __name__ == "__main__":
suite = unittest.TestLoader().loadTestsFromTestCase(Test)
unittest.TextTestRunner(verbosity=2).run(suite)
csv文件:
value1,value2,result
aa,bb,aabb
ww,r,wwr
111,222,111222
1,2,3
复制代码
运行结果:
test_addnum_1__1__1__2_ (__main__.Test) ... ok
test_addnum_2__1__2__3_ (__main__.Test) ... ok
test_addstr_1___aa____bb____aabb__ (__main__.Test) ... ok
test_addstr_2___ww____r____wwr__ (__main__.Test) ... ok
test_addstr_3___111____222____111222__ (__main__.Test) ... ok
test_addstr_4___1____2____3__ (__main__.Test) ... FAIL
复制代码
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2