auto create test cases in zephyr scale using rest api and python
This involves a few key steps, including setting up your environment, generating an API token for authentication, and using Python to send requests to the Zephyr Scale API. Below is a detailed guide on how to accomplish this:
Ensure you have Python installed on your system. It's recommended to use a virtual environment for Python projects to manage dependencies efficiently.
To interact with Zephyr Scale via its API, you need an API token. Follow these steps to generate one:
- Log into your Jira instance.
- Navigate to Apps > Zephyr Scale .
- Go to API Keys (under Integrations) and generate a new API token.
You'll need the `requests` package to make HTTP requests. If you're using a virtual environment, activate it and install the package using pip:
```bash
pip install requests
```
You'll use the Zephyr Scale API to create test cases. The following Python script demonstrates how to do this. Replace `YOUR_API_TOKEN`, `YOUR_JIRA_DOMAIN`, and other placeholders with your actual data.
```python
import requests
import json
Replace these variables with your information
api_token = 'YOUR_API_TOKEN'
jira_domain = 'YOUR_JIRA_DOMAIN'
project_key = 'YOUR_PROJECT_KEY'
test_cycle_key = 'YOUR_TEST_CYCLE_KEY' Optional, if you want to link the test case to a test cycle
API Endpoint for creating a test case in Zephyr Scale
url = f'https://{jira_domain}/rest/atlassian-connect/1/project/{project_key}/testcase'
Headers for authentication and content type
headers = {
'Authorization': f'Bearer {api_token}',
'Content-Type': 'application/json',
}
Test case data
test_case_data = {
"name": "Test Case Name",
"objective": "Test Case Objective",
"precondition": "Preconditions for the test case",
"labels": ["label1", "label2"],
"priority": "Normal",
"status": "Approved",
"steps": [
{
"description": "Step 1 description",
"testData": "Test data for step 1",
"expectedResult": "Expected result for step 1"
},
{
"description": "Step 2 description",
"testData": "Test data for step 2",
"expectedResult": "Expected result for step 2"
}
],
Uncomment if linking to a test cycle
"testScript": {
"type": "STEP_BY_STEP",
"testCycleKey": test_cycle_key
}
}
Make the POST request to create the test case
response = requests.post(url, headers=headers, data=json.dumps(test_case_data))
Check for success
if response.status_code == 200:
print("Test case created successfully.")
print("Test Case ID:", response.json()['key'])
else:
print("Failed to create test case.")
print("Response:", response.text)
```
Run the Python script. If everything is set up correctly, it should create a new test case in Zephyr Scale under the specified project.
Considerations
- API Limits: Be mindful of API rate limits to avoid getting temporarily banned.
- Error Handling: Implement error handling in your script to manage API request failures gracefully.
- Security: Securely manage your API token to prevent unauthorized access.
This script is a starting point. Depending on your specific requirements, you might want to customize the test case fields, handle responses more thoroughly, or manage test cycles and folders programmatically.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.