美文网首页
[python]数据生成库hypothesis

[python]数据生成库hypothesis

作者: 小哥讲笑话 | 来源:发表于2019-11-13 12:23 被阅读0次

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

          本文链接:https://www.haomeiwen.com/subject/hgdqictx.html