Overview
Functionize Extensions provide powerful, reusable logic that can be integrated directly into your test cases as pre/post-action hooks or as a standalone Extension Action. A common use case is creating an extension that requires dynamic inputs during a test run, such as fetching data from an external API or database based on test-specific parameters. This guide explains how to correctly design and pass data to your extensions within a test case.
How It Works
There is a fundamental difference between testing an extension on the Extension Management page and executing it within a live test case. Understanding this difference is key to designing your extension correctly.
- Testing in Extension Management: When you test an extension directly from the management page, the UI provides a field to enter a JSON body. This simulates an HTTP request, and your code can use methods like
request.get_json()to access this input. This method is for testing and validation purposes only. - Executing in a Test Case: When an extension runs as part of a test, it does not operate like an HTTP endpoint. There is no request body to parse. Instead, inputs must be passed as direct arguments to the function.
To make your extension compatible with test case execution, you must refactor it to accept parameters through its function signature rather than reading from a request body.
Example: Refactoring an Extension
Imagine an extension designed to fetch data from a Snowflake database, which was initially tested using a JSON body.
Incorrect (API-style, for testing only):
def get_snowflake_data():# This will fail in a test case because request.get_json() is not availabledata = request.get_json()query = data.get('query')# ... function logic to connect to Snowflake and run the query ...
Correct (Test Case Compatible):
def get_snowflake_data(account, user, warehouse, private_key, query):# Inputs are received directly as function arguments# ... function logic using the provided arguments to connect and run the query ...
Once your code is refactored, you can map test variables or static values to these arguments in the test step's settings where you apply the extension.
Limitations and Key Considerations
- No HTTP Request Object: The execution environment for extensions within a test does not include a traditional HTTP request object. Your code cannot read from a request body, headers, or query strings.
- Function Arguments are Key: All dynamic inputs must be passed as arguments defined in your extension's function signature.
- Parameter Mapping: You must map the required arguments in the test action's settings (for pre/post-hooks) or in the standalone Extension Action. The order and names should correspond to your function's definition to ensure correct execution.
Related Information
- Understanding Extensions
- Mapping Extensions