import unittest
# 求 和
def add(x, y): return x + y
class TestAdd(unittest.TestCase): def test_add_01(self):
result = add(1, 1) self.assertEqual(result, 2)
def test_add_02(self): result = add(1, 0)
self.assertEqual(result, 1)
def test_add_03(self): result = add(0, 0)
self.assertEqual(result, 0)
def test_add(self):
test_data = [(1, 1, 2), (1, 0, 1), (0, 0, 0)]
for x, y, expect in test_data:
print("x={} y={} expect={}".format(x, y, expect)) result = add(x, y)
self.assertEqual(result, expect)
# 方式一
@parameterized.expand([(1, 1, 2), (1, 0, 1), (0, 0, 0)]) def test_add(self, x, y, expect):
pass
# 方式二
data = [(1, 1, 2), (1, 0, 1), (0, 0, 0)]
@parameterized.expand(data)
def test_add(self, x, y, expect): pass
# 方式三
def build_data():
return [(1, 1, 2), (1, 0, 1), (0, 0, 0)]
@parameterized.expand(build_data) def test_add(self, x, y, expect):
pass
import unittest
from parameterized import parameterized
# 求 和
def add(x, y): return x + y
# 构建测试数据
def build_data():
return [(1, 1, 2), (1, 0, 1), (0, 0, 0)]
class TestAdd(unittest.TestCase): @parameterized.expand([(1, 1, 2), (1, 0, 1), (0, 0, 0)]) def test_add_1(self, x, y, expect):
print("x={} y={} expect={}".format(x, y, expect)) result = add(x, y)
self.assertEqual(result, expect) data = [(1, 1, 2), (1, 0, 1), (0, 0, 0)]
@parameterized.expand(data)
def test_add_2(self, x, y, expect):
print("x={} y={} expect={}".format(x, y, expect)) result = add(x, y)
self.assertEqual(result, expect)
@parameterized.expand(build_data) def test_add_3(self, x, y, expect):
print("x={} y={} expect={}".format(x, y, expect)) result = add(x, y)
self.assertEqual(result, expect)
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |