pytest 常用方法介紹


 前言

  之前一篇文章簡單介紹了 pytest 以及 pytest.fixture 裝飾器 :https://www.cnblogs.com/shenh/p/11572657.html 。實際在寫自動化測試腳本中,還會有一些很實用的方法,下文就來講述下這些用法。

 

一.pytest.mark.parametrize 裝飾器

 pytest 內置裝飾器 @pytest.mark.parametrize 可以讓測試數據參數化,把測試數據單獨管理,類似 ddt 數據驅動的作用,方便代碼和測試數據分離。

1.一次傳多個參數

import pytest

@pytest.mark.parametrize('x,y',[(1,2),(3,4)])
def test_sum(x,y):
    sum = x + y
    print(sum)

if __name__ =="__main__":
    pytest.main(['test_sample.py','-s'])

執行結果:

test_sample.py 
3
.
7
.

============================== 2 passed in 0.06s ==============================

 

2.組合傳參:

 注意:這種方式一共傳遞了4組參數  (1,3)、(1,4)、(2,3)、(2,4)。這種方式可以簡化測試數據,不用手動再將參數組合。

import pytest

@pytest.mark.parametrize('x',[1,2])
@pytest.mark.parametrize('y',[3,4])
def test_sum(x,y):
    sum = x + y
    print(sum)

if __name__ =="__main__":
    pytest.main(['test_sample.py','-s'])

 執行結果:

test_sample.py 
4
.
5
.
5
.
6
.

============================== 4 passed in 0.14s ==============================

 

二、fixture返回值

1.獲取被調用函數返回值

import pytest

@pytest.fixture(scope='function')
def login():
    accesstoken = '197ce8083c38467f'

    return accesstoken


def test_sum(login):
    token = login
    print(token)

if __name__ =="__main__":
    pytest.main(['test_sample.py','-s'])

 執行結果:

test_sample.py 
197ce8083c38467f
.

============================== 1 passed in 0.04s ==============================

 

 若被調用函數返回多個參數:

import pytest

@pytest.fixture(scope='function')
def login():
    accesstoken = '197ce8083c38467f'
    customerguid = '096799f5-e040-11e9-8c01-0242ac11000d'

    return accesstoken,customerguid


def test_sum(login):
    token = login[0]
    guid = login[1]
    print(token)
    print(guid)

if __name__ =="__main__":
    pytest.main(['test_sample.py','-s'])

 執行結果:

test_sample.py 
197ce8083c38467f
096799f5-e040-11e9-8c01-0242ac11000d
.

============================== 1 passed in 0.07s ==============================

 

2.單個用例調用多個函數

import pytest

@pytest.fixture(scope='function')
def login():
    print('登錄')

@pytest.fixture(scope='function')
def conn():
    print('連接數據庫')

def test_1(login,conn):
    print('測試用例1')

def test_2():
    print('測試用例2')

if __name__ =="__main__":
    pytest.main(['test_sample.py','-s'])

 執行結果:

test_sample.py 
登錄
連接數據庫
測試用例1
.
測試用例2
.

============================== 2 passed in 0.05s ==============================

 

三、測試用例分類

 有時候我們只需執行部分測試用例,比如從用例集當中挑選 smoke 測試,要怎么做呢?通過裝飾器 @pytest.mark.smoke,smoke 是可以自定義的,運行時加上命令‘-m=smoke’,pytest 就會挑選帶有裝飾器的類或函數運行。

import pytest

@pytest.fixture(scope='function')
def login():
    accesstoken = '197ce8083c38467f'
    customerguid = '096799f5-e040-11e9-8c01-0242ac11000d'

    return accesstoken,customerguid

@pytest.mark.smoke
def test_sum(login):
    token = login[0]
    guid = login[1]
    print(token)
    print(guid)

def test_2():
    print('測試用例')

if __name__ =="__main__":
    pytest.main(['test_sample.py','-s','-m=smoke'])

 執行結果:

test_sample.py 
197ce8083c38467f
096799f5-e040-11e9-8c01-0242ac11000d
.

======================= 1 passed, 1 deselected in 0.02s =======================

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM