Skip to content

fastNLP测试说明

Coet edited this page Sep 10, 2018 · 1 revision

测试框架: pytest

持续集成服务: travis ci

代码覆盖率计算: codecov

什么是测试:

  1. 确保程序正确运行的代码,能够发现主要功能故障的代码,体现软件的自检能力。
  2. 单元测试:对一个独立功能单元的测试,主要指针对类和函数的测试。

怎样测试:

  1. 安装pytest。在Python3.6环境下 pip install pytest 或者 conda install pytest。通过pytest --version 确认该pytest对应Python 3.6。

  2. 原理:在任意文件目录命令行运行pytest, 会自动寻找所有文件名以test为前缀或后缀的文件。在每个这样的文件中,自动执行以test_开头的函数,函数体内可以通过Assert等语句检查运行结果。(参考资料:https://docs.pytest.org/en/latest/getting-started.html# 据观察,pytest也能找到unittest的测试,unittest是另一套测试框架。)

  3. 测试的过程是:

  • 人为构造测试对象和正确答案(有时测试对象不需构造)
  • 将待测试函数作用于该测试对象,得到输出结果
  • 比较输出结果和正确答案

例子

class Foo(object):
    def __init__(self, val):
        self.val = val

    def __eq__(self, other):
        return self.val == other.val

def test_compare():
    f1 = Foo(1)
    f2 = Foo(2)
    assert f1 == f2

更多测试例子,参考https://docs.pytest.org/en/latest/assert.html#assert

参考资料