Testing Strategies for OpenTelemetry Demo
Overview
The OpenTelemetry Demo project utilizes various testing strategies to ensure the reliability and functionality of its components. These testing strategies include unit testing, integration testing, and end-to-end testing.
Unit Testing
Unit testing is a software testing methodology that focuses on testing individual components or units of the codebase in isolation. In the context of the OpenTelemetry Demo, unit tests are used to verify the correctness and consistency of individual components, such as tracing and metrics.
Options and Examples
The OpenTelemetry Demo uses the JUnit testing framework for unit testing. Here’s an example of a unit test for the OpenTelemetry Java agent:
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.trace.TraceInstrumentor;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.testing.TestOpenTelemetry;
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class OpenTelemetryAgentTest {
private OpenTelemetry openTelemetry;
private Tracer tracer;
@BeforeEach
public void setup() {
OpenTelemetrySdk sdk = new OpenTelemetrySdk.Builder().build();
OpenTelemetry openTelemetry = sdk.getOpenTelemetry();
this.tracer = openTelemetry.getTracer("example.service");
}
@Test
public void testOpenTelemetryAgent() {
Instrumenter<TraceInstrumentor> traceInstrumenter = new TraceInstrumentor.Builder()
.addScope(this.tracer.scopeManagers().get("main"))
.build();
OpenTelemetryAssertions.assertThat(traceInstrumenter)
.extracts("example.service")
.hasNoSpans();
}
}
Learn more about JUnit testing
Integration Testing
Integration testing is a software testing methodology that focuses on testing the interactions between different components or units of the codebase. In the context of the OpenTelemetry Demo, integration tests are used to verify the correctness and consistency of the interactions between different components, such as the OpenTelemetry Java agent and the OpenTelemetry Collector.
Options and Examples
The OpenTelemetry Demo uses the Testcontainers library for integration testing. Here’s an example of an integration test for the OpenTelemetry Java agent and the OpenTelemetry Collector:
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.trace.TraceInstrumentor;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.testing.TestOpenTelemetry;
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.DockerComposeContainer;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.TestContainer;
public class OpenTelemetryIntegrationTest {
private static OpenTelemetry openTelemetry;
private static Tracer tracer;
private static DockerComposeContainer dockerComposeContainer;
private static GenericContainer openTelemetryCollectorContainer;
@TestContainer
public static DockerComposeContainer dockerCompose() {
return new DockerComposeContainer("docker-compose.yml")
.withExposedPorts(4317);
}
@Container
public static GenericContainer openTelemetryCollector() {
return new GenericContainer("quay.io/open_telemetry/opentelemetry-collector:v0.23.0")
.withExposedPorts(4317);
}
@BeforeAll
public static void setup() {
OpenTelemetrySdk sdk = new OpenTelemetrySdk.Builder().build();
openTelemetry = sdk.getOpenTelemetry();
tracer = openTelemetry.getTracer("example.service");
dockerComposeContainer = new DockerComposeContainer("docker-compose.yml");
openTelemetryCollectorContainer = new GenericContainer("quay.io/open_telemetry/opentelemetry-collector:v0.23.0");
dockerComposeContainer.start();
openTelemetryCollectorContainer.start();
}
@Test
public void testOpenTelemetryIntegration() {
Instrumenter<TraceInstrumentor> traceInstrumenter = new TraceInstrumentor.Builder()
.addScope(tracer.scopeManagers().get("main"))
.build();
OpenTelemetryAssertions.assertThat(traceInstrumenter)
.extracts("example.service")
.hasNoSpans();
}
}
Learn more about Testcontainers
End-to-End Testing
End-to-end testing is a software testing methodology that focuses on testing the entire system, from the user interface to the backend services, to ensure that all components are working together correctly. In the context of the OpenTelemetry Demo, end-to-end tests are used to verify the functionality and performance of the entire system, including the OpenTelemetry Java agent, the OpenTelemetry Collector, and the backend services.
Options and Examples
The OpenTelemetry Demo uses the Selenide library for end-to-end testing. Here’s an example of an end-to-end test for the OpenTelemetry Demo application:
import static com.codeborne.selenide.Condition.*;
import static com.codeborne.selenide.Selectors.*;
import static com.codeborne.selenide.Selenide.*;
public class OpenTelemetryEndToEndTest {
@Test
public void testOpenTelemetryDemo() {
open("http://localhost:8080");
$(".button").click();
$(byText("Submit")).click();
$(byText("Success")).shouldBe(visible);
}
}
References
This documentation page covers the testing strategies used in the OpenTelemetry Demo project, including unit testing, integration testing, and end-to-end testing. The project utilizes the JUnit testing framework for unit testing, Testcontainers for integration testing, and Selenide for end-to-end testing. Examples of tests for each strategy are provided, along with links to the relevant OpenTelemetry components and testing libraries.