Running end to end tests (REST APIs)

Java Node PHP . . .

Unit and integration tests are important to verify that isolated and integrated software modules are behaving as expected. Nevertheless, end to end tests (sometimes called system tests) are also important, because they allow to test the system as a whole, as seen for the end user. End to end tests are different from unit and integration tests because the SuT is deployed in the same environment as in production.

To execute an end to end test in ElasTest, the SuT have to be declared explicitly. Then, any TJob can be associated to that SuT. In this way, the SuT can be managed independently the number of TJob executed against it.

Here we will run our REST Java Test:

REST Controller to test


@SpringBootApplication
@RestController
public class RestApp {

    @GetMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) 
    throws Exception {
        SpringApplication.run(RestApp.class, args);
    }
}

Path "/" must return "Hello World!" on a GET request

E2E test


public class RestAppTest {

    @Test
    public void rootServiceTest() {

        String appHost = System.getenv("ET_SUT_HOST");

        if(appHost == null) {
            appHost = "localhost";
        }

        RestTemplate client = new RestTemplate();

        String result = client.getForObject(
            "http://" + appHost + ":8080/",
            String.class);

        assertThat(result).isEqualTo("Hello World!");
    }

}

ET_SUT_HOST variable will be the IP of our SuT. ElasTest will automatically inject the right value (localhost if not defined)

Complete the form fields:

  • SuT Name: name of the Sut (Rest App)
  • SuT Description: description of the SuT (Rest App description)
  • With Docker Image / With Docker Compose: whether to deploy the SuT on ElasTest as a Docker image or a docker-compose (With Docker Image)
  • Docker Image: image of the SuT (elastest/demo-rest-java-test-sut)
  • Wait for http port: which port of the SuT should ElasTest wait to be available before starting the TJob (8080)


Complete the form fields:

  • TJob Name: name of the TJob
  • Select a SuT: already created SuT to be tested through to the TJob (Rest App)
  • Environment docker image: the docker image that will host your test. This docker images should contain a client to download your code from your repository hosting service. For example, if your tests are hosted in GitHub and implemented in a Maven project with Java, you need to include a git client, Maven and the Java Development Kit (JDK) in the image. Indeed, this image contains Git, Maven and Java (elastest/test-etm-alpinegitjava)
  • Commands: these are the bash commands to be executed to download the code from a repository and to execute the tests. The specific commands depends on the source code repository type and the technology. For example, the following commands will clone a Maven/Java repository from GitHub and execute the tests:
    git clone https://github.com/elastest/demo-projects
    cd demo-projects/rest-java-test
    mvn test
    

In this example, when the TJob is executed, the following steps are performed:

  1. SuT is executed starting the docker container elastest/demo-rest-java-test-sut
  2. ElasTest waits until container has a http service available in port 8080
  3. When service is ready, the test container elastest/test-etm-alpinegitjava is started and specified commands are executed on it.
  4. The test implemented in demo-projects/rest-java-test maven project is executed. This test makes a http request to a REST endpoint in the SuT and verifies that current result is the expected one.
  5. When the test container finishes, ElasTest stops the SuT and TJob containers