0

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'):
                raise ValidationError(_('Error!!)) 
        return super(MyClass, self).unlink()

//test_file.py

 my_obj.button_validate()
 my_obj.unlink()

When adding : my_obj.unlink() in the function , I got the output in log like this:

  raise ValidationError(_('Error!!'))
odoo.exceptions.ValidationError: Error!!

How can I add test if the validation error is shown or not ?

Ing
  • 551
  • 7
  • 20

1 Answers1

0

This question might be a duplicate, consider the solution brought here by Moe:

Use TestCase.assertRaises (or TestCase.failUnlessRaises) from the unittest module, for example:

import mymod

class MyTestCase(unittest.TestCase):
    def test1(self):
        self.assertRaises(SomeCoolException, mymod.myfunc)
idik
  • 872
  • 2
  • 10
  • 19