python3笔记

Python 单元测试

2018-05-30  本文已影响3人  shenshizhong

测试

测试用例

对函数 abs(),这个函数的作用就是取绝对值,我们可以编写以下几个测试用例:

  1. 输入正数,比如 1、 1.5、 0.99,期待返回值与输入相同
  2. 输入负数,比如 -1、 -1.5、 -0.99, 期待返回值与输入值相反
  3. 输入0, 期待返回0;
  4. 输入非数值类型,比如 None、 []、 {}, 期待抛出 TypeError
    把上面的测试用例,放到测试模块中,就是一个完整的单元测试。
    单元测试通过说明我们的函数能够正常工作,要是不过,就说明函数还有bug,
    那么就得修改,直到单元测试通过。
单元测试得意义

mydict.py 代码:

class Dict(dict):
    def __init__(self, **kw):
        super().__init__(**kw)


    def __getattr__(self, key):
        try:
            return self[key]
        except KeyError:
            raise AttributeError(r"'Dict' object has no attribute '%s'" % key)

    def __setattr__(self, key, value):
        self[key] = value

编写单元测试

我们需要引入 Python 自带的 unittest 模块,编写 mydict_test 如下:

import  unittest

from learn.two.测试.mydict import Dict


class TestDict(unittest.TestCase):
    def test_init(self):
        d = Dict(a = 1, b = '测试')
        self.assertEqual(d.a, 1)
        self.assertEqual(d.b, '测试')
        self.assertTrue(isinstance(d, dict))

    # def test_key(self):        #这部分是测试用例通不过的例子
    #     d = Dict()
    #     d['key'] = 'value'
    #     self.assertSetEqual(d.key, 'value')

    def test_attr(self):
        d = Dict()
        d.key = 'value'
        self.assertTrue('key' in d)
        self.assertEqual(d['key'], 'value')

    def test_keyerror(self):
        d = Dict()
        with self.assertRaises(KeyError):
            value = d['empty']

    def test_attrerror(self):
        d = Dict()
        with self.assertRaises(AttributeError):
            value = d.empty

if __name__ == '__main__':
    unittest.main()

直接运行 mydict_test.py

Testing started at 22:54 ...
E:\py\venv\Scripts\python.exe "D:\JetBrains\PyCharm 2018.1.1\helpers\pycharm\_jb_unittest_runner.py" --target mydict_test.TestDict
Launching unittests with arguments python -m unittest mydict_test.TestDict in E:\pyplace\learn_python3\learn\two\测试


Ran 4 tests in 0.012s

OK

Process finished with exit code 0

以上这就说明单元测试通过了

下面是测试不通过的示范(将注释的部分放开):

import  unittest

from learn.two.测试.mydict import Dict


class TestDict(unittest.TestCase):
    def test_init(self):
        d = Dict(a = 1, b = '测试')
        self.assertEqual(d.a, 1)
        self.assertEqual(d.b, '测试')
        self.assertTrue(isinstance(d, dict))

    def test_key(self):                  #这部分是测试用例通不过的例子
        d = Dict()
        d['key'] = 'value'
        self.assertSetEqual(d.key, 'value')

    def test_attr(self):
        d = Dict()
        d.key = 'value'
        self.assertTrue('key' in d)
        self.assertEqual(d['key'], 'value')

    def test_keyerror(self):
        d = Dict()
        with self.assertRaises(KeyError):
            value = d['empty']

    def test_attrerror(self):
        d = Dict()
        with self.assertRaises(AttributeError):
            value = d.empty

if __name__ == '__main__':
    unittest.main()

运行 mydict_test.py 结果:

Testing started at 22:58 ...
E:\py\venv\Scripts\python.exe "D:\JetBrains\PyCharm 2018.1.1\helpers\pycharm\_jb_unittest_runner.py" --target mydict_test.TestDict
Launching unittests with arguments python -m unittest mydict_test.TestDict in E:\pyplace\learn_python3\learn\two\测试


Ran 5 tests in 0.027s

FAILED (failures=1)

Failure
Traceback (most recent call last):
  File "D:\python\Python36\lib\unittest\case.py", line 1055, in assertSetEqual
    difference1 = set1.difference(set2)
AttributeError: 'str' object has no attribute 'difference'
  File "D:\python\Python36\lib\unittest\case.py", line 670, in fail
    raise self.failureException(msg)
AssertionError: first argument does not support set difference: 'str' object has no attribute 'difference'

可以看到,控制台输入了红色的错误日志,意味着单元测试不通过

单元测试的写法

常用方法
 self.assertEqual(abs(-1), 1)    # 断言返回的结果与1相等
with self.assertRaises(KeyError):
    value = d['empty']

如果通过 d.empty 访问不存在的 key 时,我们期待抛出 AttributeError:

with self.assertRaises(AttributeError):
    value = d.empty

运行单元测试

if __name__ == '__main___':
    unittest.main()
$ python mydict_test.py

总结

  1. 写被测试的类
  2. 继承 unittest.TestCase 写单元测试类
  3. 通过会显示绿色 Tests passed,并在输出日志中显示 OK
  4. 不通过显示红色 Tests failed,并在输出日志中显示 FAILD

github地址: https://github.com/shenshizhong/learn_python3

上一篇下一篇

猜你喜欢

热点阅读