问题描述
问题陈述
下面是一个玩具示例,与我正在尝试的操作很接近。
@given(
idx_start=integers(min_value=0,max_value=100000),idx_window=integers(min_value=0,)
def test_calc_max(conftest_df,idx_start,idx_window):
row_idxs = conftest_df.index[idx_start : (idx_start + idx_window)]
assert calc_max(conftest_df.loc[row_idxs,"my_column"]) >= 0
conftest_df
是我在conftest.py
固定文件中提供的数据框,它代表了我用于包装的真实数据的一部分。
此数据框中的值很少 NaN
。我想使用hypothesis
,因为它很棒,而且我坚信这是做事的正确方法。
但是我还想确保被测试的方法和函数适用于NaN。我真的不是只想说NaN
,将来可能还会出现其他情况(例如,使用逗号而不是句点代表小数的数字)。
通过hypothesis
的理想解决方案
我宁愿能够做这样的事情:
@given(
idx_start=integers(min_value=0,max_value=100000,includes=[5,4000,80000]),includes=[20]),)
.
.
.
并有一种方法确保通过includes
参数考虑某些值。
我知道hypothesis
会跟踪失败的值,但是根据我的经验,这似乎不能保证它们的使用。
有没有办法做我想做的事?
解决方法
您可以使用@example
装饰器来确保正在测试某些示例。
这是hypothesis's quick start guide中的示例:
from hypothesis import given,example
from hypothesis.strategies import text
@given(text())
@example('')
def test_decode_inverts_encode(s):
assert decode(encode(s)) == s
在这里,@example('')
确保以空白字符串为例运行测试。
我还没有完全了解您的方案的详细信息,但是也许您可以根据自己的需要调整此示例。