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)
新聞熱點(diǎn)
疑難解答
圖片精選