pytest
1、安裝
pip install pytest
測試安裝是否成功
pytest --version
出現版本信息則安裝成功
2、pytest的優點
- 1、簡單靈活,容易上手,文檔豐富;
- 2、支持參數化,可以細粒度地控制要測試的測試用例;
- 3、能夠支持簡單的單元測試和複雜的功能測試,還可以用來做selenium/appnium等自動化測試、接口自動化測試(pytest+requests);
- 4、pytest具有很多第三方插件,並且可以自定義擴展,比較好用的如pytest-selenium(集成selenium)、pytest-html(完美html測試報告生成)、pytest-rerunfailures(失敗case重複執行)、pytest-xdist(多CPU分發)等;
- 5、測試用例的skip和xfail處理;
- 6、可以很好的和CI工具結合,例如jenkins
3、使用
注意點
- 測試文件以test_開頭(以_test結尾也可以)
- 測試類以Test開頭,並且不能帶有 init 方法
- 測試函數以test_開頭
- 斷言使用基本的assert即可
運行方式
1、pytest,自動尋找測試用例
2、pytest ***.py 運行模塊內的測試用例
3、pytest somepath 運行指定路徑下的測試用例
4、pytest test_mod.py::test_func 運行指定包下的測試用例
控制檯參數
-v 用於顯示每個測試函數的執行結果
-q 只顯示整體測試結果
-s 用於顯示測試函數中print()函數輸出
-x, --exitfirst, exit instantly on first error or failed test
-h 幫助
例子
test_demo.py
class TestDemmo(object):
def test_1(self):
print("test_1")
def test_2(self):
print("test_2")
4、使用擴展插件生成測試報告
安裝
pip install pytest-html
生成報告
pytest --html=report.html
將css樣式合併到html
pytest --html=report.html --self-contained-html
2、unittest
unittest 要求單元測試類必須繼承 unittest.TestCase,該類中的測試方法需要滿足如下要求:
測試方法應該沒有返回值。
測試方法不應該有任何參數。
測試方法應以test 開頭。
unittest.TestCase 內置了大量 assertXxx 方法來執行斷言
unittest.TestCase 包含了 setUp() 和 tearDown() 兩個方法,其中 setUp() 方法用於初始化測試;而 tearDown() 方法用於銷燬測試。程序會在運行每個測試用例(以 test_ 開頭的方法)之前自動執行 setUp() 方法來初始化測試固件,井在每個測試用例(以 test_ 開頭的方法)運行完成之後自動執行 tearDown() 方法來銷燬測試固件。
如果希望程序在該類的所有測試用例執行之前都用一個方法來初始化測試固件,在該類的所有測試用例執行之後都用一個方法來銷燬測試固件,則可通過重寫 setUpClass() 和 tearDownClass() 類方法來實現。
執行方法
1、在測試用例中添加
if __name__ == '__main__':
unittest.main()
python -m unittest 測試文件
或python -m unittest
自動尋找
TestSuite
測試包(TestSuite)可以組織多個測試用例,測試包還可以嵌套測試包。在使用測試包組織多個測試用例和測試包之後,程序可以使用測試運行器(TestRunner)來運行該測試包所包含的所有測試用例。
使用addTest或addTests添加測試用例,例如
suite = unittest.TestSuite()
suite.addTests([TestT('test_1'), TestT('test_2')])
import unittest
from BeautifulReport import BeautifulReport as BF
class TestT(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("setUpClass")
@classmethod
def tearDownClass(cls):
print('tearDownClass')
def test_1(self):
"""
測試相等
:return:
"""
unittest.TestCase.assertEqual(self, 3, 3, msg='測試失敗')
def test_2(self):
"""
測試大於
:return:
"""
unittest.TestCase.assertGreater(self, 4, 6, msg='測試失敗')
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTests([TestT('test_1'), TestT('test_2')])
runner = unittest.TextTestRunner()
runner.run(suite)
運行結果
生成更炫酷的測試報告
pip install BeautifulReport
import unittest
from BeautifulReport import BeautifulReport as BF
class TestT(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("setUpClass")
@classmethod
def tearDownClass(cls):
print('tearDownClass')
def test_1(self):
"""
測試相等
:return:
"""
unittest.TestCase.assertEqual(self, 3, 3, msg='測試失敗')
def test_2(self):
"""
測試大於
:return:
"""
unittest.TestCase.assertGreater(self, 4, 6, msg='測試失敗')
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTests([TestT('test_1'), TestT('test_2')])
# runner = unittest.TextTestRunner()
# runner.run(suite)
run = BF(suites=suite) # 實例化BeautifulReport模塊
run.report(filename='report.html', description='這個描述參數是必填的', theme='theme_memories')
# theme可選theme_cyan theme_candy theme_default