1. 關(guān)于Nose
nose項(xiàng)目是于2005年發(fā)布的,也就是 py.test改名后的一年。它是由 Jason Pellerin 編寫(xiě)的,支持與 py.test 相同的測(cè)試習(xí)慣做法,但是這個(gè)包更容易安裝和維護(hù)。
nose的口號(hào)是:擴(kuò)展unittest,nose讓測(cè)試更簡(jiǎn)單!
nose官網(wǎng):http://nose.readthedocs.io/en/latest/index.html
使用nose框架進(jìn)行Python項(xiàng)目的自動(dòng)化測(cè)試,可以參考:http://www.cnblogs.com/liaofeifight/p/5148717.html
本文主要介紹nose框架編寫(xiě)自動(dòng)化測(cè)試用例的方法。
2. Nose編寫(xiě)測(cè)試用例方法
nose會(huì)自動(dòng)識(shí)別源文件,目錄或包中的測(cè)試用例。
任何匹配testMatch正則表達(dá)式(默認(rèn)為(?:^|[/b_/.-])[Tt]est,在一個(gè)單詞的邊界處或者緊跟-或_處有test或Test)的函數(shù)或類,并且所在的模塊也匹配該表達(dá)式,都會(huì)被識(shí)別為測(cè)試并執(zhí)行。
出于對(duì)unittest兼容性的考慮,nose也支持繼承unittest.TestCase的子類測(cè)試用例。與py.test類似,nose按照測(cè)試集在模塊文件中出現(xiàn)的順序執(zhí)行功能測(cè)試。繼承于TestCase的測(cè)試集和測(cè)試類按照字母表順序執(zhí)行。
2.1 Fixtures
nose支持包,模塊,類和函數(shù)例級(jí)別的Fixtures(setup和teardown方法,用以自動(dòng)測(cè)試的初始化或者清理工作)
2.2 Test packages
nose允許測(cè)試?yán)园姆绞椒纸M。
因此,也需要包級(jí)別的setup;比如,如果你想要?jiǎng)?chuàng)建一個(gè)數(shù)據(jù)庫(kù)測(cè)試,你可能會(huì)想要在包setup時(shí)創(chuàng)建數(shù)據(jù)庫(kù),當(dāng)每個(gè)測(cè)試結(jié)束之后運(yùn)行包teardown時(shí),銷毀它。而不是在每一個(gè)測(cè)試模塊或者測(cè)試?yán)袆?chuàng)建和銷毀數(shù)據(jù)庫(kù)。
想要?jiǎng)?chuàng)建包級(jí)別的setup和teardown函數(shù),你需要在測(cè)試包的_ init_.py 函數(shù)中定義setup和teardown函數(shù)。setup函數(shù)可以被命名為setup,setup_package,setUp,或者setUpPackage;teardown可以被命名為teardown,teardown_package, tearDown, 或者tearDownPackage。一旦第一個(gè)測(cè)試模塊從測(cè)試包中被加載后,一個(gè)包中的測(cè)試?yán)烷_(kāi)始執(zhí)行。
2.3 Test modules
Test modules是一個(gè)匹配testMatch的python模塊。
測(cè)試模塊提供模塊級(jí)別的setup和teardown。可以定義setup, setup_module, setUp, setUpModule用于setup,teardown, teardown_module, tearDownModule用于teardown。一旦一個(gè)模塊中所有的用例被收集完后,模塊中的測(cè)試就開(kāi)始執(zhí)行。
2.4 Test classes
Test classes是模塊中定義的匹配testMatch或者繼承unittest.TestCase的類。
所有的測(cè)試類以相同方式運(yùn)行:通過(guò)testMatch匹配的找到類中的方法,并以全新的測(cè)試類實(shí)例運(yùn)行測(cè)試方法。
像繼承于unittest.TestCase的子類一樣,測(cè)試類可以定義setUp tearDown函數(shù),它們將會(huì)分別在每一個(gè)測(cè)試方法之前和之后運(yùn)行。類級(jí)別setup fixture可以被命名為setup_class, setupClass, setUpClass, setupAll, setUpAll;teardown被命名為teardown_class, teardownClass, tearDownClass, teardownAll, tearDownAll, 類級(jí)別setup和teardown必須是類方法(@classmethod)。
新聞熱點(diǎn)
疑難解答
圖片精選