Python的unittest做引數化測試
約定
引數化case的名字必須以 "param_" 為字首,後面跟真正的test名字;資料提供函式必須是classmethod,以 "collection_" 為字首,後面跟真正的test名字。
比如 parameterized_test_add 和 collection_test_add 就是一組引數化case,其中testcase基礎名字為test_add,引數化後具體的case為test_add_0, test_add_1, test_add_2 等等。為實現此功能,必須過載unittest的 TestCase 和 TestLoader。
class Test(unittest.TestCase): def __init__(self, methodName='runTest'): def isParameterizedMethod(attrname): return attrname.startswith("param") and \ hasattr(getattr(self, attrname), '__call__') testFnNames = filter(isParameterizedMethod, dir(self)) for func in testFnNames: name = func.split("_", 1)[1] collect = "collection_" + name if hasattr(getattr(self, collect), '__call__'): collectFunc = getattr(self, collect) array = collectFunc() for index in xrange(len(array)): test = "%s_%d" % (name, index) setattr(self.__class__, test, getattr(self, func)(array[index])) # must called at last unittest.TestCase.__init__(self, methodName)
過載unittest.TestLoader
class Loader(unittest.TestLoader):
def getTestCaseNames(self, testCaseClass):
"""Return a sorted sequence of method names found within testCaseClass
"""
testFnNames = unittest.TestLoader.getTestCaseNames(self, testCaseClass)
def isParameterizedMethod(attrname, testCaseClass=testCaseClass,
prefix="parameterized"):
return attrname.startswith(prefix) and \
hasattr(getattr(testCaseClass, attrname), '__call__')
testFnNames0 = filter(isParameterizedMethod, dir(testCaseClass))
for func in testFnNames0:
name = func.split("_", 1)[1]
collect = "collection_" + name
if hasattr(getattr(testCaseClass, collect), '__call__'):
collectFunc = getattr(testCaseClass, collect)
for item in xrange(len(collectFunc())):
testFnNames.append("%s_%d" % (name, item))
if self.sortTestMethodsUsing:
testFnNames.sort(key=_CmpToKey(self.sortTestMethodsUsing))
return testFnNames
編寫測試用例
from unittest import * from Test import * from Loader import * class TestFunctions(Test): @classmethod def collection_test_add(cls): return [1,2,3,5] def parameterized_test_add(self, x): def test_body(self): print(x * x) return test_body if __name__ == '__main__': suite = Loader().loadTestsFromTestCase(TestFunctions) runner = unittest.TextTestRunner() rc = runner.run(suite) print(rc)
在該用例中,真正的testcase定義在test_body函式中。collection_test_add 必須是一個無參的classmethod,返回一個list;parame_test_add 必須為非 classmethod 的成員函式,接受一個入參,該入參為 collection_test_add 所返回的 list 的元素,顯然,該 list 的元素可以是任意資料型別,可以是list,tuple,dict等等,這樣在test_body內可以接收更加豐富的輸入。
本例中,collection_test_add 所返回的 list 中有4個元素,依次生成 test_add_0, test_add_1, test_add_2, test_add_3共4個具體的case。
相關文章
- Python 自動化測試框架unittestPython框架
- Python 自動化測試 必會模組 UnittestPython
- python自動化測試框架pytest和unittest區別!!!Python框架
- python 自動化測試 (一):安裝 requests,unittest,HTMLTestRunnerPythonHTML
- Python + requests + unittest + ddt 進行介面自動化測試的框架Python框架
- Python中的單元測試框架:使用unittest進行有效測試Python框架
- python介面自動化測試 —— unittest框架suite、runner詳細使用Python框架UI
- 記錄python介面自動化測試--利用unittest生成測試報告(第四目)Python測試報告
- 介面測試 - 引數測試
- Jmeter模板化引數併發測試JMeter
- 基於Python的介面自動化-unittest測試框架和ddt資料驅動Python框架
- 自動化冒煙測試 Unittest , Pytest 哪家強?
- Python測試框架pytest命令列引數用法Python框架命令列
- 介面測試之unittest框架框架
- .net持續整合測試篇之Nunit引數化測試
- 肖sir__介面測試之python+rquest+unittest分層自動化框架Python框架
- 記錄python介面自動化測試--unittest框架基本應用(第二目)Python框架
- 使用Postman工具做介面測試(五)——生成隨機引數Postman隨機
- python+appium+pytest做app自動化測試PythonAPP
- 一文搞懂Python Unittest測試方法執行順序Python
- Oracle JDBC ResultSet引數測試OracleJDBC
- 介面測試-引數校驗
- 如何使得 unittest 的測試用例有序規劃?
- 軟體測試學習資料——Jmeter引數化2JMeter
- 軟體測試學習資料——Jmeter引數化1JMeter
- Python中的Unittest框架Python框架
- 【python介面自動化】初識unittest框架Python框架
- 一篇文章帶你瞭解Python基礎測試工具——UnitTestPython
- 使用 JUnit 5.7 進行引數化測試:深入瞭解 @EnumSource
- 效能測試學習筆記:Loadrunner如何進行引數化?筆記
- unittest 單元測試框架教程 1-執行測試指令碼框架指令碼
- 如何做自動化測試?什麼是自動化測試?
- python自動化測試Python
- 滲透測試學習之報告測試引數五
- [20180308]測試ARG_MAX引數.txt
- 使用 testng 做介面自動化測試
- Allure 測試報告:allure.title 如何去掉後方的引數化顯示測試報告
- 測試資料放 yaml 檔案,不同介面存在關聯引數怎麼做更好YAML
- 介面測試並不只是測試引數和返回值