簡體   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)

我有一個 Python function ,它描述了語言 L ,它獲取一個單詞並返回 True 以防單詞在該語言中,否則返回 False 。 此外,我有一個確定性有限自動機(DFA),它描述了另一種語言 L2,我想檢查 L2=L。 我想也許我可以使用假設庫來獲得反例並區分 function 和 DFA,但我不知道如何以編程方式將假設結合到我的代碼中,而不是作為測試。 謝謝

看起來您將此問題作為假設問題跟蹤器上的兩個部分提出 - 感謝您按照建議將其移至此處:-) 將我的答案也移植到后代:

這里的關鍵見解是,您可以像其他任何方法一樣調用假設包裝的 function,並讓內部 function 保存其輸入。 例如:

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