self.assertTrue(False) what exactly does it test
Solution 1
Function signature from python docs
assertTrue(expr, msg=None)
Check if given expr
is True
. In case that expr
is False
, it will raise an error using the provided message (msg
)
self.assertTrue(True, "Not authorized") # Is always True
self.assertTrue(False, "Should not create reservation") # Is always False and it will throw an error saying "Should not create reservation"
So within your try block, if any exception is not raised, self.assertTrue(False, "Should not create reservation")
will throw an error because False
is not True
If an exception is raised, then your code will enter into Except block
and
self.assertTrue(True, "Not authorized")
will not raise any error, because True is True.
So, if you want to throw an exception, if any exception occurs, move self.assertTrue(False, "Should not create reservation")
to Except block
except Exception, e:
self.assertTrue(False, "Should not create reservation")
Solution 2
Though not a direct answer to the title, this should answer your actual problem.
-
You should never catch
Exception
(unless you re-reraise it after).except Exception, e:
When you handle exceptions, you must specify the exact error you want to catch. For instance, the code that calls
create_reservation
might catch aReservationError
exception, that you defined.If you want this code to work, you must then test and make sure that
create_reservation
raises the correct error. So even in the test, you still must use the correct error. -
If you need to test some code raises an exception, use the appropriate assertion, which is assertRaises. It can be used in two ways. Either as a call:
self.assertRaises(ReservationError, create_reservation, self.regularUser2, .........)
or you may use it as a context manager, using the
with
keyword (requires python 2.7 or python 3.1 or newer):with self.assertRaises(ReservationError): create_reservation(self.regularUser2, ...........)
Both example will run
create_reservation
, check that it raises an exception, pass if it does and it is of the correct type, fail otherwise.
james
Updated on June 09, 2022Comments
-
james almost 2 years
I am writting some tests for our application and am not sure I am testing the correct thing here. This is my test.
def test_ReservationExtensionFalseWrongResource(self): 'does not create a reservation that is an extension if different resource' try: reservation1 = Reservation.objects.create(user=self.regularUser1, resource=self.resource1, modality=self.modality, timeFrom=datetime(2015, 6, 11, 20, tzinfo=pytz.utc), timeTo=datetime(2015, 6, 11, 21, tzinfo=pytz.utc), count=1, notes=None, extendedReservation=None) reservation = create_reservation(self.regularUser2, self.regularUser2, None, self.resource2, self.modality, datetime(2015, 6, 11, 20, tzinfo=pytz.utc), datetime(2015, 6, 11, 21, tzinfo=pytz.utc), 1, reservation1.uuid) self.assertTrue(False, "Should not create reservation") except Exception, e: self.assertTrue(True, "Not authorized")
I want to be sure that a reservation extension cannot be created if it is of a different resource, so this line should fail in the try block:
reservation = create_reservation(self.regularUser2, self.regularUser2, None, self.resource2, self.modality, datetime(2015, 6, 11, 20, tzinfo=pytz.utc), datetime(2015, 6, 11, 21, tzinfo=pytz.utc), 1, reservation1.uuid)
Does this:
self.assertTrue(False, "Should not create reservation")
Assert that the reservation creation resulted in a False value? Or am I understanding the assert matchers incorrectly. I have tried going over the documentation but I could not see any analogous example of something like this in a try catch block that was obvious to me.
Help is appreciated.