Reader small image

You're reading from  Odoo 12 Development Cookbook - Third Edition

Product typeBook
Published inApr 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789618921
Edition3rd Edition
Languages
Tools
Right arrow
Authors (4):
Parth Gajjar
Parth Gajjar
author image
Parth Gajjar

Parth Gajjar is an Odoo expert with a deep understanding of the Odoo framework. He started his career at Odoo and spent 7 years in the R&D department at Odoo India. During his time at Odoo, he worked on several key features, including a marketing automation app, mobile application, report engine, domain builder, and more. He also worked as a code reviewer and helped manage the code quality of the new features. Later, he started his own venture named Droggol and now provides various development services related to Odoo. He loves working on Odoo and solving real-world business problems with different technologies. He often gives technical training to Odoo developers.
Read more about Parth Gajjar

Alexandre Fayolle
Alexandre Fayolle
author image
Alexandre Fayolle

Alexandre Fayolle started working with Linux and free software in the mid-1990s and quickly became interested in the Python programming language. In 2012, he joined Camptocamp to share his expertise on Python, PostgreSQL, and Linux with the team implementing Odoo. He currently manages projects for Camptocamp and is strongly involved in the Odoo Community Association. In his spare time, he likes to play jazz on the vibraphone.
Read more about Alexandre Fayolle

Holger Brunn
Holger Brunn
author image
Holger Brunn

Holger Brunn has been a fervent open source advocate since he came into contact with the open source market sometime in the nineties. He has programmed for ERP and similar systems in different positions since 2001. For the last 10 years, he has dedicated his time to TinyERP, which became OpenERP and evolved into Odoo. Currently, he works at Therp BV in the Netherlands as a developer and is an active member of the Odoo Community Association.
Read more about Holger Brunn

Daniel Reis
Daniel Reis
author image
Daniel Reis

Daniel Reis has a degree in applied mathematics and an MBA. He has had a long career in the IT industry, mostly as a consultant implementing business applications in a variety of sectors. He has been working with Odoo (OpenERP at the time) since 2010 and is an active contributor to the Odoo Community Association (OCA), where he also serves as a board member. He is the managing director of Open Source Integrators, a leading open source and Odoo consultancy firm.
Read more about Daniel Reis

View More author details
Right arrow

Automated Test Cases

In this chapter, we will cover the following recipes:

  • Python test cases
  • Running tagged Python test cases
  • Setting up Headless Chrome for client-side test cases
  • Client-side QUnit test cases
  • Adding tour test cases
  • Running client-side test cases from the UI
  • Debugging client-side test cases
  • Generating videos/screenshots for failed test cases

Introduction

When it comes to developing large applications, automated test cases are a good practice for improving the reliability of your module. This makes your module more robust. Every year, Odoo releases a new version of its software, and automated test cases are very helpful in detecting regression in your application, which may have been generated due to a version upgrade. Luckily, an Odoo framework comes with different automated testing utilities. Odoo includes the following three main types of tests:

  • Python test case: Used to test Python business logic
  • JavaScript QUnit test: Used to test JavaScript implementation in Odoo
  • Tours: Integration test to check that Python and JavaScript work with each other properly

In this chapter, we will look at all of these test cases in detail. In order to cover all of test cases in the same module, we have created a small module....

Technical requirements

Python test cases

Python test cases are used to check the correctness of the business logic. In Chapter 6, Basic Server-Side Development, you saw how you can modify the business logic of the existing app. This makes it even more important, as customization might break the app's functionality. In this chapter, we will write a test case to validate the business logic to change the book's state.

Getting ready

We will be using the my_library module from the Chapter18/r0_initial_module directory of the GitHub repository.

How to do it...

Follow these steps to add Python...

Running tagged Python test cases

When you run the Odoo server with the --test-enabled module, the test cases run immediately after the module is installed. If you want to run a test case after the installation of all the modules, or if you just want to run a test case for only one module, a tagged() decorator is the answer. In this recipe, we will illustrate how to use this decorator to mold test cases.

Getting ready

For this recipe, we will be using the my_library module from the last recipe. We will modify the sequence of the test case.

How to do it...

Add a tagged...

Setting up Headless Chrome for client-side test cases

Odoo v12 uses Headless Chrome for performing JavaScript test cases and tour test cases. In this recipe, we will install Headless Chrome and other packages, in order to run JavaScript test cases.

How to do it...

You will need to install a Chrome browser to enable a JavaScript test case. For the development of the modules, we mostly use the desktop OS. Consequently, if you have a Chrome browser installed in your system, then there is no need to install it separately. You can run client-side test cases with desktop Chrome itself. Make sure that you have a Chrome version higher than Chrome 59. Odoo also supports the Chromium browser.

Note that with Headless Chrome, client-side...

Client-side QUnit test cases

Building new fields or views is very simple in Odoo. In just a few lines of XML, you can define a new view. However, under the hood, it uses JavaScript in large amounts. Modifying/adding new features on the client side is complex, and it might break a few things. Most of the client-side issues go unnoticed, as most errors are only displayed in the console. So, QUnit test cases are used in Odoo to check the correctness of different JavaScript components.

Getting ready

For this recipe, we will continue using the my_library module from the previous recipe. We will add a QUnit test case for the int_color widget.

...

Adding tour test cases

You have now seen Python and JavaScript test cases. Both of these work in an isolated environment, and they don't interact with each other. To test integration between JavaScript and Python code, tour test cases are used.

Getting ready

For this recipe, we will continue using the my_library module from the previous recipe. We will add a tour test case to check the flow of the book model.

How to do it...

Follow these steps to add a tour test case for books:

  1. Add a /static/src/js/my_library_tour.js file, and add a tour like this:
odoo.define(...

Running client-side test cases from the UI

Odoo provides a way to run client-side test cases from the UI. This helps you to see the status of each test case from the user interface.

How to do it...

You can run both the QUnit test case and the tours test case from the UI. It is not possible to run Python test cases from the UI. In order to see the options to run test cases from the UI, you need to enable developer mode.

Running QUnit test cases

Click on the bug icon to open the drop-down menu, as shown in the following image. Click on the Run JS Tests option:

This will...

Debugging client-side test cases

Developing complex client-side test cases can be a headache. In this recipe, you will learn how you can debug the client-side test cases in Odoo. Instead of running all of the test cases, we will just run the single one. Additionally, we will display the UI of the test case.

Getting ready

For this recipe, we will continue using the my_library module from the previous recipe.

How to do it...

Follow these steps to run the test case in debug mode:

  1. Open the file /static/tests/colorpicker_tests.js, and update the test QUnit.test with QUnit...

Generating videos/screenshots for failed test cases

Odoo version 11 uses PhantomJS to run the test cases on the server side, but Odoo 12 uses Headless Chrome. This opens new possibilities. With Odoo 12, you can record videos of the failed test cases, or you can take screenshots of the failed test cases.

How to do it...

Recording a video for the test case requires an ffmpeg package. To install this, you need to execute the following command in the Terminal (note that this command only works on a Debian-based OS):

apt-get install ffmpeg

Generating a video or screenshot requires the directory location of the log file, so if you are not saving the log in a separate log file, then do it through the --logfile option in the command...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Odoo 12 Development Cookbook - Third Edition
Published in: Apr 2019Publisher: PacktISBN-13: 9781789618921
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (4)

author image
Parth Gajjar

Parth Gajjar is an Odoo expert with a deep understanding of the Odoo framework. He started his career at Odoo and spent 7 years in the R&D department at Odoo India. During his time at Odoo, he worked on several key features, including a marketing automation app, mobile application, report engine, domain builder, and more. He also worked as a code reviewer and helped manage the code quality of the new features. Later, he started his own venture named Droggol and now provides various development services related to Odoo. He loves working on Odoo and solving real-world business problems with different technologies. He often gives technical training to Odoo developers.
Read more about Parth Gajjar

author image
Alexandre Fayolle

Alexandre Fayolle started working with Linux and free software in the mid-1990s and quickly became interested in the Python programming language. In 2012, he joined Camptocamp to share his expertise on Python, PostgreSQL, and Linux with the team implementing Odoo. He currently manages projects for Camptocamp and is strongly involved in the Odoo Community Association. In his spare time, he likes to play jazz on the vibraphone.
Read more about Alexandre Fayolle

author image
Holger Brunn

Holger Brunn has been a fervent open source advocate since he came into contact with the open source market sometime in the nineties. He has programmed for ERP and similar systems in different positions since 2001. For the last 10 years, he has dedicated his time to TinyERP, which became OpenERP and evolved into Odoo. Currently, he works at Therp BV in the Netherlands as a developer and is an active member of the Odoo Community Association.
Read more about Holger Brunn

author image
Daniel Reis

Daniel Reis has a degree in applied mathematics and an MBA. He has had a long career in the IT industry, mostly as a consultant implementing business applications in a variety of sectors. He has been working with Odoo (OpenERP at the time) since 2010 and is an active contributor to the Odoo Community Association (OCA), where he also serves as a board member. He is the managing director of Open Source Integrators, a leading open source and Odoo consultancy firm.
Read more about Daniel Reis