Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

auto create test cases in zephyr scale using rest api and python

Saurabh Pathak
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 7, 2024

auto create test cases in zephyr scale using rest api and python

1 answer

0 votes
Ignacio Aredez
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 7, 2024

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:

 

  1. Environment Setup

Ensure you have Python installed on your system. It's recommended to use a virtual environment for Python projects to manage dependencies efficiently.

 

  1. Obtain API Token

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.

 

  1. Install Required Python Packages

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

```

 

  1. Create Test Cases Using the API

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)

```

 

  1. Execute the Script

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.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events