pytest plugin¶
PyJulia automatically installs a pytest plugin. It takes care of tricky aspects of PyJulia initialization:
- It loads
libjuliaas early as possible to avoid incompatibility of shared libraries such aslibstdc++(assuming that the ones bundled withjuliaare newer than the ones otherwise loaded). - It provides a way to succinctly mark certain tests require Julia runtime (see Fixture and Marker).
- The tests requiring Julia can be skipped with
--no-julia. - It enables debug-level logging. This is highly recommended especially in CI setting as miss-configuration of PyJulia may result in segmentation fault in which Python cannot provide useful traceback.
To activate PyJulia’s pytest plugin [1] add -p julia.pytestplugin
to the command line option. There are several ways to do this by
default in your project. One option is to include this using
addopts setup of pytest.ini or tox.ini file. See How to
change command line options defaults:
[pytest]
addopts =
-p julia.pytestplugin
| [1] | This plugin is not activated by default (as in normal
pytest-* plugin packages) to avoid accidentally breaking user’s
pytest setup when PyJulia is included as a non-test dependency. |
Options¶
Following options can be passed to pytest
-
--no-julia¶ Skip tests that require julia.
-
--julia¶ Undo
--no-julia; i.e., run tests that require julia.
-
--julia-runtime¶ Julia executable to be used. Defaults to environment variable
PYJULIA_TEST_RUNTIME.
-
--julia-<julia_option>¶ Some
<julia_option>that can be passed tojuliaexecutable (e.g.,--compiled-modules=no) can be passed topytestplugin by--julia-<julia_option>(e.g.,--julia-compiled-modules=no). Seepytest -p julia.pytestplugin --helpfor the actual list of options.
Fixture¶
PyJulia’s pytest plugin includes a pytest fixture julia which is
set to an instance of Julia that is appropriately
initialized. Example usage:
def test_eval(julia):
assert julia.eval("1 + 1") == 2
This fixture also “marks” that this test requires a Julia runtime.
Thus, the tests using julia fixture are not run when
--no-julia is passed.
Marker¶
PyJulia’s pytest plugin also includes a pytest marker julia
which can be used to mark that the test requires PyJulia setup. It is
similar to julia fixture but it does not instantiate the actual
Julia object.
Example usage:
import pytest
@pytest.mark.julia
def test_import():
from julia import MyModule