Odoo.sh homepage

Configuring and using Odoo.sh

In this tutorial I will learn you how to setup an Odoo.sh account, how to configure Odoo.sh and how to tests your code automatically with Odoo.sh.
In this example I will create a new account, create a new repository and add code to the Github repository in order to explain how Odoo.sh works.

1. Creating an Odoo.sh account

Go to odoo.sh and click on the “Sign in” button at the top:
Odoosh homescreen
When you click on the “Sign in button” you’ll get an authorize screen from Github. If you’re not yet logged in on Github it will ask you to login, if you’re already logged in on Github you’ll get the authorize screen. Click on “Authorize odoo”:
Authorize Odoo
After you click on “Authorize odoo” Github will ask for additional permissions. Click on “Authorize Odoo” again to give the additional permissions. Because of these additional permissions Odoo.sh can follow all changes and handle it automatically for you.
Additional permissions Odoosh
Now Odoo will ask you to deploy your platform. Choose an existing repository if you want to use it or create a new one. In this tutorial I will create a new repository to show everything in detail. Choose a repository name. Next choose the Odoo version you want to test against and finally provide your enterprise (or partner) license. The hosting location is up to you. Finally click on “Deploy”:
New repository settings
Thats it! You’ve just registered your own Odoosh account and connected Github to Odoosh!

2. The Odoo.sh main screen

After you’ve clicked on “Deploy” you’ll now see the main screen of Odoo.sh.
Odoosh main screen
In the left menu you will see the title “DEVELOPMENT”. Under this section you will see every branch you’ve made on this Github repository. If I would create a second branch named “11.0” I would see “11.0” and “master” in the left menu. At the top you’ll see a main menubar with the options “Branches”, “Builds”, “Status” and “Settings.

  • The Branches tab opens the main page, from where you can see everything related to your branches. This includes the mails, shell access to the test instance and access to the logs.
  • The Builds tab opens a page where you can see all your test instances. This is almost identical to the Odoo runbot (at runbot.odoo.com).
  • The Status tab opens a page where you can see all the statistics of the Odoo.sh platform. It shows you the uptime and the status of all the servers.
  • The Settings tab opens a page where you can configure advanced settings. You can add collaborators, set a project name (and URL), add submodules and much more.

3. Github

Now open up your Github and go to your (just created) repository. Add a commit to the repository so that it contains some (new) code. In my example I will push a module to Github that contains an automated test so I can show you how tests work and what happens. If you’d like to do the same you can take my example module from Github. The moment that you make a commit to the Github repository Odoo.sh will detect this and it will start up a test environment because of the new commit. My Github after making a commit:
New commit on Github
My Odoo.sh a few seconds after making this commit:
Odoosh new commit

4. Checking the commit

Now switch back to Odoo.sh. After a few minutes your new commit will procude a new test instance that is ready and built. In my example I’ve deliberately added an error in my last commit so as a result you can see the test fail. When the test instance is done, typically after a few minutes, you’ll see the result in the main screen:
Failed Odoo.sh build

So now my test has failed it means I must have done something wrong. I’ve figured out what was wrong and I correct my test. After correcting this test I make a new commit to Github. After a few seconds you’ll see that Odoo.sh automatically detects and tests this new commit again:
Succeeded Odoo.sh build

5. Odoo.sh builds and test instances

Finally go to the builds page by clicking on “Builds” in the top menu bar. After you click on “Builds” you will see an overview of all your test instances and if they succeeded or not:
Odoosh build page
From this page you can directly connect to a test instance in order to test functionalities or ‘play’ with Odoo.
When a test has failed (such as “INIT: Add demo module wit…” in my screenshot) you can click on the exclamation icon, next to the connect button, to see why your instance has failed:
Failed test output
Tip: If you want to see the full log of the instance you can do this by clicking on the “…” icon at the top of a build and selecting “Logs”. From here you can download and view all logs. Alternatively you can also do this from the “Branches” page by clicking on the “Logs” menu.

6. Conclusion

As Odoo.sh is very big and has a lot of options I’ve decided to split the content in two tutorials. Because of this the first blog post, this one, is only about basic operations. You can find a second tutorial about how to configure public and private submodules here.

Odoo.sh is a very powerfull platform to use. Odoo has invested a lot of time into Odoo.sh and due to this it is an advanced system that allows you to quickly (and easily!) use test instances with Odoo. Thanks to Odoo.sh you no longer need your own runbot instance and complex hardware setup as you can get it all out of the box.

Has this tutorial helped you, do you have any feedback or questions? Post away!


PayPal

Odoo Experts

Creating automated tests in Odoo

In this tutorial I will learn you how to write tests, how to use the tests in Odoo and how to test them. I will create an example test that creates a new project, a new task and attaches the project to the new task.
Tip: This tutorial is based on Odoo V11 but will also work in V10.

1. Creating a new module

Let us create a new module to start. You can either manually create a module or create one with the scaffold command from Odoo. Now that you’ve created your module you should add the dependency to the module where you want to create a test for. In this tutorial I will create a test for the project module so I’ll add a dependency for ‘project’. Add your depends in the manifest.py (or __openerp__.py in V10):

2. Creating the Python files

After you’ve created your new module you should create a new folder named ‘tests’. Create a new Python file for the test, in this example I’ll create a new file named ‘test_project.py’.
Tip: The filename of a Python test should always start with ‘test_’ or it will not be executed! Your folder structure should now look like this:
Tests folder structure

Next create a new Python file named __init__.py and import the Python file ‘test_project’ in it:

Your folder structure should now look like this:
Complete tests folder structure
Tip: You should not import the tests folder in the main __init__.py file. Odoo tests are an exception and have a built-in check by Odoo. They do not need an explicit import like models or controllers.

3. Creating the class and adding imports

Now open up your ‘test_project.py’ file again and let us start by adding the import and creating the class structure!
In order to write a test you should import ‘odoo.tests’ and you should create a new class with a TransactionCase. Have a look at this code:

So, does this make sense to you? We simply import the default Odoo framework options for tests with ‘from odoo.tests import common’. We then create a new class in which we add ‘common.TransactionCase’. The ‘common.TransactionCase’ tells Odoo that we want to test a transaction and that we want to do a rollback of this record after the test is done.

4. Writing the tests

Now that we have the basic structure setup there is just one more thing to do: write a function in which you want to do your tests. Go ahead and create a new function in your file.
Tip: Function names for tests should always start with ‘test_’ or they will not be run by Odoo!

Finally, write the test cases you’d like in this function. In my example I will create a test that creates a new project, then creates a new task and then adds the project to the task. Have a look at this piece of code:

So, does this make sense to you? A lot of the code looks the same as if you’re writing custom code. With self.env[‘project.project’].create({}) we’ll create a new project named ‘TestProject’. With self.env[‘project.task’].create({}) we’ll create a new task named ‘ExampleTask’ and we will assign the project to this task, thanks to the ‘test_project’ variable.
If you would run this code it would create a new project and task and the moment the test is finished the transaction is rolled back (so there is no record in the database).

4.1 Understanding assertEqual

The final part of my code shows some assertEqual calls, so what do these calls do? assertEqual is made available by default in Odoo to test and compare values. For example:

This will test if the just created project record it’s name is indeed named ‘TestProject’. In the second assertEqual I check if the task name is in fact ‘ExampleTask’. And, finally, in the third assertEqual I check if the project attached to the task is indeed the right project:

The first variable of assertEqual is always the value you’ve gotten back from the test and the second part is the result that you’re expecting. If there is a difference in those two the test will fail.

5. Running / testing the test

So now we’ve coded the whole test but how do we run and test it? In order to run a test you should use the ‘–test-enable’ parameter and the ‘-i’ parameter to say which module(s) you want to test. If you want to test this in your local Odoo instance you will need to run your Odoo from the terminal like this:

If you have a runbot instance (or use Odoo.sh) the tests will be executed automatically and you will see if the test succeeded or failed right away. An example of Odoo.sh:
Odoo.sh status example
An example of a runbot instance:
Runbot status example

When you just run the tests locally you can see if the test succeeded or failed in the logfile. Open up your logfile after you’ve ran the test and have a look at the output:

If you add a print statement in the test, like in my example, you will also see it printed in the terminal:
Succesfull test

5.1 Making a test fail

We’ve now seen how to create a test and how to run it, but how exactly does a failed test look?
Open up your test again and change the assertEqual from this:

to this:

Run the test from your terminal again with -i your_module –test-enable in the command. You’ll see that the print statement is no longer printed in your terminal. Now open up your logfile again after your Odoo is loaded. You’ll now find a failure in the logfile:

This shows you that the input ‘TestProject’ does not match what we expect (‘TestProject11’) and because of this our test failed.
If you would commit these changes to Github and if you’re using a runbot or Odoo.sh you’ll also see a failed instance after this.

6. Conclusion

Writing and using tests in Odoo is quite easy to do. It is a great feature to use if you’re writing a lot of custom code and want to keep overview. Thanks to Odoo.sh and runbots it is also very easy to follow up on your tests and to see if something has broken or not. Sadly at this point running tests locally isn’t very easy to follow up though so you might want to consider setting up a runbot or Odoo.sh account.
Do you want to try the demo code and see the source code of this tutorial? You can view it on my Github account.
Has this tutorial helped you, do you have any feedback or questions? Post away!


PayPal

Odoo Experts