We used cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it. What For?

Importance of Unit Test Cases in Odoo

Unit Test Cases

It’s not about how your code works but it is more about how well your code works. It is very important to ensure the quality of your code in any development. Unit tests are a way to write test cases for all functions and methods so that whenever a change causes a fault then in that case bugs are quickly identified and fixed.

To make your odoo database migration resilient and trouble-free one must migrate the unit test cases from the older version to the newer version and if it doesn't exist in the older version then one must write it to make your ERP robust.

It is vital to consider the unit test cases while upgrading your customizations and once it is done you can proceed with odoo database upgrade.

Here we are going to explore Unit Testing in odoo V15, before we dive into the technical stuff let's go through the advantages of implementing unit testing in software development.

  • The process becomes Agile
  • Quality of Code
  • Find Software Bugs Easily
  • Ease in debugging
  • Minimal production breaks
Types of unit testing

In Odoo, we mainly have three types of tests:

  1. Python unit tests: useful for testing model business logic
  2. JS unit tests: useful to test the javascript code in isolation
  3. Tours: tours simulate a real situation. They ensure that the python and the javascript parts properly communicate with each other.

Here we are going to explore the Python unit testing in odoo which requires python’s unit test library. Below is the structure of the unit test code in the custom module.

  • We need to create a directory named “tests” under our custom module and then inside it, we have to create the __init__.py file and other files containing the unit test logic, also don’t forget to import the test files in the __init__.py file. The test files should have a filename starting with “test_”
  • To perform the testing of our custom business logic there are some prerequisites such as demo data and objects to run our test cases. For this, we can use the common method common() for the setup which is done in the class method setUpClass().
  • After execution of the test cases each method cleans up the record cache and registry cache but it won't clean up the registry models and fields. We can prepare the cleanup for that with self.registry.resest_changes()
  • The most common situation is to use TransactionCase and test a property of a model in the model.

Let’s understand it better with the help of an example below:

Below is the list of few methods to check the values in the test case methods:

  • assertEqual
  • assertNotEqual
  • assertTrue
  • assertFalse
  • assertIs
  • assertIsNot
  • assertIsNone
  • assertIsNotNone
  • assertIn
  • assertNotIn
  • assertItemsEqual
  • assertDictContainsSubset
  • assertMultiLineEqual
  • assertSequenceEqualsequences
  • assertSetEqual
  • assertIsInstance
  • assertNotIsInstance
  • assertRaises
  • assertRaisesRegexp
  • assertAlmostEqual
  • assertNotAlmostEqual
  • assertGreater
  • assertGreaterEqual
  • assertLess
  • assertLessEqual
  • assertRegexpMatches
  • assertNotRegexpMatches
  • assertListEqual
  • assertTupleEqual
  • assertDictEqual
Invocation:
  • We have two ways to invoke the test cases
    • --test-enable
    • --test-tags
  • --test-enable will run all the test cases which are by default having the standard+ tag with them i.e. those which are not associated with any tag belong to standard+ tag.
  • While with --test-tag we can run the test cases with the special decorated tags “tagged()” instead of all (like standard+), which takes the tag as the argument in it.
    • For eg: Class decorated with the decorator: tagged(‘basket’) Then its test cases can be called with --test-tag basket

A reference to automated unit test cases for javascript can be found.

Conclusion:

We are a leading odoo consulting company with a team of proficient developers capable of writing the unit test cases with ease. Get in touch with us for more information.

contact-us Request a callback WhatsApp