Nosetests-技术文档

fansichao 2021-10-23 16:16:35
Categories: Tags:

Noestests 介绍

nostests 优点:

unittest 核心工作原理

unittest 中最核心的四个概念

unittest 关键类说明

Linux-Nosetests02静态类图

Noestest 使用

安装部署

1
pip install nose

插件

TODO 如何配置插件

使用 pip 安装所需要的插件,然后通过 nosetests 命令行配置插件。
如果通过 nose.main()或者 nose.run()执行测试,可以将要使用的插件关键字参数的列表传递进去

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 显示已安装插件
nosetests --plugins
# -v 显示插件详细信息

(env) [scfan@scfan project]$ nosetests --plugins -v
Plugin capture
score: 1600
Output capture plugin. Enabled by default. Disable with ``-s`` or
``--nocapture``. This plugin captures stdout during test execution,
appending any output captured to the error or failure output, should
the test fail or raise an error.

Plugin failuredetail
score: 1600
Plugin that provides extra information in tracebacks of test
failures.

Plugin xunit
score: 1500
This plugin provides test results in the standard XUnit XML format.

Plugin deprecated
score: 1000
Installs a DEPRECATED error class for the DeprecatedTest exception.
Enabled by default.

Plugin skip
score: 1000
Plugin that installs a SKIP error class for the SkipTest exception.
When SkipTest is raised, the exception will be logged in the skipped
attribute of the result, 'S' or 'SKIP' (verbose) will be output, and
the exception will not be counted as an error or failure.

Plugin multiprocess
score: 1000
Run tests in multiple processes. Requires processing module.

Plugin logcapture
score: 500
Log capture plugin. Enabled by default. Disable with --nologcapture.
This plugin captures logging statements issued during test
execution, appending any output captured to the error or failure
output, should the test fail or raise an error.

Plugin coverage
score: 200
Activate a coverage report using Ned Batchelder's coverage module.
.....

nosetests 参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
-V,—version

输出nose的版本

-p,—plugins

输出可获取的插件列表。

-v=DEFUALT,—verbose=DEFAULT

使用更多的verbose

--verbosity=VERBOSITY

设置verbosity;--verbosity=2与-v设置一致

-q,—quiet=DEFAULT

使用更少的verbose

-c=FILES,—config=FILES

设置配置文件。可以设置很多次,然后将所有的配置文件合并。

-w=WHERE,--where=WHERE

设置查找的根目录。

-py3where=WHERE

顾名思义,针对python3.x以上设置查找路径。

-m=REGEX,--match=REGEX,—testmatch=REGEX

设置用于自动化收集用例的正则表达式。

--tests=NAMES

执行这些测试。

--debug-log=FILE

设置调试的日志文件路径。

--logging-config=FILE,—log-config=FILE

设置日志文件的配置文件。

-I=REGEX,—ignore-files=REGEX

设置自动收集测试用例时忽略的正则表达式。

-e=REGEX,—exclude=REGEX

排除要执行的测试用例的正则表达式

-i=REGEX,—include=REGEX

包含要执行的测试用例的正则表达式

-x,—stop

执行测试发生错误后,停止执行测试。

--noexe

不查找可以执行文件。

-a=ATTR,--attr=ATTR

只执行包含ATTR属性的测试用例。

-A=EXPR,—eval-attr=EXPR

只执行属性与EXPR匹配的测试用例。

-s,—nocapture

不抓取标准输出(stdout)

--nologcapture

禁止使用日志插件

--logging-format=FORMAT

打印语句的自定义格式

--logging-datefmt=FORMAT

日志的日期时间格式

--logging-filter=FILTER

日志语句的过滤器。

--logging-clear-handlers

清除日志的其他handlers

--logging-level=DEFUALT

设置日志的等级

--with-coverage

开启coverage插件

--cover-package=PACKAGE

限定coverage所在包

--cover-erase

在执行之前 清除上次coverage统计结果

--cover-testes

在coverage报告中包含测试模块

--cover-html

产生html的coverage报告

--cover-html-dir=DIR

设置存储html的目录

--cover-xml

产生xml的coverage报告

--cover-xml-file=FILE

设置存储coverage报告的xml文件

--cover-config-file=DEFUALT

设置coverage的配置文件

-pdb

当测试失败或产生错误是进入调试模式

--pdb-failures

当测试失败时进入调试模式

--pdb-errors

当测试产生错误时进入调试模式

--with-doctest

开启doctest插件

--doctest-tests

在测试模块中查询doctests

--with-profile

开启profile插件

--profile-sort=SORT

设置profiler 输出排序

--profile-stats-file=FILE

设置统计所在的文件地址

--with-id

开启TestId插件

--processes=NUM

开始测试处理器的个数

--processes-timeout=SECONDS

设置超时时间。

--with-xuint

开始Xunit插件

--xuint-file=FILE

设置XUnit报告所在的xml文件

--all-modules

开启AllModules插件

--collect-only

开启只收集测试功能。只收集测试用例及输出测试名字,而不执行测试

Nosetests 语法说明

TODO 详细说明

扩展 nose-printlog

可以在控制台输出日志,也可以将日志显示在 caplog 中

1
2
3
4
# 安装插件
pip install nose-printlog
# 运行命令样例
nosetests -sv test_data_load.py --logging-format="%(asctime)s:%(name)s:%(levelname)s: %(message)s" --with-printlog

附件

用 HTMLTestRunner 输出漂亮的 HTML 报告

参考链接:CSDN 博客 unittest 单元测试

tools_unittest_HTMLTestRunner.py
下载链接

测试代码

tools_mathfunc.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(env) [scfan@WOM tools]$ cat tools_mathfunc.py
#! -*- coding:utf-8 -*-
u"""
常见数学类函数

- add
- minux
- divide
- multi

"""

def add(x,y):
return x+y

def minus(x,y):
return x-y

def divide(x,y):
return x/y

def multi(x,y):
return x * y

test_unittest_htmlreport.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# coding=utf-8
u"""
单元测试unittest 生成HTML报告测试

运行命令:
cd /home/scfan/pro/server && python -m pro.tests.test_unittest_htmlreport

"""
import unittest
from ..tools.tools_unittest_HTMLTestRunner import HTMLTestRunner
from ..tools.tools_mathfunc import *

class TestMathFunc(unittest.TestCase):

def test_add(self):
self.assertEqual(3, add(1, 2))
self.assertNotEqual(3, add(2, 2))

def test_minus(self):
self.assertEqual(1, minus(3, 2))

def test_multi(self):
self.assertEqual(6, multi(3, 2))

def test_divide(self):
self.assertEqual(2, divide(6, 3))
self.assertEqual(2.5, divide(5, 2))

if __name__ == '__main__':
suite = unittest.TestSuite()

tests = [TestMathFunc("test_add"), TestMathFunc("test_minus"), TestMathFunc("test_divide")]
suite.addTests(tests)

with open('HTMLReport.html', 'w') as f:
runner = HTMLTestRunner(stream=f,
title = 'MathFunc Test Report',
description='generated by HTMLTestRunner.',
verbosity=2
)
runner.run(suite)

参考资源

参考链接:

扩展: