python-测试框架nose(nosetests)简介

 

官网文档

 nose使用文档-英文版

简介

nose是一个可以从python源文件/目录或工作目录找到符合自身规则的自动收集测试。任何与testMatch正则表达式匹配的python源文件/目录或包收集。此外,发现的包或模块会沿着树结构一层层匹配,并收集所有的文件,然后检查所有匹配的测试用例执行,并可以兼容插件使用。

收集规则:testMatch,使用正则匹配,符合

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

--nologcapture
不使用log

--logging-format=FORMAT
使用自定义的格式显示日志

--logging-datefmt=FORMAT
和上面类类似,多了日期格式
--logging-filter=FILTER
日志过滤,一般很少用,可以不关注

--logging-clear-handlers
也可以不关注

--logging-level=DEFAULT
log的等级定义
nosetests -v test_case_0001 --logging-config=logging.conf

2)跳过测试---Skip

在实际测试过程中,有些测试在特定情况下需要将用例跳过不执行,可以使用SkipTest,如下

from nose.plugins.skip import SkipTest
@attr(mode=1) 
def test_learn_1():
    raise SkipTest

执行时,该条用例就会跳过

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.

等等,其他查看文档

小案例

#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

#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

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章