2020-03-18python高手之路学习随笔(8.1)

开始狄部长测试单元的学习

所谓的测试单元,即是通过假设访问,数据对编写的函数进行稳定性测试。
这实际上也是python在付出代价,因为其动态性的语言,使得错误往往要在运行到那边的时候。才会显示出来这个错误。
使用测试单位可以减少这种错误的发生。
引入测试库,需要再全局引入,虚拟环境下,有时候path是不起作用的

pip install nose
nosetests -v

能直接运行当前目录下的所有带test的文件里面带test的函数
缺点是只能判断assert的对错的类型(也就是只能判断断言出来的真或者 假)

例子:

/test/test_true.py

#!/usr/bin/env python
#-*- coding:utf-8 -*-


def test_true():

    assert True


def test_flase():

    assert False

运行结果:

test_foobar.test_true ... ok

----------------------------------------------------------------------
Ran 1 test in 0.007s

2个函数之后,测试运行失败的反应

test_foobar.test_true ... ok
test_foobar.test_flase ... FAIL

======================================================================
FAIL: test_foobar.test_flase
----------------------------------------------------------------------
Traceback (most recent call last):
  File "d:\python37-32\lib\site-packages\nose\case.py", line 198, in runTest
    self.test(*self.arg)
  File "C:\Users\Administrator\PycharmProjects\test测试\test_foobar.py", line 12, in test_flase
    assert False
AssertionError

----------------------------------------------------------------------
Ran 2 tests in 0.009s

FAILED (failures=1)

你可能感兴趣的:(2020-03-18python高手之路学习随笔(8.1))