使用多個fixture的返回值作為測試用例的入參

2021-10-12 06:14:48 字數 2052 閱讀 6189

有時自動化場景需要使用多個fixture的返回值作為測試用例的入參。解決方法有兩個,乙個是使用fixture的params引數解決,乙個是使用pytest-lazy-fixture外掛程式

方法一:使用fixture的params引數解決

示例

import pytest

@pytest.fixture()

def get_env():

return 1

@pytest.fixture()

def get_env2():

return 2

# 寫乙個新的fixture,將get_env,get_env2作為引數傳入,

@pytest.fixture(params=["get_env", "get_env2"])

def get_system(request):

# getfixturevalue 的作用是獲取 fixture 的返回值

return request.getfixturevalue(request.param)

def test_example(get_system):

print(get_system)

assert get_system in [1, 2]

執行結果為

passed                            [ 50%]1

passed [ 100%]2

方法二:使用pytest-lazy-fixture外掛程式

pytest-lazy-fixture外掛程式的安裝

pip install pytest-lazy-fixture
示例:

import pytest

@pytest.fixture()

def get_env():

return 1

@pytest.fixture()

def get_env2():

return 2

@pytest.fixture(params=[pytest.lazy_fixture("get_env"),

pytest.lazy_fixture("get_env2")])

def get_system2(request):

return request.param

def test_example2(get_system2):

print(get_system2)

assert get_system2 in [1, 2]

執行結果為

passed                            [ 50%]1

passed [ 100%]2

**實際場景:**web自動化的時候,想在 chrome 和 firefox 瀏覽器上測試同一功能的測試用例

import pytest

from selenium import webdriver

@pytest.fixture

def chrome():

driver = webdriver.chrome()

yield driver

driver.quit()

@pytest.fixture

def firefox():

driver = webdriver.firefox()

yield driver

driver.quit()

@pytest.fixture(params=['chrome', 'firefox'])

def driver(request):

return request.getfixturevalue(request.param)

def test_func(driver):

driver.get("")

python函式多個返回值

python函式可以返回多個值嗎?答案是肯定的。比如在遊戲中經常需要從乙個點移動到另乙個點,給出座標 位移和角度,就可以計算出新的新的座標 import math def move x,y,step,angle 0 nx x step math.cos angle ny y step math.si...

Lua函式的多個返回值

lua中的函式的乙個很特殊也很有用的性質,即可以有多個返回值。包括一些內建的函式就是這樣。比如string.find函式,在給定的字串中查詢乙個pattern,如果有匹配的部分,則返回對應的頭 尾的兩個索引值 如果不存在匹配,則返回nil。當然,使用者定義的函式也可以有多個返回值,通過return關...

返回值包含多個變數的方法

直接丟 給我乙個陣列,返回乙個陣列 最大值,最小值,和 param array array是個引數是個陣列 returns 返回值是個陣列 第乙個元素是和,第二個元素是最小值,第三個元素是最大值 function getnum array 判斷最小值 if min array i 再定義乙個陣列儲存...