简体   繁体   English

如何以编程方式将假设结合到我的代码中而不是作为测试? (使用Hypothesis区分自动机和Python函数)

[英]How to combine Hypothesis in my code programaticly and not as a test? (Use Hypothesis to distinguish between automata and Python function)

I have a Python function which describes Language L which gets a word and returns True in case that the word is in the language and return False otherwise.我有一个 Python function ,它描述了语言 L ,它获取一个单词并返回 True 以防单词在该语言中,否则返回 False 。 In addition, I have a Deterministic Finite Automata (DFA) which describes another language L2 and I want to check if L2=L.此外,我有一个确定性有限自动机(DFA),它描述了另一种语言 L2,我想检查 L2=L。 I thought maybe I can use Hypothesis library to get counterexample and distinguish between the function and the DFA but I don't know how to combine Hypothesis in my code programmatically and not as a test.我想也许我可以使用假设库来获得反例并区分 function 和 DFA,但我不知道如何以编程方式将假设结合到我的代码中,而不是作为测试。 Thanks谢谢

It looks like you asked this question as two parts on the Hypothesis issue tracker - thanks for moving it over here as suggested:-) Porting my answer over too for posterity:看起来您将此问题作为假设问题跟踪器上的两个部分提出 - 感谢您按照建议将其移至此处:-) 将我的答案也移植到后代:

The key insight here is that you can call a Hypothesis-wrapped function like any other, and have the inner function save its inputs.这里的关键见解是,您可以像其他任何方法一样调用假设包装的 function,并让内部 function 保存其输入。 For example:例如:

counterexample = None

@given(x=st.integers())
def check(f, g, x):
    if f(x) != g(x):
        global counterexample
        counterexample = x
        raise AssertionError

with contextlib.suppress(AssertionError):
    check(f=math.sin, g=math.cos)

assert counterexample is not None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM