Blender Pytest plugin.
Project description
pytest-blender
Pytest plugin for Blender testing. Executes your pytest testsuite with Blender in headless mode using its builtin Python interpreter.
Install
pip install pytest-blender
Documentation
Usage
Install dependencies in Blender Python interpreter
Before execute it, you need to install your testing dependencies inside the
builtin Blender Python interpreter. To get the interpreter location you can
use the CLI utility pytest-blender
, something like:
blender_python="$(pytest-blender)"
$blender_python -m ensurepip
$blender_python -m pip install -r test-requirements.txt
You can also get the intepreter for a custom Blender installation location
with --blender-executable
option:
pytest-blender --blender-executable ~/blender-2.91.2-linux64/blender
Execute tests
After installing dependencies, just call pytest as usually.
pytest -svv
Blender 2.82 (sub 7)
Read prefs: ~/.config/blender/2.82/config/userpref.blend
========================= test session starts ==================================
platform linux -- Python 3.8.5, pytest-6.1.2, py-1.9.0, pluggy-0.13.1 -- /usr/bin/blender
cachedir: .pytest_cache
rootdir: /home/mondeja/files/code/pytest-blender
collected 1 item
tests/test_bpy_import.py::test_inside_blender <module 'bpy' from '/usr/share/blender/scripts/modules/bpy/__init__.py'>
PASSED
=========================== 1 passed in 0.01s ==================================
Just can specify a custom blender executable path using --blender-executable
option:
pytest --blender-executable ~/blender-2.91.2-linux64/blender
Blender 2.91.2 (hash 5be9ef417703 built 2021-01-19 16:16:34)
Read prefs: ~/.config/blender/2.91/config/userpref.blend
found bundled python: ~/blender-2.91.2-linux64/2.91/python
============================ test session starts ===============================
platform linux -- Python 3.7.7, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: ~/pytest-blender
collected 1 item
tests/test_bpy_import.py . [100%]
============================== 1 passed in 0.00s ===============================
Reference
Fixtures
# blender_executable ⇒ str
Returns the path of the executable that has started the current Blender session.
# blender_version ⇒ str
Returns the version of Blender running in the current session.
# blender_python_executable ⇒ str
Returns the path of the Python executable builtin in the Blender release of the currently running session.
# blender_python_version ⇒ str
Returns the version of the Python executable builtin in the Blender release of the currently running session.
# install_addons_from_dir(addons_dir, addon_module_names, save_userpref=True, default_set=True, persistent=True, **kwargs)
Returns a function that installs and enables a set of addons whose modules are
located in a directory. This function is designed to be executed before the
pytest session to install the addons that you want to test, using the other
fixture disable_addons
to disable them after the execution of the suite:
import os
ADDON_MODULE_NAMES = ["my_awesome_addon_module_name"]
@pytest.fixture(scope="session", autouse=True)
def _register_addons(request, install_addons_from_dir, disable_addons):
install_addons_from_dir(os.path.abspath("src"), ADDON_MODULE_NAMES)
yield
disable_addons(ADDON_MODULE_NAMES)
- addons_dir (str) Directory in which are located the modules of the addons.
- addon_module_names (list) Name of the addons modules (without the
.py
extension). - save_userpref (bool) Save user preferences after installation.
- default_set (bool) Set the user-preference calling
addon_utils.enable
. - persistent (bool) Ensure the addon is enabled for the entire session (after loading new files).
- **kwargs (dict) Subsecuent keyword arguments are passed to
bpy.ops.preferences.addon_install
.
# disable_addons(addon_module_names, save_userpref=True, default_set=True, **kwargs)
Returns a function that disables a set of addons by module name. Is designed to
disables your addons after a pytest suite execution (check
install_addons_from_dir
for an example).
- addon_module_names (list) Name of the addons modules (without the
.py
extension). - save_userpref (bool) Save user preferences after installation.
- default_set (bool) Set the user-preference calling
addon_utils.disable
. - **kwargs (dict) Subsecuent keyword arguments are passed to
addon_utils.disable
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pytest_blender-0.0.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d09ff91515be7c88114a5461750f176d0e849d93756ffb242788fc049b55978b |
|
MD5 | 21a3afef468a8929a06e7a600884de7c |
|
BLAKE2b-256 | 22ee2b6f32b6830eb22de2f9933000b8424063af90f1616a19f1cc93a08de854 |