[python]数据生成库hypothesis

被测试的函数 文件 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))


测试代码

#! -*- coding:utf-8 -*-

import unittest

from hypothesis import given

from hypothesis import settings

from hypothesis import strategiesas st

from myadd import add



#集成unittest.TestCase,方便后续的断言使用

class AddTest(unittest.TestCase):

#通过@settings max_examples 的值设置测试集大小

@settings(max_examples=10)

#通过given设置伪造数据的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()

你可能感兴趣的:([python]数据生成库hypothesis)