Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django/Python assertRaises with message check

I am relatively new to Python and want to use a assertRaises test to check for a ValidationError, which works ok. However, I have many ValidationErrors and I want to make sure the right one is returned. I figured I could pass something into assertRaises but it doesn't look like I can, so I figured I would just do an assertTrue and check the exception message. However, I don't know how to access it. Is this even a good way to approach this issue? thanks.

class DailyEntriesTests(TestCase): def test_cant_have_ip_and_user(self):     u = createUser(False)     de = createDailyEntry(u, "1.1.1.1", 1)     with self.assertRaises(ValidationError) as cm:         de.full_clean()      # this line bombs - message doesn't exist. I also tried "error_code" like I saw in the documentation, but that doesn't work     print(cm.exception.message)      self.assertTrue(cm.exception.message.contains("Both")) 
like image 640
skaz Avatar asked Oct 30 '14 22:10

skaz


People also ask

How do you use assertRaises in Python?

There are two ways you can use assertRaises: using keyword arguments. Just pass the exception, the callable function and the parameters of the callable function as keyword arguments that will elicit the exception. Make a function call that should raise the exception with a context.

How do I skip a Django test?

Just trick it to always skip with the argument True : @skipIf(True, "I don't want to run this test yet") def test_something(): ... If you are looking to simply not run certain test files, the best way is probably to use fab or other tool and run particular tests.

What is test case in Django?

The best base class for most tests is django. test. TestCase. This test class creates a clean database before its tests are run, and runs every test function in its own transaction. The class also owns a test Client that you can use to simulate a user interacting with the code at the view level.

What is self client in Django?

self. client , is the built-in Django test client. This isn't a real browser, and doesn't even make real requests. It just constructs a Django HttpRequest object and passes it through the request/response process - middleware, URL resolver, view, template - and returns whatever Django produces.


2 Answers

Since the question is related to Django, you could also use the assertRaisesMessage context manager when inheriting from django's TestCase.

from django.test import TestCase  class ExceptionTest(TestCase):      def test_call_raises_exception_with_custom_message(self):         with self.assertRaisesMessage(Exception, 'My custom message!'):             call_that_causes_exception()      

Note: The assertRaisesMessage manager does an in lookup on the exceptions message: Say your exception raises "My custom message!", asserting for "custom message" passes. Bear this in mind especially if you have multiple (custom) exceptions with similar messages.

(E.g. two different exceptions raising "My custom message! Further details..." and "My custom message! No details." would both pass an assert for "My custom message!").

like image 26
Kim Avatar answered Sep 28 '22 05:09

Kim


You can just use assertRaisesRegexp.

with self.assertRaisesRegexp(ValidationError, "Both"):     de.full_clean() 

When you use it as a context manager the 2nd argument is a regular expression to search through the exception's string representation.

like image 81
Louis Avatar answered Sep 28 '22 05:09

Louis