Note: Only tests imported in ‘ your_module/tests/__init__.py ’ will be run.
To begin, certain essential requirements need to be satisfied to utilize the Odoo Test Framework effectively.
1. All tests must extend to an Odoo action class.
2. All tests must be included in the ‘ tests/__init__ ’.
3. All the test files should be started as ‘ test_name.py’.
4. All test methods should start as def test_name(self):
5. All tests must be in the directory of your module.
The tests should be extended odoo.tests.common.TransactionCase. To write test cases, we can use setUpClass. Using the setUpClass you can use ‘env’ in the class and can interact with the ‘ORM’.
from odoo.tests.common import TransactionCase
class TestStudentRecord(TransactionCase):
@classmethod
def setUpClass(cls):
super(TestStudentRecord, cls).setUpClass()
# Create a common record for all test methods in the class
cls.student = cls.env['student.record'].create({
'name': 'John Doe',
'gender': 'male',
'birthdate': '2001-03-29'
})
def test_field_values(self):
# Test the values of fields
self.assertEqual(self.student.name, 'John Doe',
"Name field value is incorrect")
self.assertEqual(self.student.gender, 'male',
"Gender field value is incorrect")
def test_function_output(self):
# Test the output of a function
result = self.student.calculate_age()
self.assertEqual(result, expected_value, "Function output is incorrect")
def test_other_function(self):
# Another test method
# ...
@classmethod
def tearDownClass(cls):
# Clean up any resources if needed
super(TestStudentRecord, cls).tearDownClass()
Test cases can be written like this. Here, we can see the setup() method where we can create data and variables that we can use. These data and variables are typically stored as class attributes, so they can be used in other test methods. Additionally, we can observe some assert functions in the above example, which are used to examine whether the output is true or false.
Now, let's explore some of the most common assert functions and their respective purposes.
* assertEqual(a, b, msg=None): This function is used to check whether the values a and b are equal. If they are not equal, the assertion fails, and an error message may be provided through the optional msg parameter.
For example:
self.assertEqual(actual_value, expected_value, msg="Values should be equal")
* assertNotEqual(a, b, msg=None): This function is used to check whether the values a and b are not equal. If they are equal, the assertion fails, and an error message may be provided through the optional msg parameter.
For example:
self.assertNotEqual(actual_value, unexpected_value, msg="Values should not be equal")
* assertTrue(expr, msg=None): This function is used to verify that the given expression expr evaluates to True. If the expression is False, the assertion fails, and an error message may be provided through the optional msg parameter.
For example:
self.assertTrue(is_valid, msg="The condition should be true")
* assertFalse(expr, msg=None): This function is used to verify that the given expression expr evaluates to False. If the expression is True, the assertion fails, and an optional error message (msg) can be provided to give additional information about the failure.
For example:
self.assertFalse(has_error, msg="The condition should be false")
* assertIs(a, b, msg=None): This function is used to verify that the objects a and b refer to the same object in memory. Essentially, it performs an identity check.
For example:
self.assertIs(actual_object, expected_object, msg="Objects should be the same")
* assertIsNot(a, b, msg=None): This function is used to verify that the objects a and b do not refer to the same object in memory. In other words, it performs the opposite of the assertIs assertion.
For example:
self.assertIsNot(actual_object, unexpected_object, msg="Objects should not be the same")