ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

我所有的测试功能都在conftest.py中加载了一个固定装置,即使它们不需要它

2019-12-10 22:55:31  阅读:496  来源: 互联网

标签:fixtures pytest python


我的conftest.py中有2个不同的测试文件和一些固定装置:

1)“ Test_dummy.py”包含以下功能:

def test_nothing():
    return 1

2)“ Test_file.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)“ conftest.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

当我运行测试时,“ Test_dummy.py”也会尝试加载“ excelvalidation_io”固定装置,并且会产生错误:

In test_nothing: function uses no argument 'csvio'

我试图仅将灯具放置在“ Test_file.py”中,问题已解决,但是我读到,将所有灯具放置在conftest文件中是一种很好的做法.

解决方法:

函数pytest_generate_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

标签:fixtures,pytest,python
来源: https://codeday.me/bug/20191210/2104964.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有