|
5 | 5 | ----------
|
6 | 6 | 问题
|
7 | 7 | ----------
|
8 |
| -You want the results of running unit tests written to a file instead of printed to standard |
9 |
| -output. |
| 8 | +你希望将单元测试的输出写到到某个文件中去,而不是打印到标准输出。 |
10 | 9 |
|
11 | 10 | |
|
12 | 11 |
|
13 | 12 | ----------
|
14 | 13 | 解决方案
|
15 | 14 | ----------
|
16 |
| -A very common technique for running unit tests is to include a small code fragment |
17 |
| -like this at the bottom of your testing file: |
| 15 | +运行单元测试一个常见技术就是在测试文件底部加入下面这段代码片段: |
18 | 16 |
|
19 |
| -import unittest |
| 17 | +.. code-block:: python |
20 | 18 |
|
21 |
| -class MyTest(unittest.TestCase): |
22 |
| - ... |
| 19 | + import unittest |
23 | 20 |
|
24 |
| -if __name__ == '__main__': |
25 |
| - unittest.main() |
| 21 | + class MyTest(unittest.TestCase): |
| 22 | + pass |
26 | 23 |
|
27 |
| -This makes the test file executable, and prints the results of running tests to standard |
28 |
| -output. If you would like to redirect this output, you need to unwind the main() call a |
29 |
| -bit and write your own main() function like this: |
| 24 | + if __name__ == '__main__': |
| 25 | + unittest.main() |
30 | 26 |
|
31 |
| -import sys |
32 |
| -def main(out=sys.stderr, verbosity=2): |
33 |
| - loader = unittest.TestLoader() |
34 |
| - suite = loader.loadTestsFromModule(sys.modules[__name__]) |
35 |
| - unittest.TextTestRunner(out,verbosity=verbosity).run(suite) |
| 27 | +这样的话测试文件就是可执行的,并且会将运行测试的结果打印到标准输出上。 |
| 28 | +如果你想重定向输出,就需要像下面这样修改 ``main()`` 函数: |
36 | 29 |
|
37 |
| -if __name__ == '__main__': |
38 |
| - with open('testing.out', 'w') as f: |
39 |
| - main(f) |
| 30 | +.. code-block:: python |
| 31 | +
|
| 32 | + import sys |
| 33 | +
|
| 34 | + def main(out=sys.stderr, verbosity=2): |
| 35 | + loader = unittest.TestLoader() |
| 36 | + suite = loader.loadTestsFromModule(sys.modules[__name__]) |
| 37 | + unittest.TextTestRunner(out,verbosity=verbosity).run(suite) |
| 38 | +
|
| 39 | + if __name__ == '__main__': |
| 40 | + with open('testing.out', 'w') as f: |
| 41 | + main(f) |
40 | 42 |
|
41 | 43 | |
|
42 | 44 |
|
43 | 45 | ----------
|
44 | 46 | 讨论
|
45 | 47 | ----------
|
46 |
| -The interesting thing about this recipe is not so much the task of getting test results |
47 |
| -redirected to a file, but the fact that doing so exposes some notable inner workings of |
48 |
| -the unittest module. |
49 |
| -At a basic level, the unittest module works by first assembling a test suite. This test |
50 |
| -suite consists of the different testing methods you defined. Once the suite has been |
51 |
| -assembled, the tests it contains are executed. |
52 |
| - |
53 |
| -These two parts of unit testing are separate from each other. The unittest.TestLoad |
54 |
| -er instance created in the solution is used to assemble a test suite. The loadTestsFrom |
55 |
| -Module() is one of several methods it defines to gather tests. In this case, it scans a |
56 |
| -module for TestCase classes and extracts test methods from them. If you want some‐ |
57 |
| -thing more fine-grained, the loadTestsFromTestCase() method (not shown) can be |
58 |
| -used to pull test methods from an individual class that inherits from TestCase. |
59 |
| -The TextTestRunner class is an example of a test runner class. The main purpose of |
60 |
| -this class is to execute the tests contained in a test suite. This class is the same test runner |
61 |
| -that sits behind the unittest.main() function. However, here we’re giving it a bit of |
62 |
| -low-level configuration, including an output file and an elevated verbosity level. |
63 |
| -Although this recipe only consists of a few lines of code, it gives a hint as to how you |
64 |
| -might further customize the unittest framework. To customize how test suites are |
65 |
| -assembled, you would perform various operations using the TestLoader class. To cus‐ |
66 |
| -tomize how tests execute, you could make custom test runner classes that emulate the |
67 |
| -functionality of TextTestRunner. Both topics are beyond the scope of what can be cov‐ |
68 |
| -ered here. However, documentation for the unittest module has extensive coverage |
69 |
| -of the underlying protocols. |
| 48 | +本节感兴趣的部分并不是将测试结果重定向到一个文件中, |
| 49 | +而是通过这样做向你展示了 ``unittest`` 模块中一些值得关注的内部工作原理。 |
| 50 | + |
| 51 | +``unittest`` 模块首先会组装一个测试套件。 |
| 52 | +这个测试套件包含了你定义的各种方法。一旦套件组装完成,它所包含的测试就可以被执行了。 |
| 53 | + |
| 54 | +这两步是分开的,``unittest.TestLoader`` 实例被用来组装测试套件。 |
| 55 | +``loadTestsFromModule()`` 是它定义的方法之一,用来收集测试用例。 |
| 56 | +它会为 ``TestCase`` 类扫描某个模块并将其中的测试方法提取出来。 |
| 57 | +如果你想进行细粒度的控制, |
| 58 | +可以使用 ``loadTestsFromTestCase()`` 方法来从某个继承TestCase的类中提取测试方法。 |
| 59 | +``TextTestRunner`` 类是一个测试运行类的例子, |
| 60 | +这个类的主要用途是执行某个测试套件中包含的测试方法。 |
| 61 | +这个类跟执行 ``unittest.main()`` 函数所使用的测试运行器是一样的。 |
| 62 | +不过,我们在这里对它进行了一些列底层配置,包括输出文件和提升级别。 |
| 63 | +尽管本节例子代码很少,但是能指导你如何对 ``unittest`` 框架进行更进一步的自定义。 |
| 64 | +要想自定义测试套件的装配方式,你可以对 ``TestLoader`` 类执行更多的操作。 |
| 65 | +为了自定义测试运行,你可以构造一个自己的测试运行类来模拟 ``TextTestRunner`` 的功能。 |
| 66 | +而这些已经超出了本节的范围。``unittest`` 模块的文档对底层实现原理有更深入的讲解,可以去看看。 |
0 commit comments