国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python unittest 簡(jiǎn)單實(shí)現(xiàn)參數(shù)化的方法

2020-02-15 23:52:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Python unittest 理論上是不建議參數(shù)驅(qū)動(dòng)的,其用例應(yīng)該專注單元測(cè)試,確保每個(gè)method的邏輯正確。

引用Stack Overflow的一個(gè)答案,

“單元測(cè)試應(yīng)該是獨(dú)立的,沒有依賴項(xiàng)的。這確保了每個(gè)用例都有非常具體而專一的測(cè)試反應(yīng)。傳入?yún)?shù)會(huì)破壞單元測(cè)試的這個(gè)屬性,從而使它們?cè)谀撤N意義上無(wú)效。使用測(cè)試配置是最簡(jiǎn)單的方法,也是更合適的方法,因?yàn)閱卧獪y(cè)試不應(yīng)該依賴外部信息來(lái)執(zhí)行測(cè)試。那應(yīng)該集成測(cè)試要做的。”

但是實(shí)際操作過(guò)程中,時(shí)不時(shí)還是有控制入?yún)⒌男枨蟮摹1热纾蚁牒?jiǎn)單實(shí)現(xiàn)一個(gè)web功能的cross-browser測(cè)試……

下面列出一些學(xué)習(xí)到的解決方案 (ENV: WIN 10, python36)。

利用類的屬性

這種方法可以不依賴其他第三方庫(kù),而且可以將參數(shù)化應(yīng)用到setUpClass 和setUp方法中。

即可以順利解決一個(gè)web 測(cè)試腳本cross-browser的驗(yàn)證的問(wèn)題。

class TestOdd1(unittest.TestCase): NUMBER=1 def runTest(self):  """Assert that the item is odd"""  self.assertTrue( self.NUMBER % 2 == 1, "Number should be odd") class TestOdd2(TestOdd1): NUMBER=2 if __name__ == '__main__': unittest.main()

nose + ddt

用nose和ddt可以簡(jiǎn)單的完成參數(shù)控制的test case,實(shí)際應(yīng)用的是python的裝飾器(decorator)。

寫出來(lái)有些類似cucumber gherkin當(dāng)中的scenario outline。

在實(shí)驗(yàn)中,這個(gè)方法不適用于setUpClass。

pip install nosepip install ddt
import unittestfrom ddt import ddt, data @ddtclass TestOdd(unittest.TestCase):  @data(3, 4, 12, 23) def runTest(self, value):  self.assertTrue( self.NUMBER % 2 == 1, "Number should be odd")

執(zhí)行 nosetests my_test.py ,4個(gè)test case被執(zhí)行。

這個(gè)方案還支持從外部文件中加載參數(shù)。具體可以參考DDT 官方文檔。

重寫unittest.TestCase的構(gòu)造函數(shù)

出處:Eli Bendersky的博客

定義類ParametrizedTestCase,使之集成unittest.TestCase并重寫其構(gòu)造函數(shù),增加param這個(gè)參數(shù)。

import unittest class ParametrizedTestCase(unittest.TestCase): """ TestCase classes that want to be parametrized should  inherit from this class. """ def __init__(self, methodName='runTest', param=None):  super(ParametrizedTestCase, self).__init__(methodName)  self.param = param  @staticmethod def parametrize(testcase_klass, param=None):  """ Create a suite containing all tests taken from the given   subclass, passing them the parameter 'param'.  """  testloader = unittest.TestLoader()  testnames = testloader.getTestCaseNames(testcase_klass)  suite = unittest.TestSuite()  for name in testnames:   suite.addTest(testcase_klass(name, param=param))  return suite

下面是一個(gè)包含我們用例的測(cè)試腳本,繼承了ParametrizedTestCase,

class TestOne(ParametrizedTestCase): def test_something(self):  print 'param =', self.param  self.assertEqual(1, 1)  def test_something_else(self):  self.assertEqual(2, 2)            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 永兴县| 廉江市| 东安县| 昌图县| 吉首市| 大冶市| 通河县| 青海省| 江西省| 北流市| 冷水江市| 梅河口市| 炎陵县| 巴彦县| 博兴县| 罗平县| 依兰县| 台湾省| 宁阳县| 康保县| 德保县| 丁青县| 曲阜市| 霞浦县| 南江县| 原阳县| 三江| 勃利县| 从化市| 大悟县| 凤庆县| 宝鸡市| 西昌市| 克拉玛依市| 新晃| 讷河市| 诸城市| 龙里县| 和平区| 正蓝旗| 磐石市|