Testing exceptions

Sometimes, we need our tests to check whether an exception was generated. A common case is when testing whether some validations are being done properly.

In our example, the test_count() method uses a Warning exception as a way to give information to the user. To check whether an exception is raised, we place the corresponding code inside a with self.assertRaises() block.

We first need to import the Warning exception at the top of the file:

from odoo.exceptions import Warning

Then, add another method with a test case to the test class:

def test_count(self): 
    """Test count button""" 
    with self.assertRaises(Warning) as e: 
        self.wizard.do_count_tasks() 
    self.assertIn(' 2 ', str(e.exception)) 

If the do_count_tasks() method does ...

Get Odoo 11 Development Essentials - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.