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

 

 

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