From 0fdb60e2e5abc3795919b134d30c7c36da86f7ed Mon Sep 17 00:00:00 2001 From: Coet Date: Mon, 10 Sep 2018 11:16:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95=E8=AF=B4?= =?UTF-8?q?=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastNLP测试说明.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 fastNLP测试说明.md diff --git a/fastNLP测试说明.md b/fastNLP测试说明.md new file mode 100644 index 0000000..6a3ca61 --- /dev/null +++ b/fastNLP测试说明.md @@ -0,0 +1,41 @@ +测试框架: 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 + + +### 参考资料 +- pytest入门: https://docs.pytest.org/en/latest/getting-started.html# +- 关于codecov: https://agostini.tech/2017/07/16/code-coverage-with-codecov/