官网文档
nose使用文档-英文版
简介
nose是一个可以从python源文件/目录或工作目录找到符合自身规则的自动收集测试。任何与testMatch正则表达式匹配的python源文件/目录或包收集。此外,发现的包或模块会沿着树结构一层层匹配,并收集所有的文件,然后检查所有匹配的测试用例执行,并可以兼容插件使用。
收集规则:testMatch,使用正则匹配,符合
1 | self.testMatch = re.compile(r'(?:^|[\\b_\\.%s-])[Tt]est' % os.sep) |
文件名/类名/方法名 符合
1)可以识别Test/test开头的文件/类名/方法名
2)可以识别**_Test/test**的文件/类名/方法名
安装
nose不是python自带模块,需要手动安装
pip install nose 或者官网下载nose,本地python setup.py install
nose插件(内置)
1)捕获日志---logcapture
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <strong>--</strong><strong>nologcapture</strong> 不使用log <strong>--logging-format=</strong><strong>FORMAT</strong> 使用自定义的格式显示日志 -<strong>-logging-datefmt=</strong><strong>FORMAT</strong> 和上面类类似,多了日期格式 <strong>--logging-filter=</strong><strong>FILTER</strong> 日志过滤,一般很少用,可以不关注 <strong>--logging-clear-</strong><strong>handlers</strong> 也可以不关注 <strong>--logging-level=</strong><strong>DEFAUL</strong>T log的等级定义 |
1 | nosetests -v test_case_0001 --logging-config=logging.conf |
2)跳过测试---Skip
在实际测试过程中,有些测试在特定情况下需要将用例跳过不执行,可以使用SkipTest,如下
1 2 3 4 | from nose.plugins.skip import SkipTest @attr(mode=1) def test_learn_1(): raise SkipTest |
执行时,该条用例就会跳过
1 2 | E:\workspace\nosetest_lear\test_case>nosetests -v -a "mode" test_case_0001.py test_case.test_case_0001.test_learn_1 ... SKIP |
3)Testid: 在输出文件中填加testid的显示
如
4)-i、-I、-e
这几个参数在实际测试中也经常使用到
-i REGEX, --include=REGEX: 加了该参数表明测试的时候去按 REGEX正则去执行用例,不匹配的则不执行
-e REGEX, --exclude=REGEX : 不跑与正则匹配的用例
-I REGEX, --ignore-file=REGEX: 忽略文件
如:
nosetests -v -s -e test_case_0002
表示不执行test_case_0002这个文件的全部用例
nose命令
a、nose执行相关命令
1、nosetests -h 查看所有nose命令与说明
2、nosetests 查看是否安装nose成功
3、nosetests -with-xunit输出xml结果报告
4、nosetests -v 查看运行信息和调试信息
5、nosetests -w 目录:指定一个目录运行测试
6、nosetests -f 执行测试
7、nosttests -p 查看可用plugins信息
8、--tests=NAMES 指定具体的测试用例 主要用于单个测试用例调试
9、-a=ATTR, --attr=ATTR只运行带ATTR属性的测试用例 [NOSE_ATTR],常用于不同场景下执行不同的测试用例
10、-s, --nocapture 不捕获输出,任何消息立马输出 [NOSE_NOCAPTURE]
11、--with-xunit 使能Xunit插件: 使用standard XUnit XML format提供测试报告. [NOSE_WITH_XUNIT]
12、--xunit-file=FILE 存xunit report的xml文件名. 默认工作目录下名为nosetests.xml [NOSE_XUNIT_FILE]
13、--xunit-testsuite-name=PACKAGE 通过插件生成的xunit xml, 默认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 | #encoding:utf-8 import nose class TestClass(): def setUp(self): print("MyTestClass setup") def tearDown(self): print("MyTestClass teardown") def Testfunc1(self): print("this is Testfunc1") pass def test_func1(self): print("this is test_func1") pass def func2_Test(self): print("this is Testfunc2") pass def test_func2(self): print("this is test_func2") pass def testfunc3(self): print("this is test_func3") pass def func4test(self): print("this is test_func4") pass def func5_test(self): print("this is test_func5") pass |
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 | #encoding:utf-8 class Class_Test(): def setUp(self): print("MyTestClass setup") def tearDown(self): print("MyTestClass teardown") def Testfunc1(self): print("this is Testfunc1") pass def test_func1(self): print("this is test_func1") pass def func2_Test(self): print("this is Testfunc2") pass def test_func2(self): print("this is test_func2") pass def testfunc3(self): print("this is test_func3") pass def func4_testddd(self): print("this is test_func4") pass def func5_test_ff(self): print("this is test_func5") pass |

