HyperExecute Python Use Cases
This guide covers the most common real-world scenarios and troubleshooting cases when running Python, Pytest, and Behave test frameworks on TestMu AI HyperExecute. Each section describes a use case, its root cause, and the precise YAML translation required to configure or fix the issue within your hyperexecute.yaml.
1. Handling Python Version Compatibility Issues
Sometimes, test scripts fail due to Python version incompatibility. For example:
Verified$ python test_script.py
Traceback (most recent call last):
File "test_script.py", line 3, in <module>
from collections import MutableMapping
ImportError: cannot import name 'MutableMapping' from 'collections'
In Python versions 3.3 to 3.9, MutableMapping was part of the collections module. From Python 3.10 onward, it was moved to collections.abc, causing import errors in older code.
Solution : To ensure compatibility, match the Python runtime version with your local setup. Specify the version explicitly in your YAML configuration.
YAML Translation :
Verifiedruntime:
- language: python
version: "3.9"
2. Ensuring PIP Version Compatibility
Certain packages require a minimum version of PIP to install successfully. An outdated PIP version may cause the following error:
ERROR : You must install pip 21.0 or later to install this package.
Solution : Upgrade PIP before installing dependencies.
Verifiedpython -m pip install --upgrade pip
If you are using Python 3:
Verifiedpython3 -m pip install --upgrade pip
YAML Translation :
Verifiedpre:
- python3 -m pip install --upgrade pip
3. Fixing Missing requirements.txt Errors
If your pipeline fails with:
ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
It means that the requirements.txt file might be missing or ignored in .gitignore.
Solution : Ensure the file exists in the project root. Remove or comment out any requirements.txt entry from .gitignore. For this issue, ther are no YAML change required. This is a file management fix. However, verify that the command below correctly references the existing file name.
Verifiedpip install -r requirements.txt --cache-dir CacheDir
4. Restoring Missing .env Files
When environment variables are not set, tests may fail due to missing credentials or configuration values because .env files are commonly excluded in .gitignore for security. If ignored, they aren’t uploaded to HyperExecute, and environment variables remain unset.
Solution : Ensure that required .env files are not ignored in .gitignore. If sensitive credentials exist, use HyperExecute Secrets or Environment Variables instead of exposing them in .env files.
5. Optimizing Package Caching for Faster Jobs
If your pre step installs dependencies every time, your caching configuration might be incorrect.
Common Causes :
- Invalid
cacheKeysyntax. - Incorrect filename in cache reference.
- Missing
--cache-dirargument.
Solution : Use proper syntax, ensure file naming consistency, and define a cache directory.
YAML Translation:
VerifiedcacheKey: '{{ checksum "requirements.txt" }}'
cacheDirectories:
- CacheDir
pre:
- pip install -r requirements.txt --cache-dir CacheDir
6. Building Projects with Public and Private Registries
Case 1: Public Registry Installation
If all dependencies are public, install them directly:
Verifiedpip3 install -r requirements.txt --cache-dir CacheDir
YAML Translation :
Verifiedpre:
- pip3 install -r requirements.txt --cache-dir CacheDir
Case 2: Private Registry Installation
When using a private registry, authentication is required. Use proxy variables and authorization tokens to install packages securely.
Verifiedpip3 install --proxy http://$LT_PROXY_HOST:$LT_PROXY_PORT \
--header "Authorization: Bearer $PRIVATE_REGISTRY_TOKEN" \
-r requirements.txt
YAML Translation :
Verifiedpre:
- pip3 install --proxy http://$LT_PROXY_HOST:$LT_PROXY_PORT \
--header "Authorization: Bearer $PRIVATE_REGISTRY_TOKEN" \
-r requirements.txt
Sample requirements.txt file :
--extra-index-url https://your-private-registry.com/simple
--trusted-host your-private-registry.com
your-private-package==1.0.0
another-package==2.0.0
Error Troubleshooting :
- Verify
$LT_PROXY_HOSTand$LT_PROXY_PORTvalues. - Confirm
$PRIVATE_REGISTRY_TOKENis valid.
7. Configuring Database Connections
When connecting to a database during test runs, you may encounter:
ConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306
Solution : Expose the database service using the --expose flag in your YAML configuration.
YAML Translation :
Verifiedargs:
- "--expose mysql1:localhost:3309"
- "--expose mysql2:localhost:3307"
Sample Connection Code :
Verifiedimport os
import mysql.connector
db_host = os.getenv("MYSQL1_PROXY_HOST", "localhost")
db_port = os.getenv("MYSQL1_PROXY_PORT", "3309")
connection = mysql.connector.connect(
host=db_host,
port=db_port,
user="your_user",
password="your_password",
database="your_database"
)
8. Running Tests in Behave and Pytest
To execute tests based on substrings or tags:
Example Commands :
Verifiedpytest -k "TestAdvanceBoy"
behave -t @smoke
YAML Translation :
VerifiedtestRunnerCommand: pytest -k "TestAdvanceBoy"
# or
testRunnerCommand: behave -t @smoke
To execute an entire feature file:
Verifiedbehave -f feature_path/sample.feature
YAML Translation :
VerifiedtestRunnerCommand: behave -f feature_path/sample.feature
9. Discovering Tests Dynamically
- List all feature files containing a specific tag (e.g., @smoke):
grep -rl "@smoke" features/*.feature
YAML Translation :
VerifiedtestDiscovery:
type: raw
command: grep -rl "@smoke" features/*.feature
- List all scenarios under that tag :
grep -rH -A 3 "@smoke" features/*.feature | grep -i "Scenario" | cut -d ":" -f 2-
YAML Translation :
VerifiedtestDiscovery:
type: raw
command: grep -rH -A 3 "@smoke" features/*.feature | grep -i "Scenario" | cut -d ":" -f 2-
10. Generating JSON Reports with Behave
To generate JSON reports for your test execution:
Verifiedbehave -f json.pretty -o reports/test_report.json
YAML Translation :
Verifiedpost:
- behave -f json.pretty -o reports/test_report.json
11. Dynamically Assigning Artifact Names
To dynamically name artifacts using environment variables, define them in your YAML configuration.
YAML Translation :
VerifieduploadArtefacts:
- name: "${abcd}"
path:
- reports/**
Pass variable values through CLI during job execution:
Verified./hyperexecute -u <your_username> -k <your_access_key> -i <relative_path_of_config> --vars "abcd=Folder"
If abcd=Folder, the uploaded artifact will appear as Folder on the HyperExecute dashboard.
12. Using a Cache Directory for Package Installation
To enable caching for faster dependency installation:
Verifiedpip3 install -r requirements.txt --cache-dir CacheDir
This ensures packages are stored and reused in subsequent runs, reducing job setup time.