Slide 1

Slide 1 text

Fake it until you `make it` API Integration testing of embedded projects with Testcontainers and WireMock Oleg Nenashev, WireMock Inc QA&Test 2023 Bilbao

Slide 2

Slide 2 text

`make it` ?

Slide 3

Slide 3 text

Integration testing? 3 ● Multiple technology stacks and toolchains ● Many teams involved ● Sacred knowledge ● [Almost] always too late

Slide 4

Slide 4 text

Outline ● Intro to API integration testing ● Testcontainers and WireMock ● Using them together ● And examples! C/C++, Golang, Rust 4 bit.ly/qatest2023_wiremock Disclaimer: This is a talk about “WireMock AND Testcontainers”, not “VERSUS” ir “OR”. Your mileage may vary These slides:

Slide 5

Slide 5 text

> whoami @oleg_nenashev oleg-nenashev Dr. Nenashev / Mr. Jenkins Developer tools hacker Community builder & DevRel consultant #RussiansAgainstPutin #StandWithUkraine

Slide 6

Slide 6 text

@oleg_nenashev oleg-nenashev > whoami --automotive --embedded --hardware 6 CloudBees Embedded & Automotive Day in Stuttgart LibreCores CI

Slide 7

Slide 7 text

Do you already use container technologies in test automation? 7

Slide 8

Slide 8 text

8 Also: 2016 - Pini Reznik, Containers and test automation & More & More

Slide 9

Slide 9 text

Why containers? ● Configuration Management ● Fast provisioning ● Clean environments ● Disposability ● Clustering

Slide 10

Slide 10 text

Docker in Embedded Prototypes, Dev Tools Test Infrastructures, CI/CD Docker Swarm Cluster on Raspberry Pi

Slide 11

Slide 11 text

Even simulator is not always an option http://blogs.synopsys.com/breakingthethreelaws 2014/04/the-secret-ninja-fu-for-higher-performa e-prototype-operation/ HAPS S96 Guess the price… Your device prototype is here

Slide 12

Slide 12 text

Docker ● Popular container engine ● Developer-friendly ● Huge ecosystem ● DockerHub and Container Registries ● Docker Compose – multi-container apps ● Universal image format (OCI) https://www.docker.com/

Slide 13

Slide 13 text

Do your projects have APIs? 13

Slide 14

Slide 14 text

APIs are essential for Integration Testing 14 GROWTH IN WEB APIS SINCE 2005 Over 90% of developers use APIs. Skyrocket growth of APIs JANUARY 2006 JANUARY 2008 JANUARY 2010 JANUARY 2012 JANUARY 2014 JANUARY 2016 JANUARY 2018 MONTH The growth over time of the Programmable Web API API directory to more than 22,000 entries 22000 20000 18000 16000 14000 12000 10000 8000 6000 4000 2000 0 TOTAL API COUNT Programmable Web * Gartner Hype Cycle for APIs, 2022

Slide 15

Slide 15 text

APIs

Slide 16

Slide 16 text

16 Source: https://medium.com/10-minutes-qa-story/api-testing-from-scratch-api-anatomy-f220856ce9b3

Slide 17

Slide 17 text

APIs are essential for Integration and Integration Testing 17

Slide 18

Slide 18 text

Ways to do API integration testing 1. Testing against Production/Staging servers 2. Testing against a simplified/containerized instance (e.g. Testcontainers) 3. Mocking at the API provider level (e.g. WireMock) 4. Mocking at the code level 18 Slow Fast Accu- rate Nope

Slide 19

Slide 19 text

Ways to do API integration testing 1. Testing against Production/Staging servers 2. Testing against a simplified/containerized instance (e.g. Testcontainers) 3. Mocking at the API provider level (e.g. WireMock) 4. Mocking at the code level 19 Slow Fast Nope Accu- rate

Slide 20

Slide 20 text

20 Build Unit tests Publish Reports Integration tests Publish Reports WAS

Slide 21

Slide 21 text

21 Build Unit tests Publish Reports Integration tests Publish Reports NOW “Shift Left” Fast integrations tests are critical

Slide 22

Slide 22 text

22 Staging & QA Tests Build Integration tests Build Tests Gaining confidence in you software

Slide 23

Slide 23 text

23 Shifting ITs left with Testcontainers and WireMock

Slide 24

Slide 24 text

Ways to do API integration testing 1. Testing against Production/Staging servers 2. Testing against a simplified/containerized instance (e.g. Testcontainers) 3. Mocking at the API provider level (e.g. WireMock) 4. Mocking at the code level 24 Slow Fast Accu- rate Nope

Slide 25

Slide 25 text

25

Slide 26

Slide 26 text

26 SDKs C/C++ testcontainers.org

Slide 27

Slide 27 text

27

Slide 28

Slide 28 text

Example - JUnit 5 28

Slide 29

Slide 29 text

Containers are not always slow! ● On-demand image build ● Caching Docker image builds ● Suspending containers between tests ● Graceful termination ○ github.com/testcontainers/moby-ryuk 29 testcontainers.org

Slide 30

Slide 30 text

30 https://wiremock.org

Slide 31

Slide 31 text

Ways to do API integration testing 1. Testing against Production/Staging servers 2. Testing against a simplified/containerized instance (e.g. Testcontainers) 3. Mocking at the API provider level (e.g. WireMock) 4. Mocking at the code level 31 Slow Fast Accu- rate Nope

Slide 32

Slide 32 text

32 WireMock 101 WireMock is a tool for building mock APIs Created in 2011 as a Java test library and CLI Available beyond Java, inc Rust or Golang You can can: ● Create stable development environments ● Isolate yourself from flakey 3rd parties ● Simulate APIs that don't exist yet wiremock.org

Slide 33

Slide 33 text

33 wiremock.org/docs/stubbing IF (request_url) THEN (response) * * it gets MUCH more complex WireMock Config JSON: Client library response request Client App Mock API Server HTTP/2

Slide 34

Slide 34 text

Breaking down JSON files Using WireMock Cloud app.wiremock.cloud 34

Slide 35

Slide 35 text

WireMock in Java 35 wiremock.org/docs/stubbing

Slide 36

Slide 36 text

WireMock Ecosystem & Features 36 wiremock.org/docs

Slide 37

Slide 37 text

WireMock Ecosystem & Features 37 wiremock.org/docs

Slide 38

Slide 38 text

Example - WireMock for Rust 38 docs.rs/wiremock/latest/wiremock LukeMathWalker/wiremock-rs Note: WireMock for Rust is maintained outside the WireMock GitHub organization

Slide 39

Slide 39 text

Comparison [1/2] 1. Testing against Production/Staging servers 2. Testing against a simplified/containerized instance (e.g. Testcontainers) 3. Mocking at the API provider level (e.g. WireMock) 4. Mocking at the code level 39 Slow Fast Nope Accu- rate

Slide 40

Slide 40 text

40

Slide 41

Slide 41 text

41 WireMock has an official Testcontainers module! testcontainers.com/modules/wiremock

Slide 42

Slide 42 text

Example - JUnit 5 42

Slide 43

Slide 43 text

WireMock as a Proxy 43 Tests Real API Provider OR ● Fault injection ● Recording ● Protocol Verification

Slide 44

Slide 44 text

44 How about embedded?

Slide 45

Slide 45 text

IEEE Spectrum Research among members 45 https://spectrum.ieee.org/top-progra mming-languages-2021

Slide 46

Slide 46 text

46 jetbrains.com/lp/ devecosystem-2021/embedded/

Slide 47

Slide 47 text

47 Supported by WireMock and Testcontainers C/C++ Shared Lib Interface

Slide 48

Slide 48 text

Testcontainers for C/C++ 48 Started by the WireMock Team in September 2023 We have Embedded/Automotive customers and users who need it Based on Testcontainers or Go WireMock module github.com/testcontainers/testcontainers-c

Slide 49

Slide 49 text

Example - WireMock with C code 49 github.com/testcontainers/testcontain ers-c/tree/main/examples/wiremock ● Testcontainers C is an experimental project ● Feedback is welcome! ● See more examples in the repository: CppUnit, OpenSCADA, etc.

Slide 50

Slide 50 text

50 WireMock for Google Test / C++ github.com/testcontainers/te stcontainers-c/tree/main/de mo/google-test

Slide 51

Slide 51 text

And then a test! 51 github.com/testcontainers/testcontainers-c/tree/main/demo/google-test

Slide 52

Slide 52 text

52 Demo time! Integration Testing With WireMock

Slide 53

Slide 53 text

Takeaways 53 ● Shift left your integration testing ● Mocking APIs and services, reducing external dependencies, containers ● There are tools for that, including WireMock and Testcontainers ● WireMock and Testcontainers co-exist well ● They work for embedded use-cases!

Slide 54

Slide 54 text

Choosing a right tool * ** * Your mileage may vary ** You will often need both and that’s OK! Need database or complex dynamic environment? Yes Server implementation is ready? Is it containerized and lightweight? Can you [easily] transfer the system to the required state in test? No No No No Yes Yes Yes 54

Slide 55

Slide 55 text

Get Started wiremock.org/docs/solutions/embedded 55

Slide 56

Slide 56 text

THANK YOU Contacts: E-mail: [email protected] GitHub: oleg-nenashev Twitter: @oleg_nenashev QUESTIONS? 56

Slide 57

Slide 57 text

WireMock Cloud by WireMock Inc. 57 ● Small startup ● WireMock creator is a co-founder of the Inc. ● WireMock Cloud - SaaS for end-to-end API mocking ● Private beta: K8s Edition for managed / on-premises wiremock.io

Slide 58

Slide 58 text

Credits to ● All WireMock contributors ● WireMock Inc. Team ● All Testcontainers contributors and AtomicJar folks ● All FOSS contributors 58