第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

pytest 鉤子可以使用夾具嗎?

pytest 鉤子可以使用夾具嗎?

不負(fù)相思意 2021-12-21 16:31:42
我知道夾具可以使用其他夾具,但是鉤子可以使用夾具嗎?我在網(wǎng)上搜索了很多,但沒有得到任何幫助。如果我在這里做錯(cuò)了,有人可以指出嗎?#conftest.py@pytest.fixture()def json_loader(request):       """Loads the data from given JSON file"""    def _loader(filename):        import json        with open(filename, 'r') as f:            data = json.load(f)        return data    return _loaderdef pytest_runtest_setup(item,json_loader): #hook fails to use json_loader    data = json_loader("some_file.json")     print(data)     #do something useful here with data運(yùn)行時(shí)出現(xiàn)以下錯(cuò)誤。pluggy.manager.PluginValidationError: 插件 'C:\some_path\conftest.py' for hook 'pytest_runtest_setup' hookimpl 定義:pytest_runtest_setup(item, json_loader) Argument(s) {'json_loader'} 在 hookimpl 中聲明但找不到在鉤子規(guī)范中即使我沒有將 json_loader 作為 arg 傳遞給 pytest_runtest_setup(),我也會(huì)收到一條錯(cuò)誤消息,指出“Fixture“json_loader”被直接調(diào)用。Fixtures 并不意味著被直接調(diào)用”
查看完整描述

2 回答

?
婷婷同學(xué)_

TA貢獻(xiàn)1844條經(jīng)驗(yàn) 獲得超8個(gè)贊

似乎當(dāng)前唯一支持的動(dòng)態(tài)實(shí)例化夾具request的getfixturevalue方法是通過夾具,特別是方法


這在 pytest 鉤子中的測(cè)試時(shí)間之前無法訪問,但您可以通過自己使用夾具來完成相同的操作


這是一個(gè)(人為的)示例:


import pytest


@pytest.fixture

def load_data():

    def f(fn):

        # This is a contrived example, in reality you'd load data

        return f'data from {fn}'

    return f



TEST_DATA = None



@pytest.fixture(autouse=True)

def set_global_loaded_test_data(request):

    global TEST_DATA

    data_loader = request.getfixturevalue('load_data')

    orig, TEST_DATA = TEST_DATA, data_loader(f'{request.node.name}.txt')

    yield   

    TEST_DATA = orig



def test_foo():

    assert TEST_DATA == 'data from test_foo.txt'


查看完整回答
反對(duì) 回復(fù) 2021-12-21
?
楊__羊羊

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊

有一種方法可以從測(cè)試中獲取使用過的夾具。


#Conftest.py#


def pytest_runtest_makereport(item, call):

    if call.when == 'call':

        cif_fixture = item.funcargs["your_cool_fixture"]

        print(cif_fixture)


#test_file.py#

@pytest.fixture(scope="module")

def your_cool_fixture(request):

    return "Hi from fixture"



def test_firsttest(your_cool_fixture):

    print(your_cool_fixture)


查看完整回答
反對(duì) 回復(fù) 2021-12-21
  • 2 回答
  • 0 關(guān)注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)