Checker Operations¶
The checker plugin provides a framework for defining and using descriptive operations. A set of common operations are registered by default (see Checker Plugin). However, in addition to the default users can define and register their own custom operations.
The core building block is the DescriptiveOp
class, which wraps callable operations with descriptive text.
Key Components¶
DescriptiveOp
: A class that wraps callable operations with descriptive text.register_op
: A function to register new operations dynamically, using their description.get_op
: A function to retrieve registered operations by their description.list_ops
: A function to list all currently registered operation descriptions.
Registering Custom Operations¶
In addition to the default set of operations provided by the checker plugin, users can define and register their own custom operations.
How to Define a Custom Operation¶
A custom operation is defined using the DescriptiveOp
class, which requires a callable (such as a function) and a descriptive string. The callable should accept at least one argument and return a boolean value.
Here's an example of defining a custom operation:
from zeloscloud.pytest.checker import ops
# Define your custom operation
def is_multiple_of(x, y):
"""Check if x is a multiple of y"""
return x % y == 0
# Create a DescriptiveOp instance
custom_op = ops.DescriptiveOp(is_multiple_of, "is multiple of")
# Register the operation
ops.register_op(custom_op)
Warning
Operation descriptions should be unique, otherwise they will fail registration. You can optionally overwrite an operator using the register_op
function with the overwrite=True
flag.
ops.register_op(custom_op, overwrite=True)
Using Your Custom Operation¶
Once you have registered your custom operation, you can use it in your checks just like any other operation.
Example usage:
def test_custom_op(check):
"""Perform a check using the custom `is_multiple_of` operation."""
check.that(10, "is multiple of", 5)
Troubleshooting¶
Common Issues¶
- Duplicate Operation Descriptions: Ensure that each operation has a unique description. Duplicate descriptions can lead to unexpected behavior or overwriting of operations.
# This will fail if "is multiple of" is already registered
ops.register_op(custom_op)
-
Operation Not Found: If you encounter an error stating that an operation is not found, check that the operation has been correctly registered and that the description matches exactly.
-
Impact on Global State: Be aware that registering or unregistering operations affects the global state of the Checker plugin. Changes made are not isolated to individual tests.
API Reference¶
See zeloscloud.pytest.checker.ops in the API Reference.