测试代码放置位置(对应于"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 跳过<指定的、期望结果是失败的>测试用例
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实例。)
TestLoader objects have the following methods:
loadTestsFromTestCase(testCaseClass):Return a suite of all test cases contained in the TestCase-derived testCaseClass.
loadTestsFromModule(module, pattern=None):Return a suite of all test cases contained in the given module. This method searches module for classes derived from TestCase and creates an instance of the class for each test method defined for the class.
loadTestsFromName(name, module=None):Return a suite of all test cases given a string specifier.
loadTestsFromNames(names, module=None):Similar to loadTestsFromName(), but takes a sequence of names rather than a single name. The return value is a test suite which supports all the tests defined for each name.
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实例中,包括运行了多少测试用例,成功了多少,失败了多少等信息。
对<上>的补充
Re-using old test code(复用旧测试代码)
Skipping tests and expected failures 跳过<指定的、期望结果是失败的>测试用例
Distinguishing test iterations using subtests 使用subtests区分测试迭代
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 moduleprovides 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实例。)loadTestsFromTestCase(testCaseClass)
:Return a suite of all test cases contained in the TestCase-derived testCaseClass.loadTestsFromModule(module, pattern=None)
:Return a suite of all test cases contained in the given module. This method searches module for classes derived from TestCase and creates an instance of the class for each test method defined for the class.loadTestsFromName(name, module=None)
:Return a suite of all test cases given a string specifier.loadTestsFromNames(names, module=None)
:Similar to loadTestsFromName(), but takes a sequence of names rather than a single name. The return value is a test suite which supports all the tests defined for each name.class unittest.TestResult
class unittest.TextTestResult(stream, descriptions, verbosity)
class unittest.TextTestRunner(stream=None, descriptions=True, verbosity=1, failfast=False, buffer=False, resultclass=None, warnings=None, *, tb_locals=False)
参考网址
核心原理图
解释