单元测试框架之unittest(四)

一、摘要

假设我们有一组测试方法差别非常小,比如仅仅是所需要的参数有少许变化时,我们的自动化测试如何进行?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

 

关键词:unittest py file line python self case msg lib case.py

相关推荐:

单元测试框架之unittest(三)

Python中的单元测试模块Unittest快速入门

unittest核心要素

Python必会的单元测试框架 —— unittest

Python3 Selenium自动化web测试 ==> 第一节 起始点之Python单元测试框架 unittest

unittest单元测试框架总结