Hooks
What are Hooks?
Hooks allow you to perform custom actions, such as modifying the contract before it is loaded, or processing test results after execution.
A hook is simply a command that, which can be configured to run at specific stages of the contract testing process. Additional data may be passed to the hook in the form of environment variables or on standard input, depending on the hook type.
Configuration
- Version 3 (recommended)
- Version 2
- YAML
- specmatic.json
specmatic.yaml
version: 3
dependencies:
data:
adapters:
post_specmatic_response_processor: python post_specmatic_response_processor.py
pre_specmatic_request_processor: python pre_specmatic_request_processor.py
pre_specmatic_response_processor: python pre_specmatic_response_processor.py
specmatic.json
{
"version": 3,
"dependencies": {
"data": {
"adapters": {
"post_specmatic_response_processor": "python post_specmatic_response_processor.py",
"pre_specmatic_request_processor": "python pre_specmatic_request_processor.py",
"pre_specmatic_response_processor": "python pre_specmatic_response_processor.py"
}
}
}
}
- YAML
- JSON
specmatic.yaml
version: 2
hooks:
post_specmatic_response_processor: python post_specmatic_response_processor.py
pre_specmatic_request_processor: python pre_specmatic_request_processor.py
pre_specmatic_response_processor: python pre_specmatic_response_processor.py
specmatic.json
{
"version": 2,
"hooks": {
"post_specmatic_response_processor": "python post_specmatic_response_processor.py",
"pre_specmatic_request_processor": "python pre_specmatic_request_processor.py",
"pre_specmatic_response_processor": "python pre_specmatic_response_processor.py"
}
}
How Hooks Work
As an example, in the above snippet, stub_load_contract is the hook name. python load.py is executed, while the path of the original contract file is present in CONTRACT_FILE environment variable.
The command can parse the contract file and write it to standard out. Specmatic will read it as the new contract.