pytest測試框架1-強大的Fixture功能

pytest測試框架1-強大的Fixture功能

版權聲明:本文爲博主原創文章,遵循 CC 4.0 by-sa 版權協議,轉載請附上原文出處連接和本聲明。session

本文連接:https://blog.csdn.net/qq_30758629/article/details/95923286框架

1.fixture是 幹什麼用的??函數

fixture是在測試函數運行先後,由pytest執行的外殼函數;代碼能夠定製,知足多變的測試需求;測試

包括定義傳入測試中的數據集,配置測試前系統的初始狀態,爲批量測試提供數據源等等...url

fixture是pytest用於將測試先後進行預備,清理工做的代碼分離出核心測試邏輯的一種機制!spa

2.舉個例子.net

@pytest.fixture()
def some_data():
    return 42

def test_some_data(some_data):
    assert some_data == 42

#@pytest.fixture() 裝飾器用於聲明函數是一個fixturecode

若是測試函數的參數列表中包含fixture名字,那麼pytest會檢測到,blog

檢測順序是:優先搜索該測試所在的模塊,而後搜索conftest.pyget

並在測試函數運行以前執行該fixture,

fixture能夠完成測試任務,也能夠返回數據給測試函數

pytest --setup-show test_example1.py

3.fixture函數放在哪裏合適?

1.能夠放在單獨的測試文件裏

2.若是但願多個測試文件共享fixture,能夠放在某個公共目錄下新建一個conftest文件,將fixture放在裏面。

4.使用fixture傳遞測試數據

fixture很是適合存放測試數據,而且他能夠返回任何數據

@pytest.fixture()
def a_list():
    return [1,2,3,44,5]

def test_a_list(a_list):
    assert a_list[2] == 3

5.指定fixture做用範圍

fixture裏面有個scope參數能夠控制fixture的做用範圍:session > module > class > function

1)function
每個函數或方法都會調用 \
@ pytest.fixture()
def first():
    print("\n獲取用戶名")
    a = "xiaoyulaoshi"
    return a
@pytest.fixture(scope="function")
def sencond():
    print("\n獲取密碼")
    b = "123456"
    return b

def test_1(first):
    '''用例傳fixture'''
    print("測試帳號:%s" % first)
    assert first == "xiaoyulaoshi"

def test_2(sencond):
    '''用例傳fixture'''
    print("測試密碼:%s" % sencond)
    assert sencond == "123456"

2).class 每個類調用一次,一個類能夠有多個方法
@pytest.fixture(scope="class")
def first():
    print("\n獲取用戶名,scope爲class級別只運行一次")
    a = "xiaoyulaoshi"
    return a

class TestCase():
    def test_1(self, first):
        '''用例傳fixture'''
        print("測試帳號:%s" % first)
        assert first == "xiaoyulaoshi"

    def test_2(self, first):
        '''用例傳fixture'''
        print("測試帳號:%s" % first)
        assert first == "xiaoyulaoshi"

3).module,每個.py文件調用一次,該文件內又有多個function和class

import pytest
@pytest.fixture(scope="module")
def first():
    print("\n獲取用戶名,scope爲module級別當前.py模塊只運行一次")
    a = "xiaoyulaoshi"
    return a
    
def test_1(first):
    '''用例傳fixture'''
    print("測試帳號:%s" % first)
    assert first == "xiaoyulaoshi"


class TestCase():
    def test_2(self, first):
        '''用例傳fixture'''
        print("測試帳號:%s" % first)
        assert first == "xiaoyulaoshi"

4).session
是多個文件調用一次,能夠跨.py文件調用,每一個.py文件就是module
當咱們有多個.py文件的用例時候,若是多個用例只需調用一次fixture,那就能夠設置爲scope = "session",而且寫到conftest.py文件裏

conftest.py

import pytest
@pytest.fixture(scope="session")
def first():
    print("\n獲取用戶名,scope爲session級別多個.py模塊只運行一次")
    a = "xiaoyulaoshi"
    return a

test_fixture11.py
import pytest
def test_1(first):
    '''用例傳fixture'''
    print("測試帳號:%s" % first)
    assert first == "xiaoyulaoshi"
import pytest
def test_2(first):
    '''用例傳fixture'''
    print("測試帳號:%s" % first)
    assert first == "xiaoyulaoshi"

6.fixture的參數化

pytest支持在多個完整測試參數化方法:

1).pytest.fixture(): 在fixture級別的function處參數化

2).@pytest.mark.parametrize:容許在function或class級別的參數化,爲特定的測試函數或類提供了多個argument/fixture設置。

3_.pytest_generate_tests:能夠實現本身的自定義動態參數化方案或擴展。

import pytest
import requests
par_to_test=[{
      "case": "serach a word :haha",
      "headers": {},
      "querystring": {
        "wd":"hah"
      },
      "payload": {},
      "expected": {
        "status_code":200
      }
    },

{
      "case": "serach a word2 :kuku",
      "headers": {},
      "querystring": {
        "wd":"kuku"
      },
      "payload": {},
      "expected": {
        "status_code":200
      } },
{
      "case": "serach a word3 :xiaoyulaoshi",
      "headers": {},
      "querystring": {
        "wd":"xiaoyulaoshi"
      },
      "payload": {},
      "expected": {
        "status_code":200
      } }
]

@pytest.fixture(params = par_to_test)
def class_scope(request):
    return request.param
def test_baidu_search(class_scope):
    url = "https://www.baidu.com"
    r = requests.request("GET", url, data=class_scope["payload"], headers=class_scope["headers"], params=class_scope["querystring"])
    assert r.status_code == class_scope["expected"]["status_code"]
相關文章
相關標籤/搜索