單元測試框架之unittest(四)
阿新 • • 發佈:2018-12-26
一、摘要
假設我們有一組測試方法差別非常小,比如僅僅是所需要的引數有少許變化時,我們的自動化測試如何進行?unittest框架為這種場景提供了一種方式,它允許我們用subTest()上下文管理器在一個測試方法內識別這種細小的不同,這麼說可能有點暈
實際上我覺得還不如直接看程式碼更能清晰的表達它為我們的測試帶來了什麼
二、程式碼例項
# coding:utf-8 import unittest class NumbersTest(unittest.TestCase): def test_even(self): """ 使用subTest上下文管理器,區分細小的變化 取模運算,返回除法的餘數,但是引數是0到5的整數,沒必要單獨寫方法""" for i in range(0, 6): with self.subTest(i=i): self.assertEqual(i % 2, 0) if __name__ == '__main__': unittest.main()
執行這段程式碼的結果會是:
SubTest failure: Traceback (most recent call last): File "C:\Python37\lib\unittest\case.py", line 59, in testPartExecutor yield File"C:\Python37\lib\unittest\case.py", line 533, in subTest yield File "D:\Programs\Python\Demo\unittest4\subtestDemo.py", line 14, in test_even self.assertEqual(i % 2, 0) File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\helpers\pycharm\teamcity\diff_tools.py", line 39, in _patched_equals raise native_error File"C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\helpers\pycharm\teamcity\diff_tools.py", line 32, in _patched_equals old(self, first, second, msg) File "C:\Python37\lib\unittest\case.py", line 839, in assertEqual assertion_func(first, second, msg=msg) File "C:\Python37\lib\unittest\case.py", line 832, in _baseAssertEqual raise self.failureException(msg) AssertionError: 1 != 0 SubTest failure: Traceback (most recent call last): File "C:\Python37\lib\unittest\case.py", line 59, in testPartExecutor yield File "C:\Python37\lib\unittest\case.py", line 533, in subTest yield File "D:\Programs\Python\Demo\unittest4\subtestDemo.py", line 14, in test_even self.assertEqual(i % 2, 0) File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\helpers\pycharm\teamcity\diff_tools.py", line 39, in _patched_equals raise native_error File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\helpers\pycharm\teamcity\diff_tools.py", line 32, in _patched_equals old(self, first, second, msg) File "C:\Python37\lib\unittest\case.py", line 839, in assertEqual assertion_func(first, second, msg=msg) File "C:\Python37\lib\unittest\case.py", line 832, in _baseAssertEqual raise self.failureException(msg) AssertionError: 1 != 0 SubTest failure: Traceback (most recent call last): File "C:\Python37\lib\unittest\case.py", line 59, in testPartExecutor yield File "C:\Python37\lib\unittest\case.py", line 533, in subTest yield File "D:\Programs\Python\Demo\unittest4\subtestDemo.py", line 14, in test_even self.assertEqual(i % 2, 0) File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\helpers\pycharm\teamcity\diff_tools.py", line 39, in _patched_equals raise native_error File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\helpers\pycharm\teamcity\diff_tools.py", line 32, in _patched_equals old(self, first, second, msg) File "C:\Python37\lib\unittest\case.py", line 839, in assertEqual assertion_func(first, second, msg=msg) File "C:\Python37\lib\unittest\case.py", line 832, in _baseAssertEqual raise self.failureException(msg) AssertionError: 1 != 0 One or more subtests failed Failed subtests list: (i=1), (i=3), (i=5) Ran 1 test in 0.020s FAILED (failures=3) Process finished with exit code 1
而如果我們不使用subTest(), 只是寫個簡單的迴圈去斷言,當程式執行到第一個斷言失敗時就會終止了,後邊可能還有斷言能夠成功的也就不會被執行了
# coding:utf-8 import unittest class NumbersTest(unittest.TestCase): def test_even(self): for i in range(0, 6): # with self.subTest(i=i): print("當前引數是:%d" % i) self.assertEqual(i % 2, 0) if __name__ == '__main__': unittest.main()
執行結果會是:
當前引數是:0 當前引數是:1 Ran 1 test in 0.010s FAILED (failures=1) 0 != 1 Expected :1 Actual :0 <Click to see difference> Traceback (most recent call last): File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\helpers\pycharm\teamcity\diff_tools.py", line 32, in _patched_equals old(self, first, second, msg) File "C:\Python37\lib\unittest\case.py", line 839, in assertEqual assertion_func(first, second, msg=msg) File "C:\Python37\lib\unittest\case.py", line 832, in _baseAssertEqual raise self.failureException(msg) AssertionError: 1 != 0 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Python37\lib\unittest\case.py", line 59, in testPartExecutor yield File "C:\Python37\lib\unittest\case.py", line 615, in run testMethod() File "D:\Programs\Python\Demo\unittest4\subtestDemo.py", line 15, in test_even self.assertEqual(i % 2, 0) Process finished with exit code 1