首先:对此可能已经有数百个答案了,对此我感到很抱歉,但我只是..不明白。
我想在某些从csv文件解析的测试用例中使用列表。 由于我不想多次分析列表,因此我想在设置过程中进行该操作。
pytest文档为此建议了固定装置。 我认为我是这样使用它们的:
@pytest.fixture()
def init_my_list():
return my_service.read_from_csv('my_file.csv')
,然后在必要时致电:
def test_get_info():
test_date = datetime.datetime(year=2018, month=8, day=15, hour=6)
assert (some, values) == myservice.get_prediction_for_datetime(test_date, init_my_list)
这将返回
TypeError:“函数”对象不可迭代
因为myservice.function遍历给定列表,但是固定装置返回一个函数而不是列表。
我需要的只是在安装程序中创建的列表,然后可以在我的测试用例中使用它。如何完成这个简单的任务?
按要求进行整个回溯:
def get_prediction_for_datetime(dt:日期时间,prediction_list:列表) ->(整数,浮点数):
尝试:result = next(x代表x在预测列表中,如果 dt.hour == x [0] .hour和dt.day == x [0] .day 和dt.month == x [0] .month和dt.year == x [0] .year)
Ë TypeError:“函数”对象为 不可迭代
答案 0 :(得分:1)
您必须将init_my_list
通过测试:
@pytest.fixture()
def init_my_list():
return [1, 2, 3]
def test_1(init_my_list):
assert init_my_list == [1, 2, 3] # will succeed
def test_2():
assert init_my_list == [1, 2, 3] # will fail
此外,默认情况下,每个函数仅对每个夹具调用一次。如果要每个 module 或 session 一次调用它,则需要将适当的scope=
参数传递给fixture
。 See the documentation