1. 程式人生 > 實用技巧 >Python 測試框架之 Unittest & Pytest

Python 測試框架之 Unittest & Pytest

歡迎關注【無量測試之道】公眾號,回覆【領取資源】,
Python程式設計學習資源乾貨、
Python+Appium框架APP的UI自動化、
Python+Selenium框架Web的UI自動化、
Python+Unittest框架API自動化、

資源和程式碼 免費送啦~
文章下方有公眾號二維碼,可直接微信掃一掃關注即可。

Unittest

Unittest是Python自帶的一個單元測試框架

Unittest中包含了對一些常規的測試用例體系性的支援,主要包括:

  • test fixture
  • test case
  • test suite
  • test runner

建立.py檔案,並選擇檔案型別如下圖所示:

點選OK後預設生成的檔案內容如下:

可以在此檔案的內容基礎上進行改造,寫自己的測試case,改造後的內容如下:

import unittest  # 匯入unittest模組
 
def number_sum(a, b):
    return a + b
 
"""
setUp():每個測試case執行之前執行
tearDown():每個測試case執行完之後執行
setUpClass():必須使用@classmethod 裝飾器,  所有case執行之前只執行一次
tearDownClass():必須使用@classmethod裝飾器, 所有case執行完之後只執行一次
"""
 
class MyTestCase(unittest.TestCase):
    @classmethod
    
def setUpClass(cls): print("setupClass") def setUp(self): print("setUp") def test_sum_int(self): self.assertEqual(number_sum(1, 2), 3) self.assertEqual(number_sum(100, 300), 400) def test_sum_number(self): self.assertEqual(number_sum(1.1, 2.2), 3.3)
def tearDown(self): print("tearDown") @classmethod def tearDownClass(cls): print("tearDownClass") if __name__ == '__main__': unittest.main()

以上程式碼是標準的unittest測試框架中的基礎結構。

Pytest

Pytest中定義測試用例主要有三種方式:

  • 相容Unittest,已有的Unittest庫和檔案都可以直接進行呼叫
  • 可以基於最簡單的類進行定義,如果一個類裡面沒有初始化方法並且以Test開頭,系統便會認為這是一個測試用例
  • 可以直接定義測試函式來定義測試用例

安裝Pytest:
1、 使用命令:pip install pytest
2、 在Pycharm編譯器裡的配置中新增,如下截圖所示

Pytest安裝成功後,將之前預設的runner由unittest修改為:pytest,修改步驟如下截圖所示:

將Pytest配置成預設的runner後,新建一個.py檔案為:test_demos.py
內容如下:

點選如上圖所示的綠色小箭頭,可檢視到第一行提示:使用Pytest來執行test_demos.py檔案,執行結果正常

將Python整合工具由Unittest切換成Pytest後,再來執行之前寫的使用Unittest框架編寫的程式碼,如圖所示:

類名前的綠色小箭頭提示可以使用:Pytest來執行test_unittest.py檔案,Pytest相容Unittest,可以直接使用Pytest直接呼叫Unittest庫和檔案。執行結果如下所示:

Pytest的用例識別規則:

  • Test類包含的所有test_方法
  • 不在class中的所有test_*函式
  • 類中不能初始化方法
  • 斷言使用基本的assert即可

檔案範圍:

  • test_*.py
  • *_test.py

用例執行順序的控制方法如下圖所示:

完整程式碼如下:

import pytest #匯入pytest模組
 
def func(x):
    return x + 1
 
def test_answer():
    assert func(3) == 5
 
 
class TestFunc:
    @classmethod
    def setup_class(self):
        print("setup_class")
 
    def setup(self):
        print("setup")
 
    @pytest.mark.fail
    def test_answer1(self):
        print("test_answer1")
        assert func(3) == 5
 
    @pytest.mark.success
    @pytest.mark.parametrize("input,expect",{
        (5, 6),
        (7, 8),
        (0, 1),
        (2, 2)
    })
    def test_answer2(self, input, expect):
        print("test_answer2")
        assert func(input) == expect
 
    def test_answer3(self):
        print("test_answer3")
        assert func(7) == 8
 
    def teardown(self):
        print("teardown")
 
    @classmethod
    def teardown_class(cls):
        print("teardown_class")

Pytest支援分組:
1、@pytest.mark.webtest
2、@pytest.mark.sec
3、Pytest -m “webtest and not sec”

呼叫方法如下圖所示:

在Terminal裡輸入命令:pytest -m fail來實現只執行指定的用例case的目的。
引數化用例的使用方法的例項程式碼如下所示:

@pytest.mark.parametrize("input,expect",{
    (5, 6),
    (7, 8),
    (0, 1),
    (2, 2)
})
def test_answer2(self, input, expect):
    print("test_answer2")
    assert func(input) == expect

引數化用例方法可以達到程式碼重複利用的效果。
單獨來執行test_answer2這個用例方法時會發現實際共執行了4次該方法,input與expect的值分別是引數化中的(5, 6),(7, 8),(0, 1),(2, 2),結果如下:

總結:今天分享的內容是Python測試框架中常用的框架Unittest與Pytest相關內容,在自動化測試過程中可以進行實操,提高測試效率。

備註:我的個人公眾號已正式開通,致力於測試技術的分享,包含:大資料測試、功能測試,測試開發,API介面自動化、測試運維、UI自動化測試等,微信搜尋公眾號:“無量測試之道”,或掃描下方二維碼:

新增關注,讓我們一起共同成長。