标签:书籍 高级 demo ... 官方文档 path div ecif The
前篇文章已经详细介绍了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.
在前边的文章中,我们说到了测试集合,其中有一种方法是使用到了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):这个方法我们已经用到好多次了unittest单元测试框架介绍到这里基本上满足了日常的工作需要,然而这并不是unittest的全部,它还有很多的特性,读者可以直接访问Python官网查看unittest的API文档,对于学习来说,官方文档是最好的书籍
标签:书籍 高级 demo ... 官方文档 path div ecif The
原文地址:https://www.cnblogs.com/davieyang/p/10162478.html