被測(cè)試的函數(shù) 文件 myadd.py
#! -*- coding:utf-8 -*-
def add(a, b):
if not isinstance(a, int)or not isinstance(b, int):
print("usage add(a,b): return result of int a + int b")
return -1
? ? return a+b
if __name__ =="__main__":
print(add(1, 2))
測(cè)試代碼
#! -*- coding:utf-8 -*-
import unittest
from hypothesis import given
from hypothesis import settings
from hypothesis import strategiesas st
from myadd import add
#集成unittest.TestCase,方便后續(xù)的斷言使用
class AddTest(unittest.TestCase):
#通過@settings max_examples 的值設(shè)置測(cè)試集大小
@settings(max_examples=10)
#通過given設(shè)置偽造數(shù)據(jù)的a以及b的類型
@given(a=st.integers(), b=st.integers())
def test_case(self, a, b):
print("a->%d" % a)
print("b->%d" % b)
res = a+b
act = add(a, b)
print("result->%d" % act)
self.assertEqual(res, act)
if __name__ =="__main__":
unittest.main()