1. 程式人生 > >單元測試框架之unittest(七)

單元測試框架之unittest(七)

一、摘要

前篇文章已經詳細介紹了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文件,對於學習來說,官方文件是最好的書籍