欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解pytest傳遞參數(shù)的幾種方式

 更新時(shí)間:2024年03月28日 09:31:36   作者:LetsStudy  
本文主要介紹了詳解pytest傳遞參數(shù)的幾種方式,詳細(xì)的介紹了4種傳參方式,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

 

測(cè)試類(lèi)內(nèi)部,屬性傳遞

import pytest

class Test_Case:
    t = 0

    def test_c(self):
        self.t = self.t + 1
        assert self.t == 1

    def test_d(self):
        self.t = self.t + 1
        assert self.t == 1

# t是測(cè)試類(lèi)的屬性,可以為所有測(cè)試方法共享該值,該值是固定不變的

global方式傳遞

import pytest

s = {}


class Test_Case:
    
    def test_b(self):
        global s
        s['name'] = 'hello'
        print(s['name'])
        assert s['name'] == 'hello'

    def test_c(self):
        global s
        s['age'] = 18
        print(s)
        assert s['age'] == 18


# global聲明的變量可以在整個(gè)測(cè)試類(lèi)中共享,值是可變的,global可以去掉,效果相同

@pytest.mark.parametrize()

import pytest

class Test_Case:
    @pytest.mark.parametrize("x", [1, 2, 3, 4])  # 傳遞單個(gè)值
    def test_b(self, x):
        assert x != 5

    @pytest.mark.parametrize("x,y", [(1, 2), (3, 4), (2, 3), (4, 6)])  # 多參數(shù),傳遞元組
    def test_c(self, x, y):
        print(x + y)
        assert x + y != 5

    @pytest.mark.parametrize("x,y", [{1, 2}, {3, 4}, {2, 3}, {4, 6}])  # 多參數(shù)傳遞集合
    def test_d(self, x, y):
        print(x + y)
        assert x + y != 6

    @pytest.mark.parametrize("x", [{"a": 1, "b": 2}, {"a": 1, "c": 4}])  # 傳遞字典
    def test_e(self, x):
        print(x)
        assert x["a"] == 1

    @pytest.mark.parametrize("x,y", [({"a": 1, "b": 2}, {"a": 3, "c": 4})])  # 多參數(shù)傳遞字典
    def test_f(self, x, y):
        assert x["a"] == 1

    @pytest.mark.parametrize("x", [{"a": 1, "b": 2}])  # 裝飾器疊加,傳遞多參數(shù)
    @pytest.mark.parametrize("y", [{"a": 1, "b": 2}])
    def test_g(self, x, y):
        assert y["a"] == 1

    @pytest.mark.parametrize(
        "test_input,expected",
        [("3+5", 8), ("2+4", 6), pytest.param("6*9", 42, marks=pytest.mark.xfail)],
    )  # xfail標(biāo)記
    def test_h(self, test_input, expected):
        assert eval(test_input) == expected

    @pytest.mark.parametrize(
        "test_input,expected",
        [("3+5", 8), ("2+4", 6), pytest.param("6*9", 42, marks=pytest.mark.skip)],
    )  # skip標(biāo)記
    def test_i(self, test_input, expected):
        assert eval(test_input) == expected

fixtrue傳遞

import pytest

class Test_Case:

    @pytest.fixture
    def get_d(self):  # 通過(guò)fixture值傳遞
        return [1, 2, 3]

    def test_a(self, get_d):
        x = get_d[0]
        assert x == 1
import pytest

class Test_Case:
# params = 'hello'等同于params = ['h','e','l','l','o']
    @pytest.fixture(params='hello') 
    def get_c(self, request):
        print(request.param)
        return request.param

    def test_c(self, get_c):
        name = get_c
        assert name == 'h'

    @pytest.fixture(params=[1, 2], ids=['hello', 'name'])  # 可以通過(guò)pytest -k <ids>執(zhí)行指定的用例
    def get_d(self, request):
        return request.param

    def test_d(self, get_d):
        name = get_d
        assert name == 2

    @pytest.fixture(params=[0, 1, pytest.param(2, marks=pytest.mark.skip)])
    def data_set(self, request):
        return request.param

    def test_f(self):
        pass
import pytest

#fixture嵌套傳遞
class Test_Case:

    @pytest.fixture(params=[0, 1, pytest.param(2, marks=pytest.mark.skip)])
    def data_set(self, request):
        return request.param

    @pytest.fixture()
    def data_s(self, data_set):
        print(data_set)
        return data_set

    def test_g(self, data_s):
        assert data_s == 1
# yield傳遞
import pytest

class Test_Case:
    @pytest.fixture
    def s(self):
        c = 'test'
        yield c

    def test_name(self, s):
        assert s == "test"

配置文件傳遞

# test_case.py
import pytest
import _case.constant as d

class Test_Case:

    def test_g(self):
        d.data = 2
        assert d.data == 2

    def test_h(self):
        assert d.data == 2

# _case.constant.py
data = 1

# 和global使用類(lèi)似,多個(gè)測(cè)試文件共享值,但是多個(gè)文件共享該值時(shí),會(huì)收到測(cè)試文件的執(zhí)行順序影響
#  global只能在一個(gè)測(cè)試文件中共享值

conftest.py

# conftest.py最好是在項(xiàng)目根目錄或者測(cè)試文件所在目錄
import pytest

@pytest.fixture(scope='session')
def say():
    return 'hello'

# test_case.py
import pytest


class Test_Case:

    def test_g(self, say):
        assert say == 'hello'

命令行參數(shù)傳參

# conftest.py   全局變量使用
import pytest

def pytest_addoption(parser):
    parser.addoption("--file", default="test")

@pytest.fixture
def file_name(request):
    return request.config.getoption("--file")

# test_case.py
import pytest


class Test_Case:

    def test_name(self, file_name):
        assert file_name == "test"

# test_case.py或者直接在測(cè)試文件中通過(guò)pytestconfig獲取,示例如下
    def test_name(self, pytestconfig):
        print(pytestconfig.getoption('file'))
        assert pytestconfig.getoption("file") == "test"

鉤子函數(shù)傳參

# conftest.py
import pytest

def pytest_addoption(parser):
    parser.addoption("--file", default="test")

def pytest_generate_tests(metafunc):
    file = metafunc.config.getoption('--file')
    metafunc.parametrize("case_data", [file])

# test_case.py
import pytest


class Test_Case:

    def test_g(self, case_data):
        assert case_data == 'test'

到此這篇關(guān)于詳解pytest傳遞參數(shù)的幾種方式的文章就介紹到這了,更多相關(guān)pytest傳遞參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

 

相關(guān)文章

最新評(píng)論