单元测试框架之unittest

Posted davieyang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单元测试框架之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:Python37libunittestcase.py", line 59, in testPartExecutor
    yield
  File "C:Python37libunittestcase.py", line 533, in subTest
    yield
  File "D:ProgramsPythonDemounittest4subtestDemo.py", line 14, in test_even
    self.assertEqual(i % 2, 0)
  File "C:Program FilesJetBrainsPyCharm Community Edition 2018.3.1helperspycharm	eamcitydiff_tools.py", line 39, in _patched_equals
    raise native_error
  File "C:Program FilesJetBrainsPyCharm Community Edition 2018.3.1helperspycharm	eamcitydiff_tools.py", line 32, in _patched_equals
    old(self, first, second, msg)
  File "C:Python37libunittestcase.py", line 839, in assertEqual
    assertion_func(first, second, msg=msg)
  File "C:Python37libunittestcase.py", line 832, in _baseAssertEqual
    raise self.failureException(msg)
AssertionError: 1 != 0

SubTest failure: Traceback (most recent call last):
  File "C:Python37libunittestcase.py", line 59, in testPartExecutor
    yield
  File "C:Python37libunittestcase.py", line 533, in subTest
    yield
  File "D:ProgramsPythonDemounittest4subtestDemo.py", line 14, in test_even
    self.assertEqual(i % 2, 0)
  File "C:Program FilesJetBrainsPyCharm Community Edition 2018.3.1helperspycharm	eamcitydiff_tools.py", line 39, in _patched_equals
    raise native_error
  File "C:Program FilesJetBrainsPyCharm Community Edition 2018.3.1helperspycharm	eamcitydiff_tools.py", line 32, in _patched_equals
    old(self, first, second, msg)
  File "C:Python37libunittestcase.py", line 839, in assertEqual
    assertion_func(first, second, msg=msg)
  File "C:Python37libunittestcase.py", line 832, in _baseAssertEqual
    raise self.failureException(msg)
AssertionError: 1 != 0

SubTest failure: Traceback (most recent call last):
  File "C:Python37libunittestcase.py", line 59, in testPartExecutor
    yield
  File "C:Python37libunittestcase.py", line 533, in subTest
    yield
  File "D:ProgramsPythonDemounittest4subtestDemo.py", line 14, in test_even
    self.assertEqual(i % 2, 0)
  File "C:Program FilesJetBrainsPyCharm Community Edition 2018.3.1helperspycharm	eamcitydiff_tools.py", line 39, in _patched_equals
    raise native_error
  File "C:Program FilesJetBrainsPyCharm Community Edition 2018.3.1helperspycharm	eamcitydiff_tools.py", line 32, in _patched_equals
    old(self, first, second, msg)
  File "C:Python37libunittestcase.py", line 839, in assertEqual
    assertion_func(first, second, msg=msg)
  File "C:Python37libunittestcase.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 FilesJetBrainsPyCharm Community Edition 2018.3.1helperspycharm	eamcitydiff_tools.py", line 32, in _patched_equals
    old(self, first, second, msg)
  File "C:Python37libunittestcase.py", line 839, in assertEqual
    assertion_func(first, second, msg=msg)
  File "C:Python37libunittestcase.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:Python37libunittestcase.py", line 59, in testPartExecutor
    yield
  File "C:Python37libunittestcase.py", line 615, in run
    testMethod()
  File "D:ProgramsPythonDemounittest4subtestDemo.py", line 15, in test_even
    self.assertEqual(i % 2, 0)


Process finished with exit code 1

 

以上是关于单元测试框架之unittest的主要内容,如果未能解决你的问题,请参考以下文章

单元测试框架之unittest

unittest单元测试框架之unittest 框架的总结2

unittest单元测试框架之测试结果输出到外部文件

unittest单元测试框架之unittest 框架的总结

单元测试框架之unittest

python单元测试之unittest框架