一, 摘要
前篇文章已经详细介绍了 unittest 框架的特性, 足以满足我们日常的测试工作, 但那并不是 unittest 的全部, 本片博文将介绍一些应该知道但未必能经常用到的内容
然而, 想全部掌握 unittest 还远远不够
二, 命令行模式执行用例
unittest 框架支持命令行执行测试模块, 测试类甚至单独的测试方法
执行测试模块: python -m unittest test_module1 test_module2 ...... 也可以采用路径的方式 python -m unittest tests/test_something.py, 如果想用一个高级的 verbosity 的方式执行加上参数 - v 即可, 例如 python -m unittest -v test_module
执行测试类: python -m unittest test_module1.Test_Class
执行测试方法: python -m unittest test_module1.Test_Class.test_method
如果想获取这种命令组合的 help, 则执行命令 python -m unittest -h 将得到如下帮助信息
- D:\Programs\Python\Demo\unittest1>python -m unittest -h
- usage: python.exe -m unittest [-h] [-v] [-q] [--locals] [-f] [-c] [-b]
- [-k TESTNAMEPATTERNS]
- [tests [tests ...]]
- positional arguments:
- tests a list of any number of test modules, classes and test
- methods.
- optional arguments:
- -h, --help show this help message and exit
- -v, --verbose Verbose output
- -q, --quiet Quiet output
- --locals Show local variables in tracebacks
- -f, --failfast Stop on first fail or error
- -c, --catch Catch Ctrl-C and display results so far
- -b, --buffer Buffer stdout and stderr during tests
-k TESTNAMEPATTERNS Only run tests which match the given substring 例如 -k foo 则会去匹配
- foo_tests.SomeTest.test_something
- ,
bar_tests.SomeTest.test_foo 去执行, 但是不会匹配
- bar_tests.FooTest.test_something
- .
- Examples:
- python.exe -m unittest test_module - run tests from test_module
- python.exe -m unittest module.TestClass - run tests from module.TestClass
- python.exe -m unittest module.Class.test_method - run specified test method
- python.exe -m unittest path/to/test_file.py - run tests from test_file.py
- usage: python.exe -m unittest discover [-h] [-v] [-q] [--locals] [-f] [-c]
- [-b] [-k TESTNAMEPATTERNS] [-s START]
- [-p PATTERN] [-t TOP]
- optional arguments:
- -h, --help show this help message and exit
- -v, --verbose Verbose output
- -q, --quiet Quiet output
- --locals Show local variables in tracebacks
- -f, --failfast Stop on first fail or error
- -c, --catch Catch Ctrl-C and display results so far
- -b, --buffer Buffer stdout and stderr during tests
- -k TESTNAMEPATTERNS Only run tests which match the given substring
- -s START, --start-directory START
- Directory to start discovery ('.' default)
- -p PATTERN, --pattern PATTERN
- Pattern to match tests ('test*.py' default)
- -t TOP, --top-level-directory TOP
- Top level directory of project (defaults to start
- directory)
- For test discovery all test modules must be importable from the top level
- directory of the project.
如果没有传参数, 那么将执行 Test Discovery, 例如输入命令 python -m unittest(它也等价于 python -m unittest discover) 并未给他任何模块, 类或者方法, 那么他将做的事情便是 Test Discovery
例如命令: python -m unittest discover -s project_directory -p "*_test.py" 本条命令中使用了参数 -s 和 -p ,-s 表示从那个目录开始, 默认为 (.), -p 则表示匹配哪样的文件名, 这条命令也等价于 python -m unittest discover project_directory "*_test.py"
输入 python -m unittest discover -h 将的到如何帮助, 很清楚的写明了各参数说明
- D:\Programs\Python\Demo\unittest1>python -m unittest discover -h
- usage: python.exe -m unittest discover [-h] [-v] [-q] [--locals] [-f] [-c]
- [-b] [-k TESTNAMEPATTERNS] [-s START]
- [-p PATTERN] [-t TOP]
- optional arguments:
- -h, --help show this help message and exit
- -v, --verbose Verbose output
- -q, --quiet Quiet output
- --locals Show local variables in tracebacks
- -f, --failfast Stop on first fail or error
- -c, --catch Catch Ctrl-C and display results so far
- -b, --buffer Buffer stdout and stderr during tests
- -k TESTNAMEPATTERNS Only run tests which match the given substring
- -s START, --start-directory START
- Directory to start discovery ('.' default)
- -p PATTERN, --pattern PATTERN
- Pattern to match tests ('test*.py' default)
- -t TOP, --top-level-directory TOP
- Top level directory of project (defaults to start
- directory)
- For test discovery all test modules must be importable from the top level
- directory of the project.
三, TestLoader
在前边的文章中, 我们说到了测试集合, 其中有一种方法是使用到了 unittest.TestLoader().loadTestsFromTestCase(TestClass), 这种方式是直接 loader() 了测试类
实际上 TestLoader 还有其他的方法
loadTestsFromModule(module, pattern=None)
loadTestsFromName(name, module=None) 和 loadTestsFromNames(names, module=None)
getTestCaseNames(testCaseClass) 返回一个存出测试方法名称的序列
discover(start_dir, pattern='test*.py', top_level_dir=None): 这个方法我们已经用到好多次了
defaultTestLoader: 他是一个 TestLoader 的实例, 如果我们不需要定制化的 TestLoader, 直接使用这个即可还能必变重复创建 TestLoader 实例
四, 总结
unittest 单元测试框架介绍到这里基本上满足了日常的工作需要, 然而这并不是 unittest 的全部, 它还有很多的特性, 读者可以直接访问 Python 官网查看 unittest 的 API 文档, 对于学习来说, 官方文档是最好的书籍
来源: http://www.bubuko.com/infodetail-2900822.html