chzhiyi / -KnowledgeShare

6 stars 1 forks source link

20190218 - unittest框架介绍(下) - qiaosong #13

Open chzhiyi opened 5 years ago

chzhiyi commented 5 years ago

对<上>的补充

  • 参见http://pyunit.sourceforge.net/pyunit_cn.html,摘出以下部分:
  • 如果setUp方法在测试运行时抛出异常,框架会认为测试遇到了错误并且 testCase不会被执行。
  • 如果setUp执行成功, 那么无论testCase是否成功,tearDown方法都将被执行。
  • 测试代码放置位置(对应于"orgnizing test code") 你可以将测试用例定义与被测试代码置于同一个模块中(例如“widget.py”),但是将测试代码放置在单独的模块中(如“widgettests.py”)会有一些优势:
    • 测试模块可以从命令行单独执行
    • 测试代码可以方便地从发布代码中分离
    • 少了在缺乏充足理由的情况下为适应被测试代码而更改测试代码的诱惑
    • 相对于被测试代码,测试代码不应该被频繁的修改
    • 被测试代码可以更方法的进行重构
    • 既然C语言代码的测试应该置于单独的模块,那何不保持这个一致性呢?
    • 如果测试策略改变,也无需修改被测试源代码

Re-using old test code(复用旧测试代码)

Some users will find that they have existing test code that they would like to run from unittest, without converting every old test function to a TestCase subclass.(部分用户希望将已有的测试代码不需转变为TestCase子类而直接从unittest中运行)

For this reason, unittest provides a FunctionTestCase class. This subclass of TestCase can be used to wrap an existing test function. Set-up and tear-down functions can also be provided.(unittest提供了一个FunctionTestCase类。这个TestCase子类可以用来封装已有测试函数。set-up和tear-down也可以选择性地被包装)

Skipping tests and expected failures 跳过<指定的、期望结果是失败的>测试用例

支持跳过独立的测试方法、甚至是跳过整个类中所有的测试方法;另外,支持将某个test标识为"expected failure",意味着该test坏掉了且执行时会失败,但它不会计入TestResult中的失败个数。

最简单的跳过一个test是使用skip()装饰器,或是它的conditional variants(条件变种)。

  • @unittest.skip(reason) 由什么原因跳过测试
  • @unittest.skipIf(condition, reason) 满足 condition 条件时跳过
  • @unittest.skipUnless(condition, reason) 不满足 条件时跳过
  • @unittest.expectedFailure 如果失败 ,不包含在失败结果中
  • exception unittest.SkipTest(reason) 报错错误
  • 装饰器都可以用于方法或类

Distinguishing test iterations using subtests 使用subtests区分测试迭代

When there are very small differences among your tests, for instance some parameters, unittest allows you to distinguish them inside the body of a test method using the subTest() context manager.(当tests之间存在很小的区别时,例如某些参数,unittest允许您使用subTest()上下文管理器在同一个方法体内区分它们。)

Grouping tests

class unittest.TestLoader

The TestLoader class is used to create test suites from classes and modules. Normally, there is no need to create an instance of this class; the unittest module provides an instance that can be shared as unittest.defaultTestLoader. Using a subclass or instance, however, allows customization of some configurable properties.(TestLoader是用来加载TestCase到TestSuite中的,其中有几个loadTestsFrom__()方法,就是从各个地方寻找TestCase,创建它们的实例,然后add到TestSuite中,再返回一个TestSuite实例。)

class unittest.TestResult

This class is used to compile information about which tests have succeeded and which have failed. A TestResult object stores the results of a set of tests. The TestCase and TestSuite classes ensure that results are properly recorded; test authors do not need to worry about recording the outcome of tests. Testing frameworks built on top of unittest may want access to the TestResult object generated by running a set of tests for reporting purposes; a TestResult instance is returned by the TestRunner.run() method for this purpose.

class unittest.TextTestResult(stream, descriptions, verbosity)

A concrete implementation of TestResult used by the TextTestRunner.

class unittest.TextTestRunner(stream=None, descriptions=True, verbosity=1, failfast=False, buffer=False, resultclass=None, warnings=None, *, tb_locals=False)

A basic test runner implementation that outputs results to a stream. If stream is None, the default, sys.stderr is used as the output stream. This class has a few configurable parameters, but is essentially very simple. Graphical applications which run test suites should provide alternate implementations. Such implementations should accept **kwargs as the interface to construct runners changes when features are added to unittest.

run(test)

This method is the main public interface to the TextTestRunner. This method takes a TestSuite or TestCase instance. A TestResult is created by calling _makeResult() and the test(s) are run and the results printed to stdout. TextTestRunner是来执行测试用例的,其中的run(test)会执行TestSuite/TestCase中的run(result)方法。 测试的结果会保存到TextTestResult实例中,包括运行了多少测试用例,成功了多少,失败了多少等信息。

参考网址

核心原理图

unittest

解释

unittest