|
| 1 | +:show-content: |
| 2 | +:show-toc: |
| 3 | + |
| 4 | +======================= |
| 5 | +JavaScript Unit Testing |
| 6 | +======================= |
| 7 | + |
| 8 | +Writing unit tests is as important as writing the code itself: it helps to |
| 9 | +ensure that the code is written according to a given specification and that it |
| 10 | +remains correct as it evolves. |
| 11 | + |
| 12 | +Testing Framework |
| 13 | +================= |
| 14 | + |
| 15 | +Testing the code starts with a testing framework. The framework provides a level |
| 16 | +of abstraction that makes it possible to write tests in an easy and efficient way. |
| 17 | +It also provides a set of tools to run the tests, make assertions and report the |
| 18 | +results. |
| 19 | + |
| 20 | +Odoo developers use a home-grown testing framework called :abbr:`HOOT (Hierarchically Organized |
| 21 | +Odoo Tests)`. The main reason for using a custom framework is that it allows us to extend it based |
| 22 | +on our needs (tags system, mocking of global objects, etc.). |
| 23 | + |
| 24 | +On top of that framework we have built a set of tools to help us write tests for the web client |
| 25 | +(`web_test_helpers`), and a mock server to simulate the server side (`mock_server`). |
| 26 | + |
| 27 | +You can find links to the reference of each of these parts below, as well as a section filled with |
| 28 | +examples and best practices for writing tests. |
| 29 | + |
| 30 | +Setup |
| 31 | +===== |
| 32 | + |
| 33 | +Before learning how to write tests, it is good to start with the basics. The following steps |
| 34 | +will ensure that your test files are properly picked up by the test runner. |
| 35 | + |
| 36 | +Note that in existing addons, most of these steps can be skipped since the proper |
| 37 | +folder structure and asset bundles are probably set up. |
| 38 | + |
| 39 | +#. Writing files in the right **place**: |
| 40 | + |
| 41 | + All JavaScript test files should be put under the `/static/tests` folder of the |
| 42 | + related addon (e.g. :file:`/web/static/tests/env.test.js`). |
| 43 | + |
| 44 | +#. Using the right **name**: |
| 45 | + |
| 46 | + Test files must end with :file:`.test.js`. This is not only a convention, but a requirement |
| 47 | + for test files to be picked up by the runner. All other JavaScript files will be |
| 48 | + interpreted either as production code (i.e. the code to be tested), or as test |
| 49 | + helper files (such as `web_test_helpers <{GITHUB_PATH}/addons/web/static/tests/web_test_helpers.js>`_). |
| 50 | + |
| 51 | + .. note:: |
| 52 | + It is to be noted that there is an exception for :file:`.hoot.js` files, which are not |
| 53 | + considered as test files, but as global modules for the whole test run, while other |
| 54 | + JavaScript modules are re-created for each test suite. Since the same instance of |
| 55 | + these modules will be running for the whole test run, they follow strict constraints, |
| 56 | + such as restricted imports, or advanced memory management techniques to |
| 57 | + ensure no side-effects are affecting tests. |
| 58 | + |
| 59 | +#. Calling the files in the right **bundle**: |
| 60 | + |
| 61 | + Test files, added in the right folder, must be included in the `web.assets_unit_tests` |
| 62 | + bundle. For ease of use, this can be done with glob syntax to import all test |
| 63 | + and test helper files: |
| 64 | + |
| 65 | + .. code:: python |
| 66 | +
|
| 67 | + # Unit test files |
| 68 | + 'web.assets_unit_tests': [ |
| 69 | + 'my_addon/static/tests/**/*', |
| 70 | + ], |
| 71 | +
|
| 72 | +#. Heading to the right **URL**: |
| 73 | + |
| 74 | + To run tests, you can then go to the `/web/tests` URL. |
| 75 | + |
| 76 | + .. tip:: |
| 77 | + This page can be accessed through :icon:`fa-bug` :menuselection:`Debug menu --> Run Unit Tests`. |
| 78 | + |
| 79 | +Writing tests |
| 80 | +============= |
| 81 | + |
| 82 | +After creating and including test files, it is time to write tests. You may refer |
| 83 | +to the following documentation sections to learn about the testing framework. |
| 84 | + |
| 85 | +.. toctree:: |
| 86 | + :titlesonly: |
| 87 | + |
| 88 | + unit_testing/hoot |
| 89 | + unit_testing/web_helpers |
| 90 | + unit_testing/mock_server |
0 commit comments