Enable Dark Mode!
how-to-write-test-case-in-the-odoo-17-erp.jpg
By: Cybrosys Technologies

How to Write Test Case in the Odoo 17 ERP

Technical

Writing test cases plays an important role in the software development process, as they verify that the software meets the specified requirements and functions correctly by identifying and fixing any issues, bugs, or inconsistencies.  This can help to reduce the cost of development and support, improve the quality of software products and applications, and increase customer satisfaction.

Odoo offers the capability to test modules using Python’s unit test library. Using this, the developers can write test cases for their custom modules to verify that their modules meet the desired requirements and behave as expected under various conditions.  With the following tips, you can write effective test cases that will help improve the quality of your Odoo modules.

How to write a test?

In Odoo, to write a test case, you have to create a subfolder ‘tests’ in your module and import it inside ‘ your_module/__init__ ’. Inside the test folder test modules should have the name starting with  ‘test_ ‘ and it should be imported in ‘ tests/__init__ ’.

How to Write Test Case in the Odoo 17 ERP-cybrosys

The __init__.py contains

From . import test_case1,test_case2
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’.
For example:
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")
These assertions provide a way to validate conditions during unit testing, helping to ensure that the code behaves as we expected.
Odoo offers several test classes and helper functions tailored for testing Odoo content. Here are some of these classes:
* odoo.tests.common.TransactionCase: This class executes all test methods within a single transaction. Each test method operates within a sub-transaction managed by a savepoint.
* odoo.tests.common.SingleTransactionCase: All test methods run within the same transaction in this class. The transaction begins with the first test method and rolls back at the end of the last.
* odoo.tests.common.SavepointCase: This class provides a savepoint for each test method.
* odoo.tests.common.HttpCase: Specifically designed for transactional HTTP test cases, this class offers helpers for URL opening and Chrome headless functionality.

How to run tests in Odoo?

To run tests in Odoo, follow these steps:
* Launch Odoo with the "--test-enable" parameter.
* Use the "-d [my_database]" flag to specify the database you want to test.
* Utilize the "-i [modules_to_install]" flag to indicate the modules you want to test.
* To execute a test on your local Odoo installation, use the following command as an example:
./odoo-bin -i {module_to_install} --test-enable -c {conf path}
You can use these commands in the terminal if you want to run the tests in a particular module. 
./odoo-bin -c /path/to/odoo.conf -d your_database_name -u your_module_name --test-enable
Following the steps outlined in this blog, you can streamline your testing process and catch potential issues early in the development cycle.
To read more about How to Write Test Case in the Odoo 16 ERP, refer to our blog How to Write Test Case in the Odoo 16 ERP


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment

 


whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message