Linked Questions
34 questions linked to/from How do you test that a Python function throws an exception?
209
votes
4
answers
218k
views
How to properly use unit-testing's assertRaises() with NoneType objects [duplicate]
I did a simple test case:
def setUp(self):
self.testListNone = None
def testListSlicing(self):
self.assertRaises(TypeError, self.testListNone[:1])
And I am expecting the test to pass, but I am ...
48
votes
1
answer
52k
views
assertRaises in python unit-test not catching the exception [duplicate]
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,...
18
votes
5
answers
45k
views
Testing in Python - how can I use assertRaises in testing using 'unittest'? [duplicate]
I am trying to do a simple test in Python using 'unittest', to see if a class throws an exception if it gets an unsuitable input for the constructor. The class looks like this:
class SummaryFormula:
...
3
votes
2
answers
13k
views
In Python3, how do you test exception handling within a unit test? [duplicate]
I have several unit tests that I'm working on for an api. The tests use @patch to mock the api calls. Some of the tests I wish to create should trigger exceptions. How do I handle that in a unit test?
...
0
votes
1
answer
1k
views
How to test functions that throw exceptions [duplicate]
Possible Duplicate:
How do you test that a Python function throws an exception?
I have to do white-box and black-box testing so i`m wondering how it is possible to test a function that trows an ...
0
votes
1
answer
943
views
Test if ValidationError was raised . Odoo 14 [duplicate]
I want to test if an exception was raised how can I do that?
In my file.py I have this function:
//file.py
def unlink(self):
for rec in self:
if rec.state in ('progress', 'done'):
...
0
votes
1
answer
891
views
Python Testing raised Value Error [duplicate]
I am just using the regular library ( not pytest) and trying to confirm a ValueError.
test:
data = get_time_zone("sun/10-21 tz:US/Eas2tern")
Result:
if zone not in pytz.all_timezones:
raise ...
2
votes
1
answer
423
views
How to test if exception is raised when calling socket.send [duplicate]
This is my first time testing a socket interface using mocking. I have a class which invoked the _socket.sendto() method. I would like to test if an exception is raised.
class socketUDP(object):
...
0
votes
1
answer
236
views
Testing exception is thrown when queue is empty [duplicate]
When I attempt to test the catching of an exception with a spsecific message when a Queue is empty the test fails.
Here is the Queue class:
import time
from multiprocessing import Lock, Process, Queue,...
0
votes
1
answer
49
views
Assert when ObjectDoesNotExitst is raise [duplicate]
I have this function which raises the ObjectDoesNotExist
def is_user_login(self,id):
try:
u = m.CustomUser.objects.get(id=id)
except ObjectDoesNotExist as e:
raise e
Now I am ...
0
votes
0
answers
41
views
How to compare in Python to TypeError using assert? [duplicate]
I write tests using Assert. How do I compare to TypeError ?
Function
def calc_of_the_cost_of_delivery(distance: int, size: str, fragility: bool, workload: str) -> int:
....
if distance > 30:...
0
votes
0
answers
30
views
testing class init method with unittest [duplicate]
I'm learning how to use unittest in python. I have a class that receives a pandas dataframe in it's init method.
class GenericClass:
def __init__(self, data):
if not (isinstance(...
0
votes
0
answers
29
views
How to write unittest for myexception and raise? [duplicate]
how to writing unit tests for exception and raise? For example I have
someFoo.py
class MyExc(Exception):
"""
my exc
"""
pass
def foo(a,b,c):
raise MyExc
def ...
21
votes
6
answers
11k
views
Unittest causing sys.exit()
No matter what I do sys.exit() is called by unittest, even the most trivial examples. I can't tell if my install is messed up or what is going on.
IDLE 1.2.2 ==== No Subprocess ====
>>>...
17
votes
2
answers
17k
views
Test if ValidationError was raised
I want to test if an exception was raised how can I do that?
in my models.py I have this function, the one I want to test:
def validate_percent(value):
if not (value >= 0 and value <= ...