DolphinScheduler API Automation Test

Preparatory knowledge

The difference between API Test and Unit Test

API test, which imitates the user calling API, starts from a certain entry and performs operations step by step until a certain work is completed. Different from unit testing, the latter usually needs to test parameters, parameter types, parameter values, parameter numbers, return values, throw errors, etc. in order to ensure that a specific function can complete its work stably and reliably in any case. Unit tests assume that the entire product will work as long as all functions work properly.

In contrast, API testing focuses on whether a complete operation chain can be completed

For example, the API test of the tenant management interface focuses on whether users can log in normally; If the login fails, whether the error message can be displayed correctly. After logging in, you can perform tenant management operations through the sessionid you carry.

API Test

API-Pages

DolphinScheduler’s API tests are deployed using docker-compose. The current tests are in standalone mode and are mainly used to check some basic functions such as “add, delete, change and check”. For further cluster validation, such as collaboration between services or communication mechanisms between services, refer to deploy/docker/docker-compose.yml for configuration.

For API test, the page model form is used, mainly to create a corresponding model for each page. The following is an example of a login page.

  1. package org.apache.dolphinscheduler.api.test.pages;
  2. import org.apache.dolphinscheduler.api.test.entity.HttpResponse;
  3. import org.apache.dolphinscheduler.api.test.utils.RequestClient;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. public final class LoginPage {
  7. public HttpResponse login(String username, String password) {
  8. Map<String, Object> params = new HashMap<>();
  9. params.put("userName", username);
  10. params.put("userPassword", password);
  11. RequestClient requestClient = new RequestClient();
  12. return requestClient.post("/login", null, params);
  13. }
  14. }

During the test process, we only test the interfaces we need to focus on, not all interfaces in the page. Therefore, we only declare the user name, password and interface path on the login page.

In addition, during the testing process, the interface are not requested directly. The general choice is to package the corresponding methods to achieve the effect of reuse. For example, if you want to log in, you input your username and password through the public LoginPage login() method to manipulate the elements you pass in to achieve the effect of logging in. That is, when the user finishes logging in, he or she achieve the effect of login.

On the login page, only the input parameter specification of the interface request is defined. For the output parameter of the interface request, only the unified basic response structure is defined. The data actually returned by the interface is tested in the actual test case. Whether the input and output of main test interfaces can meet the requirements of test cases.

API-Cases

The following is an example of a tenant management test. As explained earlier, we use docker-compose for deployment, so for each test case, we need to import the corresponding file in the form of an annotation.

The interface is requested using the RemoteWebDriver provided with Selenium. Before each test case is started there is some preparation work that needs to be done. For example: logging in the user, jumping to the corresponding page (depending on the specific test case).

  1. @BeforeAll
  2. public static void setup() {
  3. LoginPage loginPage = new LoginPage();
  4. HttpResponse loginHttpResponse = loginPage.login(user, password);
  5. sessionId = JSONUtils.convertValue(loginHttpResponse.body().data(), LoginResponseData.class).sessionId();
  6. }

When the preparation is complete, it is time for the formal test case writing. We use a form of @Order() annotation for modularity, to confirm the order of the tests. After the tests have been run, assertions are used to determine if the tests were successful, and if the assertion returns true, the tenant creation was successful. The following code can be used as a reference:

  1. @Test
  2. @Order(1)
  3. public void testCreateTenant() {
  4. TenantPage tenantPage = new TenantPage();
  5. HttpResponse createTenantHttpResponse = tenantPage.createTenant(sessionId, tenant, 1, "");
  6. Assertions.assertTrue(createTenantHttpResponse.body().success());
  7. }

The rest are similar cases and can be understood by referring to the specific source code.

https://github.com/apache/dolphinscheduler/tree/dev/dolphinscheduler-api-test/dolphinscheduler-api-test-case/src/test/java/org/apache/dolphinscheduler/api.test/cases

Supplements

When running API tests locally, First, you need to start the local service, you can refer to this page: development-environment-setup

When running API tests locally, the -Dlocal=true parameter can be configured to connect locally and facilitate changes to the UI.

The current default request timeout length is 10 seconds. This value should not be modified without special requirements.