【发布时间】:2020-11-19 08:30:30
【问题描述】:
我是参数化和固定装置的新手,还在学习。我发现了一些使用间接参数化的帖子,但我很难根据我的代码中的内容来实现。如果有任何关于我如何实现这一目标的想法,我将不胜感激。
我的 conftest.py 中有几个固定装置,它们为我的测试文件中的函数“get_fus_output()”提供输入文件。该函数处理输入并生成两个数据帧以在我的测试中进行比较。此外,我将基于一个共同值('Fus_id')转租这两个 DF 来单独测试它们。所以这个函数的输出将是[(Truth_df1, test_df1),(Truth_df2, test_df2)...] 只是为了参数化每个测试和真值df的测试。不幸的是,我无法在我的测试函数“test_annotation_match”中使用它,因为这个函数需要一个夹具。
我无法将夹具作为输入提供给另一个夹具以进行参数化。是的,pytest 不支持它,但无法找到间接参数化的解决方法。
#fixtures from conftest.py
@pytest.fixture(scope="session")
def test_input_df(fixture_path):
fus_bkpt_file = os.path.join(fixture_path, 'test_bkpt.tsv')
test_input_df= pd.read_csv(fus_bkpt_file, sep='\t')
return test_input_df
@pytest.fixture
def test_truth_df(fixture_path):
test_fus_out_file = os.path.join(fixture_path, 'test_expected_output.tsv')
test_truth_df = pd.read_csv(test_fus_out_file, sep='\t')
return test_truth_df
@pytest.fixture
def res_path():
return utils.get_res_path()
#test script
@pytest.fixture
def get_fus_output(test_input_df, test_truth_df, res_path):
param_list = []
# get output from script
script_out = ex_annot.run(test_input_df, res_path)
for index, row in test_input_df.iterrows():
fus_id = row['Fus_id']
param_list.append((get_frame(test_truth_df, fus_id), get_frame(script_out, fus_id)))
# param_list eg : [(Truth_df1, test_df1),(Truth_df2, test_df2)...]
print(param_list)
return param_list
@pytest.mark.parametrize("get_fus_output", [test_input_df, test_truth_df, res_path], indirect=True)
def test_annotation_match(get_fus_output):
test, expected = get_fusion_output
assert_frame_equal(test, expected, check_dtype=False, check_like=True)
#OUTPUT
================================================================================ ERRORS ================================================================================
_______________________________________________________ ERROR collecting test_annotations.py
_______________________________________________________
test_annotations.py:51: in <module>
@pytest.mark.parametrize("get_fus_output", [test_input_df, test_truth_df, res_path], indirect=True)
E NameError: name 'test_input_df' is not defined
======================================================================= short test summary info ========================================================================
ERROR test_annotations.py - NameError: name 'test_input_df' is not defined
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=========================================================================== 1 error in 1.46s ===========================================================================
【问题讨论】:
-
@thebjorn 抱歉,现在更新了。
-
也许这也会有所帮助:stackoverflow.com/questions/45225950/…
标签: python python-3.x pandas unit-testing pytest