python3_unittest單元測試框架

看見英文懵逼,強迫學習英語html

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 frameworkpython

(支持測試自動化,爲測試共享設置和關閉代碼,將測試集合到集合中,以及從報告框架中獨立測試。)數據庫

To achieve this,unittest supports some important concepts in an object_oriented way:服務器

(爲了實現這一點,unittest以面向對象的方式支持一些重要的概念:)框架

整理結構:unittest庫提供了testcase,test suites,test fixtures, test runner:ide

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模塊爲構建和運行測試提供了一套豐富的工具。本節代表,一小部分工具足以知足大多數用戶的需求。

一、簡單的例子

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 option to your test script will instruct to enable a higher level of verbosity, and produce the following output:
-vunittest.main()
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK

二、命令行:

從命令行中能夠運行單元測試的模塊,甚至單獨的測試方法

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) == b
assertNotEqual(a, b) != b
assertTrue(x) bool(x) is True
assertFalse(x) bool(x) is False
assertIs(a, b) is b
assertIsNot(a, b) is not b
assertIsNone(x) is None
assertIsNotNone(x) is not None
assertIn(a, b) in b
assertNotIn(a, b) not in b
assertIsInstance(a, b) isinstance(a, b)
assertNotIsInstance(a, b) not isinstance(a, b)

全部的assert方法都接受一個msg參數,若是指定的話,這個參數將被用做失敗時的錯誤消息

三、實際運用:

 

 

 

參考:

https://docs.python.org/3.6/library/unittest.html

https://www.jianshu.com/p/8e22c1213260 

相關文章
相關標籤/搜索