Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Integration and end-to-end testing with TestСon...

Integration and end-to-end testing with TestСontainers

Nikolay Kuznetsov

October 31, 2019
Tweet

More Decks by Nikolay Kuznetsov

Other Decks in Programming

Transcript

  1. About me • Go developer at Zalando Helsinki • Java

    developer at Infobip, DevExperts • C developer at Samsung, Motorola
  2. Integration testing evolution • In-memory mocking • Local DBs •

    Vagrant • Docker, Docker Compose • Docker API
  3. Start Docker containers for integration test • Shell scripts •

    Maven plugin • Docker Compose • Docker API • MiniKube, Kubernetes
  4. TestContainers • github.com/testcontainers/testcontainers-java • Wraps docker-java library • Docker environment

    discovery • Host port randomization • Containers clean up on JVM shutdown • Readiness waiting strategies
  5. Host port randomization • To prevent port conflicts • Enables

    parallel builds • API to get a host port
  6. Host port waiting strategy • Default: at first exposed port

    with timeout of 60s • Implementation checks both from outside and inside container
  7. Demo recap • JUnit 5 integration via Extension API @TestContainers

    / @Container annotations • Modules ◦ wrappers on top of GenericContainer ◦ setters translated to containers environment variables
  8. JUnit 5 extensions • Test execution lifecycle phases = extension

    points • Extension logic ◦ Implement interface(s) from o.j.j.api.extension.* ◦ Register with @ExtendsWith annotation
  9. Modules • 14 database modules • MockServer • LocalStack =

    mocked AWS by Atlassian • Kafka, Pulsar, RabbitMQ • Toxiproxy
  10. Why end-to-end testing? • Test business flows across multiple services

    • Regression when ◦ a new service introduced ◦ a legacy service removed
  11. Test cluster for automated E2E tests • Unexpected versions of

    dependencies ◦ Teammates deploy from a development branch ◦ Pinging other teams to stabilize their test environment • Unexpected database states ◦ Care to clear data generated by a test run?
  12. On-demand cluster for E2E tests • Locally and at CI

    machines • Needs time to spin up a new cluster • Resources: memory and CPU • How actually to do it?
  13. Non-Kubernetes for E2E tests • YAGNI = You aren’t gonna

    need it (Kubernetes for end-to-end tests) • Unless, you really want to test specific Kubernetes features or manifests • So, start services in containers within the same Docker network! (with TestContainers)
  14. E2E approach (2) • An extension implements BeforeAllCallback interface •

    Starts a corresponding container via TestContainers API • Configures it, provides a shared a network • Perform tests by calling endpoints via host ports
  15. Hints • Host port forwarding Testcontainers.exposeHostPorts() • Fixed host port

    for remote debug GenericContainer.addFixedExposedPort() •
  16. Takeaways • https://testcontainers.org • Balance between flexibility, speed and features

    • Works on Mac, Linux, Windows • Great for integration tests • Possible to use for end-to-end tests