Questions tagged [assertraises]
30 questions
47
votes
1 answer
assertRaises in python unit-test not catching the exception
Can somebody tell me why the following unit-test is failing on the
ValueError in test_bad, rather than catching it with assertRaises
and succeeding? I think I'm using the correct procedure and syntax,
but the ValueError is not getting caught.
I'm…

user3014653
- 735
- 2
- 8
- 13
13
votes
2 answers
Testing failure of an assignment with unittest
One of my attributes is a property where the setter calls a validation function that raises an exception if the new value is invalid:
pos.offset = 0
# @offset.setter calls validate(offset=0)
# PositionError: Offset may not be 0.
I'm trying to add a…

Lenna
- 1,445
- 9
- 21
12
votes
2 answers
Proper Assert_Raise Unit Testing and Use of Exception Class
I am working on Exercise 49 of Learn Ruby the Hard Way
The exercise asks to write a unit test for each function provided. One of the items I am testing is if a proper exception is raised. It is suggested that we use assert_raise for this…

Andrew Lauer Barinov
- 5,694
- 10
- 59
- 83
12
votes
1 answer
Custom exceptions in unittests
I have created my custom exceptions as such within errors.py
mapper = {
'E101':
'There is no data at all for these constraints',
'E102':
'There is no data for these constraints in this market, try changing market',
'E103':
…

Ludo
- 2,307
- 2
- 27
- 58
9
votes
1 answer
assertRaises() in a test cases that raises multiple exceptions
Is it possible to use assertRaises with multiple types of exceptions. Some thing like
assertRaises(RuntimeError, "error message")
assertRaises(Exception, "exception message")
both these errors occur in my code at different places with the same…

physicist
- 844
- 1
- 12
- 24
7
votes
4 answers
Use a custom failure message for `assertRaises()` in Python?
The Python 2.7 unittest docs say:
All the assert methods (except assertRaises(), assertRaisesRegexp()) accept a msg argument that, if specified, is used as the error message on failure
… but what if I want to specify the error message for…

Stu Cox
- 4,486
- 2
- 25
- 28
6
votes
3 answers
How do you test an instance method in Python with assertRaises?
I understand how to use assertRaises on a function or a lambda, but I wanted to use it on an instance method.
So for example, if I have a class calculator that does infinite precision arithmetic, I might write the test:
def setUp(self):
…

grasingerm
- 1,955
- 2
- 19
- 22
5
votes
2 answers
Exception statement not covered by self.assertRaises in python unit test cases
This is my function:
def get_value(request, param):
s = get_string(request, param)
value = re.search('(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)', s)
if not value:
print 'match not found!'
raise Exception('incorrect format: %s' % param)
test…

Prashant Kumar
- 308
- 2
- 3
- 19
4
votes
2 answers
Python assertRaises on user-defined exceptions
The following question was triggered by the discussion in this post.
Assume two files (foobar.py and foobar_unittest.py). File foobar.py contains a class (FooBar) with two functions (foo and bar). Function bar raises a built-in exception, function…

Michael Gruenstaeudl
- 1,609
- 1
- 17
- 31
2
votes
1 answer
using assertRaises - handling propagated exceptions
I have some code where I'm testing for a wrapped exception, when it failed and the exception propagated I thought the error message and back trace wasn't verbose enough, primarily because it didn't tell me what was expected vs. the test, I would…

Christopher Horler
- 71
- 2
- 8
2
votes
2 answers
How to test that tornado read_message got nothing to read
I have a Tornado chat and I'm doing some tests, most client messages generate a reply from the server, but others must not generate any reply.
I managed to do it with this code, waiting for the read timeout to occur, there is a better way to do…

Chemary
- 1,304
- 12
- 18
2
votes
1 answer
How to create/implement/test a new exception in python?
I clearly have some fundamental misunderstanding on how to raise exceptions in python. I'm including the simplest example of what I'm trying (and failing) to do. I am trying to create a new exception, and properly test whether it working.
import…

JBWhitmore
- 11,576
- 10
- 38
- 52
1
vote
1 answer
Cant understand results while using assertRaises in unit testing on a sqlalchemy code
I am trying to unit test my sqlalchemy code.
def add_user(*user_details):
try:
u = User(*user_details)
session.add(u)
session.commit()
except:
session.rollback()
Now in my…

codecool
- 5,886
- 4
- 29
- 40
1
vote
0 answers
Validation of JSON schema 'oneOf' fails unittest assertRaisesRegex
On using Python3.6 unittest assertRaisesRegex to validate some JSON there are differences in the output generated between validating code under the JSON oneOf keyword and code that is not. I am testing the removal of required properties.
The problem…

Dodomac
- 194
- 2
- 17
1
vote
1 answer
unittest: wxpython's event method raises exception but assertRaises does not detect it
I have a wxpython dialog which raises a TypeError exception when clicking the OK button. I would like to test the occurrence of the exception with unittest but the test does not work as expected. The output shows that the exception is raised. Anyhow…

Humbalan
- 677
- 3
- 14