Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON...

37
Cloud-native apps REST API testing Kirill Shepitko Konstantins Tarasjuks

Transcript of Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON...

Page 1: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Cloud-native apps REST API testing

Kirill Shepitko

Konstantins Tarasjuks

Page 2: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Who We Are & What We Do

neotech.lv www.neotech.lvneotechlv

neotechlv

Page 3: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

How We Do It

Page 4: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

How We Deliver It

Page 5: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

•Relies on IaaS

• Scalable, resilient, self-healing, stateless

•Containerized

What is a cloud-native

app?

«Cloud native is an approach to building andrunning applications that fully exploit theadvantages of the cloud computing model.»Source: What are Cloud-Native Applications? - Pivotal

Page 6: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Test pyramid

Unit tests

Integration tests

Acceptance tests

UI tests

5%

5%

10%

80%

• UI – 5%

• Acceptance – 5%

• Integration – 10%

• Unit – 80%

Page 7: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Our test pyramid

Unit tests

Integration tests

Acceptance tests

UI tests

0%

0%

20%

80%

• UI – 0%

• Acceptance – 0%

• Integration – 20%

• Unit – 80%

Page 8: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Problems we hadContract broken

Deployment is broken

Page 9: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

What we needed• REST API acceptance tests

• Tests should test REST contracts

• Tests should be fast

• Should be able to mimic correspondent systems

• Should be able to detect the deployment problems

• Should be lightweight enough to run tests on dev machines

• Should have nice reporting

Page 10: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

REST API contract testing

Page 11: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Existing Frameworks’ Issues

Great for a “Hello world” app

Lots of very similar contracts (maintain it!)

Tests are generated

Lot of code to write

Page 12: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

How Spring Cloud Contract Works

1. Write contract

2. Verify contract from producer side (automated)

3. Publish service stubs to artifactory(there’s a plugin)

4. Write a test on client side, using stubs

Page 13: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Writing Spring Cloud

Contract

• Groovy

• YAML

• Pact JSON

• Spring REST docs

Page 14: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Spring Cloud Contract for

Polyglots

Page 15: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

The SolutionDescribe

REST endpoints

Create scenario (pact)

Author test steps

Run tests

Page 16: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Imagine a sample

app…

Page 17: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

How we test

REST API

Page 18: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Before Tests

Manually start up the app ecosystem

1

Pull Swagger specs

2

Save Swagger specs to tests

Git repo

3

Page 19: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Test Flow

Generate REST clients

1

Run tests• Launch ecosystem

via TestContainers

• Launch mocks

• Run scenarios

2

Generate reports

3

Page 20: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Benefits

• Scenario is a contract

• WireMock is useful for mocks and trouble emulation

• Test steps and mocks can be reused

• Easy to bring up the whole app ecosystem

• REST client generation approach is polyglot

• Great reports

Page 21: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Lessons Learned• QA needs a faster solution

• REST client may be stale

• Supporting different test profiles

• Regenerating REST clients on the fly

Page 22: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Updated Test Flow

Start up the app ecosystem and

download Swagger JSON

1

Generate REST clients (Git-

ignored)

2

Run tests

• Launch ecosystem again

• Launch mocks• Run scenarios

3

Generate reports

4

Page 23: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Docker-compose Is a God

Local development

Tests

REST client generation

Page 24: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Problems

• No clear boundary between contract and acceptance testing

• TestContainers bind the solution to JUnit 4

• Springfox (generates Swagger spec) is quite limited

Page 25: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Swagger

Page 26: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Scenario

Page 27: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Generated REST client in use

Page 28: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Service mock

Page 29: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Allure report: overview

Page 30: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Allure report: graphs

Page 31: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Allure report: behaviours

Page 32: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Demo

Page 33: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints
Page 34: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Real performance

Page 35: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Our framework is open-sourcehttps://github.com/neotechlv

Page 36: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints
Page 37: Cloud-native apps REST API testing · Spring Cloud Contract •Groovy •YAML •Pact JSON •Spring REST docs. Spring Cloud Contract for Polyglots. The Solution Describe REST endpoints

Thank you for listening!