【发布时间】:2019-05-21 06:30:32
【问题描述】:
我想对我的代码进行单元测试,以确保代码的输出是正确的。
我也从 StackOverflow 尝试过,但没有工作:
input = pd.DataFrame.from_dict({
'rev': [0],
'price': [0]
})
expected = {
'data': ['cool']
}
assert_dict_equal(expected, data(input).to_dict(),
"oops, there's a bug...")
def temperature(row):
rev = row['rev']
price = row['price']
group = row['group']
if group == 'error':
return 'error'
elif revenue > 2 * price:
return 'cold'
elif revenue >= price:
return 'cool'
elif revenue < 0.5 * price:
return 'hot'
elif revenue < price:
return 'lukewarm'
else:
float('NA')
data['temp'] = sample.apply(temperature, axis=1)
assert temperature({"group": 'T2_Y2', "rev": 0, "price": 0},
"group", "rev", "price") == 'cool'
assert temperature({"group": 'T2_Y2', "rev": 30, "price": 10},
"group", "rev", "price") == 'cold'
assert temperature({"group": 'T2_Y2', "rev": 3, "price": 10},
"group", "rev", "price") == 'hot'
当输出给出错误时,预期的结果应该返回而没有错误。
TypeError: temperature() 接受 1 个位置参数,但给出了 4 个
【问题讨论】:
-
请提供您看到的完整错误回溯
-
实施编辑修正后仍然报错。这是错误:TypeError: temperature() 需要 1 个位置参数,但给出了 4 个。
标签: python pandas unit-testing dataframe assert