| 1 |
Running tests |
| 2 |
============= |
| 3 |
|
| 4 |
Bonsai has three layers of tests for each of its three technology layers: |
| 5 |
|
| 6 |
1. **Core tests**: abstract domain logic unit tests agnostic of Blender |
| 7 |
2. **Tool tests**: low-level concrete unit tests dependent on Blender |
| 8 |
3. **UI tests**: high-level integration UI and smoke tests dependent on Blender |
| 9 |
|
| 10 |
These tests use ``pytest`` as the test framework and runner, so install it: |
| 11 |
|
| 12 |
.. code-block:: bash |
| 13 |
|
| 14 |
pip install pytest |
| 15 |
|
| 16 |
All development is expected to use test driven development, and so we expect |
| 17 |
test coverage to be 100% where it is technically possible to test. |
| 18 |
|
| 19 |
When running tests, Makefile targets are provided for convenience so you can |
| 20 |
type in a simple command without knowing the internals. This means you can run |
| 21 |
tests by using the ``make`` command. |
| 22 |
|
| 23 |
Because Bonsai depends on IfcOpenShell, it is advised to also run tests for |
| 24 |
IfcOpenShell and its Python bindings, which is not covered in this document. |
| 25 |
|
| 26 |
Core tests |
| 27 |
---------- |
| 28 |
|
| 29 |
The core layer tests are pure Python unit tests with no dependencies on Blender |
| 30 |
or other modules. They are designed to be fast and easy to run as they test |
| 31 |
purely abstract domain logic. |
| 32 |
|
| 33 |
Although they are vanilla Python tests, they do not use the Python Mock module. |
| 34 |
Instead, a lightweight ``Prophecy`` mocker class is used, which allows tests to |
| 35 |
be written in a highly concise, expressive manner. For those coming from a |
| 36 |
BDD background in Ruby's RSpec, PHP's PHPSpec, and PHP's Prophecy, this is very |
| 37 |
similar. |
| 38 |
|
| 39 |
.. code-block:: bash |
| 40 |
|
| 41 |
cd src/bonsai/ |
| 42 |
make test-core |
| 43 |
# If you're on Windows, and don't want to use make, use: |
| 44 |
pytest -p no:pytest-blender test/core |
| 45 |
|
| 46 |
Tool tests |
| 47 |
---------- |
| 48 |
|
| 49 |
The tool layer tests actual concrete functions. You will need to install the |
| 50 |
following dependencies: |
| 51 |
|
| 52 |
* pytest-blender, accessible to your system's Python |
| 53 |
* Blender executable, accessible to pytest-blender on your system's Python |
| 54 |
(e.g. through the ``blender`` command in your path) |
| 55 |
|
| 56 |
.. code-block:: bash |
| 57 |
|
| 58 |
pip install pytest-blender |
| 59 |
# Check that "Blender" is in your system's path |
| 60 |
blender |
| 61 |
|
| 62 |
On Windows, you can add Blender to the system path by doing: |
| 63 |
|
| 64 |
1. Open the start menu and launch **Control Panel** > **System** > **Edit the |
| 65 |
system environment variables** |
| 66 |
2. In the **System Properties** window, under the **Advanced** tab press |
| 67 |
**Environment Variables**. This will open a dialog showing a list of all your |
| 68 |
variables. |
| 69 |
3. In the **System Variables** section select the entry named **Path**, and |
| 70 |
press **Edit...**. This will open a new dialog showing all the directories |
| 71 |
stored in the **Path** variable. |
| 72 |
4. Press **New** and browse to the directory where your **blender.exe** is |
| 73 |
located, such as in ``C:\Program Files\Blender Foundation\Blender 3.2``. |
| 74 |
|
| 75 |
In addition, you will need to install these dependencies for Blender: |
| 76 |
|
| 77 |
* pytest, accessible to your Blender Python |
| 78 |
* pytest-bdd, accessible to your Blender Python |
| 79 |
|
| 80 |
You can install the dependencies by running the ``setup_pytest.py`` script in |
| 81 |
Blender: |
| 82 |
|
| 83 |
1. Launch Blender |
| 84 |
2. Load ``src/bonsai/scripts/setup_pytest.py`` in the Blender text editor |
| 85 |
3. Run the script by pressing ``Text > Run Script``. |
| 86 |
4. Check the Blender console for any errors or success messages. |
| 87 |
|
| 88 |
.. warning:: |
| 89 |
|
| 90 |
The ``scripts/setup_pytest.py`` may not work for all operating systems and |
| 91 |
installation environments. In this case, you may be required to install the |
| 92 |
dependencies manually. |
| 93 |
|
| 94 |
Please be aware that some Blender may come packaged with its own Python, |
| 95 |
which may be separate to the Python installation on your system. Be sure to |
| 96 |
install the dependencies to the correct Python environment. |
| 97 |
|
| 98 |
Then, run the tests. This will launch Blender headlessly and check the behaviour |
| 99 |
of all concrete functions. |
| 100 |
|
| 101 |
.. code-block:: bash |
| 102 |
|
| 103 |
cd src/bonsai/ |
| 104 |
make test-tool # Test everything |
| 105 |
make test-tool MODULE=foo # Only test a single module |
| 106 |
# If you're on Windows, and don't want to use make, use: |
| 107 |
pytest test/tool # Test everything |
| 108 |
pytest test/tool/test_foo.py # Only test a single module |
| 109 |
|
| 110 |
UI tests |
| 111 |
-------- |
| 112 |
|
| 113 |
The UI layer acts as a full integration test. |
| 114 |
|
| 115 |
Before running these tests, follow the instructions for running tool tests |
| 116 |
above. |
| 117 |
|
| 118 |
You will also need to enable the **Sun Position** add-on, as it is required to |
| 119 |
test georeferencing features: ``Edit > Preferences > Add-ons`` and install |
| 120 |
**Lighting: Sun Position**. |
| 121 |
|
| 122 |
.. code-block:: bash |
| 123 |
|
| 124 |
cd src/bonsai/ |
| 125 |
make test-bim # Test everything |
| 126 |
make test-bim MODULE=foo # Only test a single module |
| 127 |
# If you're on Windows, and don't want to use make, use: |
| 128 |
pytest test/bim # Test everything |
| 129 |
pytest test/bim -m "foo" ./ --maxfail=1 # Only test a single module |
| 130 |
|
| 131 |
Code styling |
| 132 |
------------ |
| 133 |
|
| 134 |
`Black <https://black.readthedocs.io/en/stable/index.html>`__ is used for code |
| 135 |
formatting. The settings for black are configured in the ``pyproject.toml`` at |
| 136 |
the project root. At the project root, just run: |
| 137 |
|
| 138 |
.. code-block:: bash |
| 139 |
|
| 140 |
black . |