2

I have some function:

def reverse_number(num):
    try:
        return int(num)
    except ValueError:
        return "Please provide number"

and test for this:

assert_raises(ValueError, reverse.reverse_number, "error")

But when I call nosetests I got this error:

AssertionError: ValueError not raised

What am I doing wrong?

1 Answer 1

4

The function reverse_number catches the exception, preveting the exception to be raised; cause the assertion failure because assert_raises call expects the function call raises the ValueError exception.

Simply not catching the exception, you can get what you want:

def reverse_number(num):
    return int(num)

Or, you can catch the exception, do something, and re-raise the exception using raise statement:

def reverse_number(num):
    try:
        return int(num)
    except ValueError:
        # do something
        raise  # <--- re-raise the exception
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.