我所有的测试函数都在加载确认测试.py,即使他们不需要我

2024-04-26 21:53:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个不同的测试文件和一些夹具在我的电脑确认测试.py地址:

1)“试验_假人.py“其中包含以下函数:

def test_nothing():
    return 1

2)“试验_文件.py". 其中包含以下函数:

def test_run(excelvalidation_io):
    dfInput, expectedOutput=excelvalidation_io
    output=run(dfInput)
    for key, df in expectedOutput.items():
        expected=df.fillna(0)
        real=output[key].fillna(0)
        assert expected.equals(real)

3英寸)确认测试.py“其中包含以下装置:

def pytest_generate_tests(metafunc):
    inputfiles=glob.glob(DATADIR+"**_input.csv", recursive=False)
    iofiles=[(ifile, getoutput(ifile)) for ifile in 
    inputfiles]
    metafunc.parametrize("csvio", iofiles)
@pytest.fixture
def excelvalidation_io(csvio):
    dfInput, expectedOutput= csvio
    return(dfInput, expectedOutput)
@pytest.fixture
def client():
    client = app.test_client()
    return client

当我运行测试时,“测试_假人.py”也尝试加载“excelvalidation\u io”装置,并产生错误:

In test_nothing: function uses no argument 'csvio'

我试着把夹具放在“测试”里_文件.py“问题解决了,但是我读到在conftest文件中定位所有的fixture是一个很好的实践。你知道吗


Tags: 文件pyiotestclientreturnpytestdef
1条回答
网友
1楼 · 发布于 2024-04-26 21:53:45

函数pytest\u generate\u tests是一个特殊函数,它总是在执行任何测试之前被调用,因此在这种情况下,您需要检查metafunc是否接受名为“csvio”的参数,并且不做任何其他操作,如:

def pytest_generate_tests(metafunc):
    if "excelvalidation_io" in metafunc.fixturenames:
        inputfiles=glob.glob(DATADIR+"**_input.csv", recursive=False)
        iofiles=[(ifile, getoutput(ifile)) for ifile in 
        inputfiles]
        metafunc.parametrize("csvio", iofiles)

Source

相关问题 更多 >