看見英文懵逼,強迫學習英語
The Unittest suppots test automation,sharing of setup and shutdown code of tests, aggregation of tests into collections, and independence of the tests from the reporting framework
(支持測試自動化,為測試共享設置和關閉代碼,將測試集合到集合中,以及從報告框架中獨立測試。)
To achieve this,unittest supports some important concepts in an object_oriented way:
(為了實現這一點,unittest
以面向對象的方式支持一些重要的概念:)
整理結構:unittest庫提供了testcase,test suites,test fixtures, test runner:
test fixture:
A test fixture represents the preparation needed to perform one or more tests, and any associate cleanup actions. This may involve, for example, creating temporary or proxy databases, directories, or starting a server process.
test fixture代表執行一個或多個測試所需的准備工作,以及任何關聯清理操作。這可能涉及到,例如創建臨時代理數據庫、目錄或啟動服務器進程。
test case :
-
A
test case is the individual unit of testing. It checks for a specific response to a particular set of inputs.
unittest
provides a base class,TestCase
, which may be used to create new test cases.
測試用例是測試的各個單元。它檢查對特別輸入集合的特定響應。 unittest
提供了一個基類, TestCase
可以用來創建新的測試用例。
test suite:
A test suite is a collection of test cases, test suites, or both. It is used to aggregate tests that should be executed together.
一個測試套件是測試案例,測試套件,或兩者的集合。它被用來聚合應該一起執行的測試。
test runner:
A test runner is a component which orchestrates the execution of tests and provides the outcome to the user. The runner may use a graphical interface, a textual interface, or return a special value to indicate the results of executing the tests.
測試運行是編排的測試的執行,並提供結果給用戶的部件。test runner可以使用圖形界面,文本界面,或返回一個特殊的值來表示執行測試的結果。
The unittest
module provides a rich set of tools for constructing and running tests. This section demonstrates that a small subset of the tools suffice to meet the needs of most users.
該unittest
模塊為構建和運行測試提供了一套豐富的工具。本節表明,一小部分工具足以滿足大多數用戶的需求。
1、簡單的例子
Here is a short script to test three string methods:
1 import unittest 2 3 class TestStringMethods(unittest.TestCase): 4 def test_upper(self): 5 self.assertEqual('foo'.upper(), 'FOO') 6 7 def test_isupper(self): 8 self.assertTrue('FOO'.isupper()) 9 self.assertFalse('Foo'.isupper()) 10 11 def test_split(self): 12 s = 'hello word' 13 self.assertEqual(s.split(), ['hello', 'word']) 14 # check that s.split fails when the separator is not a string 15 with self.assertRaises(TypeError): 16 s.split(2) 17 18 if __name__ == '__main__': 19 unittest.main()
運行結果:
...
---------------------------------------------------------------------- Ran 3 tests in 0.000s OK
Passing the -v
option to your test script will instruct unittest.main()
to enable a higher level of verbosity, and produce the following output:
test_isupper (__main__.TestStringMethods) ... ok test_split (__main__.TestStringMethods) ... ok test_upper (__main__.TestStringMethods) ... ok ---------------------------------------------------------------------- Ran 3 tests in 0.001s OK
2、命令行:
從命令行中可以運行單元測試的模塊,類,甚至單獨的測試方法。
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
測試模塊也可以通過文件路徑指定:
python -m unittest tests/test_something.py
You can run tests with more detail (higher verbosity) by passing in the -v flag(顯示更詳細的測試結果的說明使用[-v]flag:)
ython3_server/python3_unittest# python3 -m unittest -v demo1.py
test_isupper (demo1.TestStringMethods) ... ok
test_split (demo1.TestStringMethods) ... ok
test_upper (demo1.TestStringMethods) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
查看所有的命令行選項使用命令:
python -m unittest -h
The -s
, -p
, and -t
options can be passed in as positional arguments in that order. The following two command lines are equivalent:
python -m unittest discover -s project_directory -p "*_test.py"
python -m unittest discover project_directory "*_test.py"
本TestCase
類提供了一些斷言方法來檢查並報告故障。下表列出了最常用的方法:
方法 | 檢查 |
---|---|
assertEqual(a, b) |
a == b |
assertNotEqual(a, b) |
a != b |
assertTrue(x) |
bool(x) is True |
assertFalse(x) |
bool(x) is False |
assertIs(a, b) |
a is b |
assertIsNot(a, b) |
a is not b |
assertIsNone(x) |
x is None |
assertIsNotNone(x) |
x is not None |
assertIn(a, b) |
a in b |
assertNotIn(a, b) |
a not in b |
assertIsInstance(a, b) |
isinstance(a, b) |
assertNotIsInstance(a, b) |
not isinstance(a, b) |
所有的assert方法都接受一個msg參數,如果指定的話,這個參數將被用作失敗時的錯誤消息
3、實際運用:
參考: