Python測試載入器——nose

來源:互聯網
上載者:User

標籤:com   工具   hang   -o   ini   .com   collect   正則   指定   

1、nose 特點:

a)         自動探索測試案例(包含[Tt]est檔案以及檔案包中包含test的函數)

b)         以test開頭的檔案

c)         以test開頭的函數或方法

d)         以Test開頭的類

  nose自動收集單元測試,會自動識別原始碼檔案、目錄或包中的測試案例,任何符合Regex:(?:^|[b_.-])[Tt]est的類、函數、檔案或目錄,以及TestCase的子類都會被識別並執行,匹配成功的包、任何python的源檔案都會被當做測試案例。

 

2、nose的setup和teardown:

a)  package(包)中的setup、teardown,放在__init__.py檔案中  —>整個測試的運行期間只運行一次

b)  模組層級別:setup_module、teardown_module   —>整個測試的運行期間只運行一次

c)  類層級:setup_class、teardown_class       —>每個測試方法執行時都會調用

 

3、nose相關執行命令:

1、  nosetests  –h查看所有nose相關命令

2、  nosetests –s執行並捕獲輸出

3、  nosetests –with-xunit輸出xml結果報告

4、  nosetests -v: 查看nose的運行資訊和調試資訊 

5、  nosetests -w 目錄:指定一個目錄運行測試

 

4、nose 測試代碼的執行方法:

1.cd + 要執行的檔案地址(如:C:\Users\zhangjx\test_main\Test1\test)-> nosetests

2.使用編碼實現測試案例的執行

建立main.py(名字隨意),內容如下:

 

執行即可:

或:

執行結果,可以看到返回True或Flase

或:

 

 

3.直接運行整個包:nosetests -v  test_case

4.運行某一個模組:nosetests –v  test_case.test_case_0002

5.運行某一個用例:nosetests -v  test_case.test_case_0002:test_lean_4

6.運行不同模式下不同用例:

nosetests  -v --tests=test_case.test_case_0002:test_lean_4,test_case.test_case_0001:test_lean_2

 

5、工具nose.tools的使用:

1)測試指令碼中引入:from nose.tools import nottest,istest;

2)不測試的方法:方法名上加修飾器@nottest;

3)指定為測試方法:方法名上加修飾器@istest(方法名無需符合命名規則);

4)查看要執行的用例列表:nosetests --collect-only –v

Python測試載入器——nose

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.