如何使用鼻子的assert_raises? [英] How to use nose's assert_raises?

查看:143
本文介绍了如何使用鼻子的assert_raises?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索过文档,但找不到任何文档.有几对解释不多.

I've searched for documentation, but couldn't find any. There were a couple that didn't explain much.

有人可以向我解释鼻子的

Can someone explain to me Nose's

assert_raises(what should I put here?)

功能以及如何使用它?

推荐答案

assert_raises()函数进行测试以确保函数调用在出现某些参数时会引发指定的异常.

The assert_raises() function tests to make sure a function call raises a specified exception when presented with certain parameters.

例如,如果您有一个将两个数字相加的函数add,则在传递它时可能会抛出一个TypeError,例如整数和字符串.所以:

For example, if you had a function add that adds two numbers, it should probably raise a TypeError when you pass it, say, an integer and a string. So:

from nose.tools import assert_raises

def add(x, y):
    return x + y

assert_raises(TypeError, add, 2, "0")

第一个参数是您期望的异常类型.第二个是要调用的函数.其余参数将传递给函数(在这种情况下,它们将在函数内部变为xy).

The first argument is the exception type you expect. The second is the function to call. The rest of the arguments will be passed to the function (in this case, they will become x and y inside the function).

如果该函数引发了预期的异常,则断言通过.

If the expected exception is raised by the function, the assertion passes.

这篇关于如何使用鼻子的assert_raises?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆