Python / Pandas:带有假设的单元测试-再现伪造的示例

问题描述

使用hypothesis库进行单元测试,我想知道如何重现一个伪造的示例pd.DataFrame

输出看起来像这样:

Falsifying example: test_data_frame_data(
    data=                            sec_1  sec_2  sec_3
    2020-01-01 00:00:00.000001    0.0   -0.0    0.0
    2020-01-01 00:00:00.000000    0.0   -0.0    0.0
    2020-01-01 00:00:00.000257    0.0   -0.0    0.0
    2020-01-01 00:00:00.000258    0.0   -0.0    0.0
    2020-01-01 00:00:00.000261    0.0   -0.0    0.0
    ...                           ...    ...    ...
    2020-01-01 00:00:01.526858    0.0   -0.0    0.0
    2020-01-01 01:00:01.065796    0.0   -0.0    0.0
    2020-01-01 01:00:01.065797    0.0   -0.0    0.0
    2020-01-01 01:01:01.065795    0.0   -0.0    0.0
    2020-01-01 00:01:01.000020    0.0   -0.0    0.0

    [300 rows x 3 columns],)

Hypothesis没有向seed打印@reproduce_failure提示

我只是想使用伪造的示例来调试我的代码

解决方法

我在假设GitHub问题页面(https://github.com/HypothesisWorks/hypothesis/issues/2584)上得到了提示。归功于@ Zac-HD。

第一个解决方案:
@settings(print_blob=True)放在测试之前。这样可以确保您获得提示,提示如何重现潜在的错误。

第二个解决方案:
注册设置配置文件(例如,在您的conftest.py中)

from hypothesis import settings
settings.register_profile(name="dev",max_examples=20,print_blob=True)

有关settings的所有信息都可以在官方文档中找到:https://hypothesis.readthedocs.io/en/latest/settings.html