ZOSPy uses PyTest for unit testing. Tox is used to automate testing for different Python versions, and to run tests in an isolated environment. Running the tests with tox also ensures that the package can be properly installed.
- Install tox:
pip install tox
- Run all tests:
tox
- Alternatively, only selected test environments can be run:
# Run only on Python 3.10 in extension mode and 3.11 in standalone mode tox run -e py310-extension,py311-standalone # Run environments with label "test-standalone" only tox run -m test-standalone
- When running tests in extension mode, first open Zemax OpticStudio, enable the Interactive Extension mode, and uncheck "Auto Close on Disconnect".
--extension: Since the ZOS-API is limited to only a single connection per session, it is not possible to test ZOSPy in extension mode and standalone mode simultaneously. Specifying the command line flag--extensioninstructs PyTest to connect to Zemax OpticStudio in extension mode. Make sure the interactive extension mode has been activated andAuto Close on Disconnectis unchecked.--output-directory=<OUTPUT_DIRECTORY>: If specified, all created OpticStudio systems are saved to this directory.
These arguments can also be passed to tox, e.g. (note the double dashes -- after the tox arguments):
tox run -e py311-standalone -- --output-directory="zospy/is/cool" The unit tests for zospy.analyses rely on reference data in order to check the validity of analysis results.
If the unit tests are run for the first time using a specific version of ZOSPy, this data is not yet present.
It can be generated by running
python -m scripts.generate_test_reference_dataThe generated reference data files will be added to tests/data/reference.
The reference data files are generated using a specification in scripts/generate_test_reference_data/tests.yaml.
If new analys tests are added, this file needs to be extended with a reference specification for these tests.
A documented example specification is shown below.
- # OpticStudio model as defined in scripts/generate_test_reference_data/systems.py
model: simple_system
# ZOSPy analysis, specified as a module relative to zospy.analyses
analysis: raysandspots.ray_fan
# Unit test file, relative to tests/analyses
file: test_raysandspots.py
# Unit test for which this reference data is intended
test: test_ray_fan_returns_correct_result
# Parameters that are parametrized using @pytest.mark.parametrize
parametrized: [plot_scale, number_of_rays, tangential, sagittal]
# List with the parameter sets in parameter_name: parameter_value format
parameters:
- plot_scale: 0
number_of_rays: 20
tangential: Aberration_Y
sagittal: Aberration_X
- plot_scale: 1
number_of_rays: 40
tangential: Aberration_Y
sagittal: Aberration_X