## 4.2.9.7.1 Running ABAP Unit Test Runs
## Prerequisites
- You have an SAP BTP ABAP environment system.
- You've created a Communication User as described in How to Create Communication Users [page 2710].
- You've created a Communication System as described in How to Create Communication Systems.
- You've created a Communication Arrangement as described in How to Create a Communication Arrangement [page 2712].
- You've selected the communication scenario SAP\_COM\_0735 for your communication arrangement and have mapped it to your communication system.
## Context
ABAP Unit is the standard tool for running unit tests. In this help topic, you'll learn how to start an ABAP Unit Test Run via REST Service.
For more information about ABAP Unit, see Ensuring Quality of ABAP Code.
## Procedure
1. Get CSRF token: The first step serves for the authentication on the server. The response header contains a CSRF token, which is used as authentication for the POST request following in step 2.
Request
Authentication Type : Basic Authentication
GET https:/ /&lt;host.com&gt;:&lt;port&gt;/sap/bc/adt/api/abapunit/runs/ 00000000000000000000000000000000
Headers
KEY
| accept       | application/vnd.sap.adt.api.abapunit.run-status.v1+xml   |
|--------------|----------------------------------------------------------|
| x-csrf-token | fetch                                                    |
## Response
Headers
| KEY          | VALUE                                                           |
|--------------|-----------------------------------------------------------------|
| x-csrf-token | <token>                                                         |
| location     | /sap/bc/adt/api/abapunit/runs/ 00000000000000000000000000000000 |
2. Start an ABAP Unit Run: To start an ABAP Unit run, insert the CSRF token that was retrieved in the first request in the header parameters.
## Request
POST https:/ /&lt;host.com&gt;:&lt;port&gt;/sap/bc/adt/api/abapunit/runs
Headers
| KEY          | VALUE                                           |
|--------------|-------------------------------------------------|
| x-csrf-token | <token>                                         |
| content type | application/vnd.sap.adt.api.abapunit.run.v1+xml |
## Body
You can specify arbitrary object sets. See also Object Sets and XML Representations of Object Sets for more details.
You can provide additional parameters to control the selection of tests. You can restrict the amount of tests to certain risk level and durations. Furthermore, you can decide to run foreign tests, which are connected via test relations.
```
<?xml version="1.0" encoding="UTF-8"?> <aunit:run title="My Run" context="AIE Integration Test" xmlns:aunit="http:// www.sap.com/adt/api/aunit">
```
## VALUE
```
<aunit:options> <aunit:measurements type="none"/> <aunit:scope ownTests="true" foreignTests="true"/> <aunit:riskLevel harmless="true" dangerous="true" critical="true"/> <aunit:duration short="true" medium="true" long="true"/> </aunit:options> <osl:objectSet xsi:type="unionSet" xmlns:xsi="http://www.w3.org/2001/ XMLSchema-instance" xmlns:osl="http://www.sap.com/api/osl"> <osl:set xsi:type="osl:packageSet"> <osl:package includeSubpackages="true" name="PACKAGE_ONE"/> </osl:set> <osl:set xsi:type="osl:flatObjectSet"> <osl:object name="ZCL_TEST_ONE" type="CLAS"/> <osl:object name="ZIF_TEST_TWO" type="INTF"/> </osl:set> </osl:objectSet> </aunit:run>
```
## Response
Headers
KEY
location
VALUE
/sap/bc/adt/api/abapunit/runs/{runId}
3. Tracking the Status of the Test Run: To track the status of the test run, you can make a GET request using the URI contained in the location header.
## Request
GET https:/ /&lt;host.com&gt;:&lt;port&gt;/sap/bc/adt/api/abapunit/runs/{runId}
Headers
KEY
accept
## Response
While the tests are still running, the returned status will be 'In Process' .
If all tests have been run, the status 'Completed' is returned. You'll find the link /sap/bc/adt/api/atc/ results/&lt;UUID&gt; at the bottom of the body. Use this link to retrieve the ABAP Unit test results in the next step.
## Body
```
<?xml version="1.0" encoding="UTF-8"?> <aunit:run xmlns:aunit="http://www.sap.com/adt/api/aunit" title="MyTitle" context="MyContext"> <aunit:progress status="FINISHED" percentage="100" /> <aunit:executedBy user="JOHNDOE" /> <aunit:time started="2021-02-12T17:09:19Z" ended="2021-02-12T17:09:22Z" /> <atom:link xmlns:atom="http://www.w3.org/2005/Atom" href="/sap/bc/adt/api/abapunit/results/0123821392189313" rel="http:// www.sap.com/adt/relations/api/abapunit/run-result" type="application/ vnd.sap.adt.api.junit.run-result.v1+xml"/> </aunit:run>
```
VALUE
application/vnd.sap.adt.api.abapunit.run-status.v1+xml
4. Retrieve Results: To get the ABAP Unit results, use the following request.
## Request
GET https:/ /&lt;host.com&gt;:&lt;port&gt;/sap/bc/adt/api/abapunit/results/{resultId}
Headers
KEY
Accept
## Response
The results are submitted in the JUnit format.
## Body
As an example, the result may look like this:
```
<?xml version="1.0" encoding="UTF-8"?> <testsuites tests="3" asserts="2" skipped="1" errors="0" failures="1" timestamp="2021-01-22T19:17:30Z" time="0.36" executedBy="JOHNDOE" client="000" system="UIA"> <testsuite tests="2" asserts="2" skipped="1" errors="0" failures="1" timestamp="2021-01-22T19:17:30Z" time="0.28 " hostname="ldai3uia" package="test_cars" name=""> <testcase asserts="1" time="0.01 " name="order" classname="fugr:zmg_cars.ltc_finder"> <failure type="Assert Failure" message="Critical Assertion Error: 'Cds_View: ASSERT_EQUALS'"> Character string different as of position 4Expected [CL_AUNIT_TESTABLE_OBJECT======CP]Actual [CL_CAR========================CP] Test 'LTC_FINDER->ORDER' in Main Program 'SAPLZMG_CARS' Stack: Include: <LZMG_CARST99> Line: <38> (ORDER) </failure> </testcase> <testcase asserts="1" time="0.01 " name="preview" classname="fugr:zmg_cars.ltc_finder"> <skipped message="Missing Prerequisites - Dummy: ABORT"> Test execution skipped due to missing prerequisites Test 'LTC_FINDER->PREVIEW' in Main Program 'SAPLZMG_CARS' Stack: Include: <LZMG_CARST99> Line: <44> (PREVIEW) </skipped> </testcase> <testcase asserts="0" time="0.01 " name="drive" classname="fugr:zmg_cars.ltc_finder"/> </testsuite> </testsuites>
```
## VALUE
application/vnd.sap.adt.api.junit.run-result.v1+xml