{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":32231,"path":"it-service-management/devops-change-velocity/test-api-tools.md","title":"Add test results to change requests using test API","bundle":null,"doc_type":null,"release":"australia","body":"\n# Add test results to change requests using test API\n\nAdd test results to DevOps change requests using the test capability of the DevOps - POST /devops/tool/\\{capability\\} API. The API is available for all test types for various tools connected to DevOps Change Velocity.\n\nFor more information about the API, see [DevOps - POST /devops/tool/\\{capability\\}](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/devops-api.md). To use the API in Azure DevOps, GitHub, and Jenkins, you must use the format mentioned in the respective sections below.\n\n**Prerequisites**\n\nRole required: sn\\_devops.admin\n\nMake sure that the test type you want to use in the API is available as a test type in DevOps Change Velocity and mapped to the respective orchestration tool.\n\n1.  Navigate to **DevOps Change Workspace** &gt; **Administration** &gt; **Integrations** &gt; **Test types**.\n\n    You can also use the classic UI navigation **DevOps** &gt; **Integrations** &gt; **Test Types**.\n\n2.  Verify if the test type is available in the Test types table.\n\n    \\[Omitted image \"test-api-12.png\"\\] Alt text: Test types table.\n\n3.  For custom test types, select **New** and add the test type to the list.\n    1.  In the **Test type** field, enter a name for your test type.\n    2.  From the **Test category** list, select the category of your test type.\n    3.  Select **Save**.\n4.  Navigate to **DevOps Change Workspace** &gt; **Administration** &gt; **Integrations** &gt; **Test type mappings**.\n\n    You can also use the classic UI navigation **DevOps** &gt; **Integrations** &gt; **Test Type Mappings**.\n\n5.  Verify if the test type is mapped to your orchestration tool.\n\n    \\[Omitted image \"test-api-13.png\"\\] Alt text: Test type mappings table.\n\n6.  If the mapping is not available, then select **New** and create the mapping.\n    1.  From the **Test type** list, select the test type that you want to map.\n    2.  From the **Tool integration** list, select your orchestration tool.\n    3.  Select **Save**.\n\n## Azure DevOps\n\nFor Azure DevOps pipelines, use the format given here to add test results to change requests for various test types.\n\nFormat for URL suffix and parameters:\n\n```\n/api/sn_devops/v1/devops/tool/test?toolId=<devops_ToolID>&testType=<test_type>\n```\n\n-   The parameter **toolId** must be a valid Tool Id from a connected and configured tool in DevOps Change Velocity.\n-   The parameter **testType** should have a test type mapping in DevOps Change Velocity.\n\nWithin the **Body**, you must pass the parameters based on the test type.\n\n**Note:** For the generic connection, use the following:\n\n-   username: devops.integration.user\n-   password: &lt;devops.integration.user password&gt;\n\n**Pipeline configuration for performance tests**\n\nSample parameters for the body:\n\n```\n{\n  \"name\": \"Perf. tests DEMO5\",\n  \"duration\": 0,\n  \"maximumVirtualUsers\": 10,\n  \"throughput\": \"5/min\",\n  \"maximumTime\": 10000,\n  \"minimumTime\": 5000,\n  \"averageTime\": 7000,\n  \"ninetyPercent\": 8000,\n  \"standardDeviation\": 2,\n\"startTime\": \"2023-12-14T23:31:31z\",\n  \"finishTime\": \"2023-12-14T23:40:32z\",\n  \"buildNumber\": $(Build.BuildNumber),\n  \"stageName\": \"Test API Job\",\n  \"pipelineName\": \"TEST-CI-PIPELINE\"\n}\n```\n\n\\[Omitted image \"test-api-05.png\"\\] Alt text: Sample Azure DevOps pipeline configuration for performance tests.\n\n**Performance test summary in your Now instance**:\n\n\\[Omitted image \"test-api-06.png\"\\] Alt text: Performance test summary.\n\n**Test summary attached to your DevOps change request**:\n\n\\[Omitted image \"test-api-07.png\"\\] Alt text: Change request test summary.\n\n**Pipeline configuration for other functional and unit tests**\n\nSample parameters for the body:\n\n```\n{\n  \"name\":\" Smoke Test summary\",\n   \"duration\":0.0,\n   \"passedTests\":1,\n   \"failedTests\":1,\n   \"skippedTests\":1,\n   \"blockedTests\":1,\n   \"totalTests\":4,\n  \"startTime\": \"2023-12-14T23:31:31z\",\n  \"finishTime\": \"2023-12-15T23:31:31z\",\n  \"buildNumber\": $(Build. BuildNumber),\n \"stageName\": \"Test API Job\",\n  \"pipelineName\": \"TEST-CI-PIPELINE\"\n}\n```\n\n\\[Omitted image \"test-api-03.png\"\\] Alt text: Sample Azure DevOps pipeline configuration for other functional tests.\n\n**Test summary attached to your DevOps change request**:\n\n\\[Omitted image \"test-api-04.png\"\\] Alt text: Change request test summary.\n\n## Azure DevOps YAML pipeline\n\nSample pipeline:\n\n```\nstages:\n  - stage: BashScript\n    jobs:\n      - job: 'bash'\n        steps:\n        - script: echo Hello, world!\n          displayName: 'BashScript'\n\n        \n  - stage: TestAPI\n    dependsOn: BashScript\n    jobs:\n      - job: 'RESTTestAPI'\n        # dependsOn: BashScript\n        pool: server\n        steps:\n        - task: InvokeRESTAPI@1\n          displayName: 'Invoke REST API: POST'\n          inputs:\n            connectionType: 'connectedServiceName'\n            serviceConnection: 'v01 - generic connection'\n            method: 'POST'\n            body: |\n              {\n                            \"name\": \"Performance test summary kl2\",\n                            \"duration\": 0,\n                            \"maximumVirtualUsers\": 10,\n                            \"throughput\": \"5/min\",\n                            \"maximumTime\": 10000,\n                            \"minimumTime\": 5000,\n                            \"averageTime\": 7000,\n                            \"ninetyPercent\": 8000,\n                            \"standardDeviation\": 2,\n                            \"startTime\": \"2023-12-14T23:31:31z\",\n                            \"finishTime\": \"2023-12-14T23:40:32z\",\n                            \"buildNumber\": $(Build.BuildId),\n                            \"stageName\": \"TestAPI/RestTestAPI\",\n                            \"pipelineName\": \"TESTYMLPipeline\"\n                          }\n            urlSuffix: '/api/sn_devops/v1/devops/tool/test?toolId=<devops_ToolID>&testType=<test_type>'\n            waitForCompletion: 'false'\n\n          \n```\n\n\\[Omitted image \"test-api-08.png\"\\] Alt text: Azure DevOps yml pipeline.\n\n**Release pipeline**\n\n\\[Omitted image \"test-api-09.png\"\\] Alt text: Azure DevOps release pipeline yml.\n\n## GitHub\n\nFor GitHub pipelines, use the format given here to add test results to change requests for various test types.\n\n**Pipeline configuration for performance tests**\n\nSample pipeline:\n\n```\nname: Test API Call Perf\n\non: \n  workflow_dispatch:\n\njobs:\n  post_api_call_perf:\n    runs-on: ubuntu-latest\n    \n    steps:\n    - name: Checkout Repository\n      uses: actions/checkout@v2\n\n    - name: post_api_call_perf\n      run: |\n        apiUrl=\"https://<your_instance>.service-now.com/api/sn_devops/v1/devops/tool/test?toolId=<devops_ToolId>&testType=<performance_test_type>\"\n        requestBody='{\"toolId\": \"<devops_ToolId>\", \"buildNumber\": '${{ github.run_number }}' , \"workflow\": \"Test API Call Perf\", \"repository\": \"<your_GitHub_repository>\", \"stageName\":\"post_api_call_perf\",\"pipelineName\":\"Test API Call Perf\", \"testType\": \"Load\", \"buildNumber\": '${{ github.run_number }}', \"buildId\": '${{ github.run_id }}' , \"attemptNumber\" : '${{ github.run_attempt }}', \"name\": \"GitHub Test Summary - Performance 1.0\",\"duration\": 0.0,\"maximumVirtualUsers\": 10,\"throughput\": \"5/min\",\"maximumTime\": 1000,\"minimumTime\": 5000,\"averageTime\": 7000,\"startTime\": \"2023-12-14T23:31:31z\",\"finishTime\": \"2023-12-14T23:31:31z\",\"ninetyPercent\": 8000, \"standardDeviation\": 2.0 }'\n        auth=\"devops.integration.user:<password> in base64 encoded\"\n        authHeader=\"Basic $auth\"\n        response=$(curl -X POST -H \"Content-Type: application/json\" -H \"Authorization: $authHeader\" -d \"$requestBody\" $apiUrl)\n        exitCode=$?\n        \n        if [ $exitCode -eq 0 ]; then\n          echo \"API call successful!\"\n          echo \"Response: $response\"\n        else\n          echo \"API call failed!\"\n          echo \"Response: $response\"\n          exit 1\n        fi\n\n```\n\n\\[Omitted image \"test-api-11.png\"\\] Alt text: Sample GitHub pipeline configuration for performance tests.\n\n**Pipeline configuration for other functional and unit tests**\n\nSample pipeline:\n\n```\nname: Test API Call\n\non: \n  workflow_dispatch:\n\njobs:\n  post_api_call:\n    runs-on: ubuntu-latest\n    \n    steps:\n    - name: Checkout Repository\n      uses: actions/checkout@v2\n\n    - name: post_api_call\n      run: |\n        apiUrl=\"https://<your_instance>.service-now.com/api/sn_devops/v1/devops/tool/test?toolId=<devops_ToolID>&testType=<test_type>\"\n        requestBody='{\"toolId\": \"<devops_ToolID>\", \"buildNumber\": '${{ github.run_number }}' , \"workflow\": \"Test API Call\", \"repository\": \"<your_GitHub_repository>\", \"stageName\":\"post_api_call\",\"pipelineName\":\"TestAPICall\", \"testType\": \"Smoke\", \"buildNumber\": '${{ github.run_number }}', \"buildId\": '${{ github.run_id }}' , \"attemptNumber\" : '${{ github.run_attempt }}', \"testSummaries\": [{\"name\": \"GitHub Test Summary - smoke\",\"passedTests\": 1,\"failedTests\": 1,\"skippedTests\": 1,\"ignoredTests\": 0,\"blockedTests\": 1,\"totalTests\": 4,\"startTime\": \"2023-12-14T23:31:31z\",\"endTime\": \"2023-12-14T23:31:31z\",\"duration\": 0.0,\"testType\": \"Smoke\", \"suites\": []}] }'\n        auth=\"devops.integration.user:<password> in base64 encoded\"\n        \n        authHeader=\"Basic $auth\"\n        response=$(curl -X POST -H \"Content-Type: application/json\" -H \"Authorization: $authHeader\" -d \"$requestBody\" $apiUrl)\n        exitCode=$?\n        \n        if [ $exitCode -eq 0 ]; then\n          echo \"API call successful!\"\n          echo \"Response: $response\"\n        else\n          echo \"API call failed!\"\n          echo \"Response: $response\"\n          exit 1\n        fi\n\n```\n\n\\[Omitted image \"test-api-10.png\"\\] Alt text: Sample GitHub pipeline configuration for other functional tests.\n\n## Jenkins\n\nFor Jenkins pipelines, use the format given here to add test results to change requests for various test types.\n\n**Pipeline configuration for performance tests**\n\nSample pipeline:\n\n```\npipeline {\n    agent any\n    stages {\n        stage('POST API Call') {\n            steps {\n                script {\n                    def apiUrl = 'https://<your_instance>.service-now.com/api/sn_devops/v1/devops/tool/test?toolId=<devops_ToolID>&testType=<performance_test_type>'\n                    def requestBody = [\n                        name: \"Performance Load tests jenkins\",\n                        duration: 0.0,\n                        maximumVirtualUsers: 10,\n                        throughput: \"6/min\",\n                        maximumTime: 1000,\n                        minimumTime: 300,\n                        averageTime: 700,\n                        ninetyPercent: 600,\n                        standardDeviation: 2.0,\n                        startTime: \"2023-12-14T23:31:31z\",\n                        finishTime: \"2023-12-14T23:40:32z\",\n                        buildNumber: env.BUILD_NUMBER,\n                        stageName: \"POST API Call\",\n                        pipelineName: \"KLPerformanceTestPipeline\"\n                    ]\n                    def auth = \"<password>\" // devops.integration.user:<password> in base64 encoded.\n                    def authHeader = \"Basic \" + auth\n                    def response = httpRequest(\n                        contentType: 'APPLICATION_JSON',\n                        httpMode: 'POST',\n                        requestBody: groovy.json.JsonOutput.toJson(requestBody),\n                        url: apiUrl,\n                        customHeaders: [[name: 'Authorization', value: authHeader]]\n                    )\n\n                    if (response.status == 200 || response.status == 201) {\n                        echo \"API call successful!\"\n                        echo \"Response: ${response.content}\"\n                    } else {\n                        error \"API call failed with status ${response.status}\"\n                    }\n                }\n            }\n        }\n    }\n}\n```\n\n\\[Omitted image \"test-api-01.png\"\\] Alt text: Sample Jenkins pipeline configuration for performance tests.\n\n**Pipeline configuration for other functional and unit tests**\n\nSample pipeline:\n\n```\npipeline {\n    agent any\n    \n    stages {\n        stage('POST API Call') {\n            steps {\n                script {\n                    def apiUrl = 'https://<your_instance>.service-now.com/api/sn_devops/v1/devops/tool/test?toolId=<devops_ToolID>&testType=<test_type>'\n                    def requestBody = [\n                        name: \"Smoke tests\",\n                        duration: 0.0,\n                        passedTests: 1,\n                        failedTests: 1,\n                        skippedTests: 1,\n                        blockedTests: 1,\n                        totalTests: 4,\n                        startTime: \"2023-12-14T23:31:31z\",\n\t                    finishTime: \"2023-12-15T23:31:31z\",\n                        buildNumber: env.BUILD_NUMBER,\n                        stageName: \"POST API Call\",\n                        pipelineName: \"TestPipelineKL\"\n                    ]\n                    \n                    def auth = \"<password>\" // devops.integration.user:<password> in base64 encoded.\n                    def authHeader = \"Basic \" + auth\n                    \n                    def response = httpRequest(\n                        contentType: 'APPLICATION_JSON',\n                        httpMode: 'POST',\n                        requestBody: groovy.json.JsonOutput.toJson(requestBody),\n                        url: apiUrl,\n                        customHeaders: [[name: 'Authorization', value: authHeader]]\n                    )\n                    \n                    if (response.status == 200 || response.status == 201) {\n                        echo \"API call successful!\"\n                        echo \"Response: ${response.content}\"\n                    } else {\n                        error \"API call failed with status ${response.status}\"\n                    }\n                }\n            }\n        }\n    }\n}\n```\n\n\\[Omitted image \"test-api-02.png\"\\] Alt text: Sample Jenkins pipeline configuration for other functional tests.\n\n## Limitations of the test API\n\n-   Waiting logic will not be taken care of.\n-   Re-run scenarios cannot be handled.\n\n**Parent Topic:**[DevOps test tool integration](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/it-service-management/devops-change-velocity/dev-ops-test-tool-integration.md)\n\n"}}