Overview
The Antithesis Python SDK enables you to integrate your Python applications with Antithesis and is available on Github here. The Antithesis Python SDK is intended for use with Python 3.9.0 or higher. Contact us if you want to use a different version.Functionality
Like our other SDKs, the Antithesis Python SDK offers three main types of functionality:- The assertions module allows you to define test properties about your software or test template.
- The random module functions request structured and unstructured randomness from the Antithesis Platform.
- The lifecycle module functions inform the Antithesis Environment that particular test phases or milestones have been reached.
The SDK includes several other modules that you should never directly use in your code. Modules with names beginning with
_ are internal tooling for the SDK.Instrumentation
The Antithesis Platform includes an instrumentor package that, in the case of Python, handles assertion cataloging but not coverage instrumentation, please see Instrumentation for a more detailed explanation of the difference. Place directories, files, or symlinks to the code you want instrumented or cataloged in the directory/opt/antithesis/catalog/ within your container image. Antithesis will follow all symlinks while resolving the path to the cataloging directory.
However, it will follow only one symlink deep into each item placed within /opt/antithesis/catalog/. That is, you should not symlink to a symlink.
Example cataloging directory:
dotnet_file.dllpython_script.pymain.jar
my_other.jar- this will be ignored as we already encountered one symlink when traversing down fromopt/antithesis/catalog/more_files.
.py files in this directory.
The instrumentor will, by default, act upon every language it supports in the cataloging directory: (Java , Python, JavaScript, .NET), not just Python.
Using the SDK
The basic workflow for using the Antithesis Python SDK is:- Include the SDK in your dependencies:
- Import the SDK into each python module that uses specific SDK functions:
- Call SDK functions from your code - for example:
-
Run your Python project. For example, run
python -m myapp. - Deploy your build into production, or into Antithesis to test.
SDK runtime behavior
When your code is run outside Antithesis, our SDK has sensible fallback behavior with minimal performance overhead. This enables you to have a single build of your software that runs both inside and outside Antithesis. Functions in the assertions module and lifecycle module have 2 modes for local execution:-
Default, where
assertandlifecycleuse local implementations of Antithesis functionality. However, the results will not be logged anywhere because no logfile has been specified. This mode is the default behavior, called when you run, for instance:python -m myapp. -
Default with logging, which is the same as the above but logs output locally in a structured JSON format.
This mode is selected at runtime by setting the environment variable
ANTITHESIS_SDK_LOCAL_OUTPUTat program startup. This variable must be set to a filepath: a logfile will be created at this location. The file must be located inside an already-existing directory. You may supply either a relative or absolute path.For instance, setANTITHESIS_SDK_LOCAL_OUTPUT=assertions_20240520.json python -m myappfor the example above.