简体   繁体   English

使用假设测试 Python function 引发错误

[英]Test Python function raising an Error using Hypothesis

Is there a way to use Hypothesis to check if a Python function raises an Error when a particular set of arguments are passed to it?有没有办法使用Hypothesis来检查 Python function 是否在一组特定的 arguments 传递给它时引发错误? As in, I want the assert to be True if the function fails and not make the test execution stop and the function marked as having failed.如,如果 function 失败并且不使测试执行停止并且 function 标记为失败,我希望断言为True

Yes, just as you would for a non-Hypothesis test, For example, you could use with pytest.raises(...): , or try: ... ; except: pass; else: raise AssertionError("did not raise error")是的,就像您进行非假设测试一样,例如,您可以使用with pytest.raises(...): ,或try: ... ; except: pass; else: raise AssertionError("did not raise error") try: ... ; except: pass; else: raise AssertionError("did not raise error") try: ... ; except: pass; else: raise AssertionError("did not raise error") . try: ... ; except: pass; else: raise AssertionError("did not raise error")

If you mean that it is only expected to fail with an exact set of arguments, this seems like a case for traditional example-based tests;如果您的意思是它只期望通过一组精确的 arguments 失败,这似乎是传统的基于示例的测试的情况; if you mean some arguments and not others I'd write a separate test for each case but you could get away with an if statement too.如果您的意思是一些 arguments 而不是其他人,我会为每种情况编写一个单独的测试,但您也可以使用if语句。

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

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