TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs...

17
@michikuehne TDD for APIs in a Microservice World Michael Kuehne-Schlinkert Head of Development @ Cybus

Transcript of TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs...

Page 1: TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs Platform Summit 2016, Stockholm

@michikuehne

TDD for APIs in a Microservice World

Michael Kuehne-Schlinkert Head of Development @ Cybus

Page 2: TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs Platform Summit 2016, Stockholm

@michikuehne

Cybus brings untapped Data Sources to a secure API

Access Control

Pre-Processing & API

Shopfloor Protocols

Security Monitoring Maintenance Efficiency Sovereignty

Page 3: TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs Platform Summit 2016, Stockholm

@michikuehne

Our Challenges

  Security, Reliability & Scalability è Service Level Agreements

Better Representation of Customer Needs through Tests & Codes

Loosly coupled but highly connected Microservices

Efficient TDD with limited resources

Page 4: TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs Platform Summit 2016, Stockholm

@michikuehne

„How to validate that our API is working as intended?“

by Steve Klabnik

Page 5: TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs Platform Summit 2016, Stockholm

@michikuehne

„How to efficiently validate that our microservice ecosystem is working as

intended?“ by Michael Kuehne-Schlinkert

Page 6: TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs Platform Summit 2016, Stockholm

@michikuehne

Simplified Ecosystem

Device Service

Data {...}

Data {...}

Data {...}

UI Service

Auth Service

Page 7: TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs Platform Summit 2016, Stockholm

@michikuehne

Where to start?

Story

  As a <role>,

__.I want <feature>

__.so that <reason>.

Example

__.As an admin of the cybus middleware

__.I want to control the read access to my devices

__.so that users can only read the data they are allowed to

Scenario

  GIVEN <context>

__.WHEN <event>

__.THEN <outcome>

Example

__. GIVEN External Device

__. GIVEN Device Service

__. GIVEN Auth Service

__. WHEN External Device provides new data

__. WHEN Read Access to Device is granted

__. THEN Device Service reads data from external device

Page 8: TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs Platform Summit 2016, Stockholm

@michikuehne

Test Scenario

Device Service

Data {...}

Data {...}

Data {...}

UI Service

Auth Service

External Device

Data {...}

Stubbed

Page 9: TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs Platform Summit 2016, Stockholm

@michikuehne

Acceptance Tests

Platform Tests

  Turn Scenarios into Tests

  Focus on whole Microservice Ecosystem

Verify Functionality of Ecosystem

External / Third-Party Services are stubbed/mocked

  Black Box Testing

  Entry Point of Platform Test depends on Scenario   API

  GUI

Contract Tests

  Turn API Specifications into Tests

  Focus on only One Mircroservice

Verify the Interface (Contract)

  Black Box Testing

Page 10: TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs Platform Summit 2016, Stockholm

@michikuehne

Integration Tests

A.k.a. Component Tests & Service Tests

  Focus on only One Microservice

Verify Functionality of One Service

  Break down Scenarios

  End-to-End Tests – but not to end of ecosystem

  Internal Services are stubbed/mocked

  Gray Box Testing (Black & White)

Resources

Domain

ORM

Exte

rnal

Cl

ient

Stubbed Service

Ext. DB Stb.

Test Client

Simplified Microservice Architecture

Page 11: TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs Platform Summit 2016, Stockholm

@michikuehne

Functional Tests

Controller

Industrial Protocol

Device Handler

  Focus on 2+ Classes/Modules of Domain

Verify Interaction between Modules

  White Box Testing

  Internal Dependencies are stubbed/mocked

Example Domain of Microservice

Stubbed

Page 12: TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs Platform Summit 2016, Stockholm

@michikuehne

Unit Tests

Controller

Industrial Protocol

Device Handler

  Focus on One Unit (Class/Module) of Domain

Verify Functionality of One Unit

  Internal Dependencies are stubbed/mocked

  White Box Testing

Example Domain of Microservice

Stubbed

Page 13: TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs Platform Summit 2016, Stockholm

@michikuehne

Our TDD Cycle

Acceptance Tests

Integration Tests

Functional Tests

Unit Tests Code

Black Box Gray Box White Box

Page 14: TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs Platform Summit 2016, Stockholm

@michikuehne

Our TDD Cycle with aspired Test Coverage

Acceptance Tests

Integration Tests

Functional Tests

Unit Tests Code

tcU à 100%

tcI à 100% tcF à 100% - tcI

tcI + tcF à 100%

tcA à x%

tc = test coverage

x –

Page 15: TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs Platform Summit 2016, Stockholm

@michikuehne

Lessons Learned

It works! We overcame Testing Hell

Less Test Code and less Fragile Tests but high test coverage

  Quality of Story Definitions increased with mandatory Scenarios

Efficency comes with the right Tools

Each Microservice must provide Test Library (e.g. Stubs)

Deterministic Environments gained Complexity

Asynchronous Services require Gray to White Box Testing

Functional Tests became our Plan B

Page 16: TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs Platform Summit 2016, Stockholm

@michikuehne

cybus.io @cybus_io [email protected]

Pssst! We‘re hiring!

Michael Kuehne-Schlinkert @michikuehne [email protected]

Thx! ...and happy testing!

Page 17: TDD for APIs in a Microservice World (Short Version) by Michael Kuehne-Schlinkert @ NordicAPIs Platform Summit 2016, Stockholm

@michikuehne

Appendix

  Icons by Gregor Cresnar from www.flaticon.com is licensed by CC 3.0 BY

  Icons by Freepik from www.flaticon.com is licensed by CC 3.0 BY