Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
606 views
in Technique[技术] by (71.8m points)

python - Pytest fixtures not found when called inside a non-test function

I want to do call pytest fixtures on a normal function (not a test function). For example:

Below code is sitting inside conftest.py

DATA_PATH_TEMP = '/data/data_temp.yaml'
@pytest.fixture()
def datapath():
    return DATA_PATH_TEMP

I have my test function on a test file:

@pytest.fixture()
def data_metadata(datapath):
    return datapath + '.yaml'

def cleanup_data(*args):
    for arg in args:
        if os.path.exists(arg):
            os.remove(arg)

def test_data_tmp(datapath):
    xxxxx
    cleanup_data(datapath, data_metadata)

I tried this code and it didn't work and threw me an error that datapath fixture is not recognized. I also wonder if it is possible to save that fixture as a global variable, so that I can make something like this:

DATAPATH = ... # the return of the fixture
def cleanup_data():
    if os.path.exists(DATAPATH):
        os.remove(DATAPATH)

Thanks!

question from:https://stackoverflow.com/questions/65883737/pytest-fixtures-not-found-when-called-inside-a-non-test-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...