How to use nose's assert_raises?

前端 未结 2 865
粉色の甜心
粉色の甜心 2020-12-28 12:00

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_ra         


        
2条回答
  •  梦谈多话
    2020-12-28 12:05

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

    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")
    

    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.

提交回复
热议问题