python单元测试
python中的單元測試,主要有3種方式:
1.unittest
2.nose(nose2)
3.pytest
關(guān)于unittest的使用,非常簡單,繼承TeseCase:
https://docs.python.org/2/library/unittest.html#unittest.TestCase.setUp
setUp()
Method called to prepare the test fixture. This is called immediately before calling the test method; other than?AssertionError?or?SkipTest, any exception raised by this method will be considered an error rather than a test failure. The default implementation does nothing.
tearDown()
Method called immediately after the test method has been called and the result recorded. This is called even if the test method raised an exception, so the implementation in subclasses may need to be particularly careful about checking internal state. Any exception, other thanAssertionError?or?SkipTest, raised by this method will be considered an additional error rather than a test failure (thus increasing the total number of reported errors). This method will only be called if the?setUp()?succeeds, regardless of the outcome of the test method. The default implementation does nothing.
setUpClass()
A class method called before tests in an individual class are run.?setUpClass?is called with the class as the only argument and must be decorated as a?classmethod():
@classmethod def setUpClass(cls):...See?Class and Module Fixtures?for more details.
New in version 2.7.
tearDownClass()
A class method called after tests in an individual class have run.?tearDownClass?is called with the class as the only argument and must be decorated as a?classmethod():
@classmethod def tearDownClass(cls):...The?TestCase?class provides several assert methods to check for and report failures. The following table lists the most commonly used methods (see the tables below for more assert methods):
| 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 | 2.7 |
| assertIsNot(a,?b) | a?is?not?b | 2.7 |
| assertIsNone(x) | x?is?None | 2.7 |
| assertIsNotNone(x) | x?is?not?None | 2.7 |
| assertIn(a,?b) | a?in?b | 2.7 |
| assertNotIn(a,?b) | a?not?in?b | 2.7 |
| assertIsInstance(a,?b) | isinstance(a,?b) | 2.7 |
| assertNotIsInstance(a,?b) | not?isinstance(a,?b) | 2.7 |
有些數(shù)據(jù)需要mock的情況下,unittest.mock庫提供解決辦法。
如果要獲取詳情,則可以這樣:
you can run tests with more detailed information by passing in the verbosity argument:
if __name__ == '__main__':unittest.main(verbosity=2)nose ?pip install nose 或者 pip ?install nose2安裝,它會(huì)自動(dòng)尋找unittest.TestCase的派生類。
python經(jīng)典的單元測試框架:pytest
https://docs.pytest.org/en/latest/
后期重點(diǎn)使用pytest來進(jìn)行單元測試,內(nèi)容很多,也不在贅述,直接看官方文檔。
編寫pytest測試樣例的規(guī)則
- 測試文件以test_開頭(以_test結(jié)尾也可以)
- 測試類以Test開頭,并且不能帶有?init?方法
- 測試函數(shù)以test_開頭
- 斷言使用基本的assert即可
簡易教程:http://www.testclass.net/pytest/
單元測試用代碼覆蓋率來衡量,代碼覆蓋率是由特定的測試套件覆蓋被測試源代碼的程度來度量的。一般使用coverage.py度量覆蓋。
pip ?install coverage
nose2代碼覆蓋率通過cov-core來度量,可以以html格式在瀏覽器輸出。
pytest使用pytest-cov來度量代碼覆蓋率。
?
總結(jié)
以上是生活随笔為你收集整理的python单元测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 理解什么是真正的架构,架构需要的几种思维
- 下一篇: mac下kafka环境搭建 测试