Pytest簡介

前言

pytest是 python 的一個第三方單元測試框架,它繼承自 python 自帶的單元測試框架unittest,兼容 unittest

官方文檔地址: //docs.pytest.org/en/latest/contents.html

相比unittestpytest的可擴展性更高,也是目前最為流行的 python 單元測試框架。至於它擴展性表現在哪些方面,我們需在後續的學習中一點一點進行總結。

簡單使用

1 安裝

安裝pytest

pip install -U pytest

驗證安裝是否成功

pytest --version

2 用例編寫

為了先讓大家對pytest的使用有個粗淺的認識,接下來我們使用pytest對兩個自定義介面進行簡單測試,其中查詢所有用戶資訊介面為get請求,註冊用戶介面為post請求,編寫腳本test_demo.py,程式碼如下:

import pytest
import requests, json

class TestDemo:

    def test_get_all_users(self):
        '''查詢所有用戶資訊'''
        url = "//127.0.0.1:5000/users"
        # 請求介面
        res = requests.get(url=url).text
        res = json.loads(res)
        print(res)
        # 斷言
        assert res['code'] == 0

    def test_register(self):
        '''註冊用戶'''
        headers = {"Content-Type": "application/json;charset=utf8"}
        url = "//127.0.0.1:5000/register"
        data = {
            "username": "張學友",
            "password": "123456",
            "sex": "0",
            "telephone": "13823456789",
            "address": "北京東城區"
        }
        # 請求介面
        res = requests.post(url=url, headers=headers, json=data).text
        res = json.loads(res)
        print(res)
        # 斷言
        assert res['code'] == 0


if __name__ == '__main__':
    pytest.main()

注意,測試類中不能定義__init__方法。

3 用例執行

  • 方式一:在python程式碼里調用pytest,使用pytest.mian()

    # 執行當前目錄下的測試用例
    pytest.main()
    
    # 執行指定的測試用例
    pytest.main("testcase/test_one.py")
    
    # 加參數如-s,執行參數根據需要進行添加,後面文章會對常用的參數進行說明
    pytest.main(["-s", "testcase/test_one.py"])
    

    pycharm控制台輸出如下:

  • 方式二:命令行執行

    pytest命令行執行pytest + 測試文件路徑,示例如下

    pytest E:/apiAutoTest/test_demo.py
    
    # 加參數如-s
    pytest -s E:/apiAutoTest/test_demo.py
    

    除了上面的直接通過pytest命令調用外,還可以通過命令在python解釋器里調用,python -m 測試文件完整路徑,如下:

    python -m pytest E:/apiAutoTest/test_demo.py
    

結果如下:

總結

如果使用unittest框架編寫上面的測試用例的話,程式碼應該如下:

import unittest
import requests, json

class TestDemo(unittest.TestCase):

    def test_get_all_users(self):
        '''查詢所有用戶資訊'''
        url = "//127.0.0.1:5000/users"
        res = requests.get(url=url).text
        res = json.loads(res)
        self.assertEqual(res['code'], 0)

    def test_register(self):
        '''註冊用戶'''
        headers = {"Content-Type": "application/json;charset=utf8"}
        url = "//127.0.0.1:5000/register"
        data = {
            "username": "張學友",
            "password": "123456",
            "sex": "0",
            "telephone": "13823456789",
            "address": "北京東城區"
        }
        res = requests.post(url=url, headers=headers, json=data).text
        res = json.loads(res)
        self.assertEqual(res['code'], 0)

if __name__ == '__main__':
    unittest.main()

由這個簡單的測試用例,我們可以比較出pytestunittest 在測試用例的編寫及執行時不一樣的地方:

  1. pytest框架編寫測試用例時,只需要引入 pytest 模組,使用python源生態的 assert 進行斷言,且可以使用自帶的命令行執行測試用例。

  2. unittest框架編寫測試用例時,自定義的測試類需要繼承unittest.TestCase,斷言則使用的是unittest提供的斷言方式(如 assertEqual、assertTrue、assertIn等),沒有提供命令行方式執行測試用例。

當然,pytestunittest的區別遠不止這些,待我們後續慢慢道來。

Tags: