How it works...

In Odoo, Python test cases are added in the tests/ directory of the module. Odoo will automatically identify this directory and run the test under the folder.

Odoo uses Python unittest https://docs.python.org/3.5/library/unittest.html for Python test cases. It also provides some helper classes, wrapped over unittest. These classes simplify the process of developing test cases. In our case, we have used TransactionCase. The TransactionCase runs each test case method in a different transaction. Once a test case method runs successfully, a transaction is automatically rolled back.

The class method starts from test_ and is considered a test case. In our example, we have added two test cases. This checks the methods that change the book's state. The self.assertEqual method is used to check whether the test case runs successfully. We have checked the book state after performing operations on the book's record. So, if the developer made a mistake and the method is not changing states as expected, the test case will be failed.

Note that the setUp() method will automatically call for every test case we run, so in this recipe, we have added two test cases, so that setUp() will call twice. As per the code in this recipe, there will only be one record of the book present during testing, because with TransactionCase, the transaction is rolled back with every test case.

The docstrings on the methods will be printed in the logger. This will be very helpful for checking the status of the particular test case.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset