Applying T est Driven Development (TDD) with Integration T ests - - PowerPoint PPT Presentation

applying t est driven development tdd with integration t
SMART_READER_LITE
LIVE PREVIEW

Applying T est Driven Development (TDD) with Integration T ests - - PowerPoint PPT Presentation

Applying T est Driven Development (TDD) with Integration T ests Steve Gordon MICROSOFT DEVELOPER TECHNOLOGIES MVP @stevejgordon www.stevejgordon.co.uk t h s Test Driven Development (TDD) Overview Applying TDD with integration tests


slide-1
SLIDE 1

@stevejgordon www.stevejgordon.co.uk

MICROSOFT DEVELOPER TECHNOLOGIES MVP

Steve Gordon

Applying T est Driven Development (TDD) with Integration T ests

slide-2
SLIDE 2

t h s

Test Driven Development (TDD) Applying TDD with integration tests Defining external boundaries Creating fakes of external services Replacing services with fakes by customizing the test client

Overview

slide-3
SLIDE 3

Test Driven Development

slide-4
SLIDE 4

Retrospective Tests

Tests written after implementation code Possible to miss test cases Possible to assert on incorrect output

slide-5
SLIDE 5

Test Driven Development

Tests come before implementation code Test coverage aligned to requirements Simplified implementation code Promotes refactoring to clean code

slide-6
SLIDE 6

Red Green Refactor

Test Driven Development Cycle

slide-7
SLIDE 7

Mental shift from retrospective testing Requires perseverance to become productive

TDD Paradigm

slide-8
SLIDE 8
slide-9
SLIDE 9

Demo

t h s

Apply test driven development

  • Write a failing test (red)
  • Make the test pass (green)
  • Refactor the code
slide-10
SLIDE 10

Add GET endpoint /api/stock/total Return JSON (application/json) response Total is count of all stock of all products

{ "stockItemTotal": 100 }

slide-11
SLIDE 11

Demo

t h s

Continue development with TDD

  • Test and implement a final requirement

Define a boundary for integration tests Create a fake of a dependency

slide-12
SLIDE 12

High-level Architecture

SDK

slide-13
SLIDE 13

Code Dependency Chain

IDatabaseClient<T> ICloudDatabase IProductDataRepository StockController

slide-14
SLIDE 14

Code Dependency Chain

IDatabaseClient<T> ICloudDatabase IProductDataRepository StockController

slide-15
SLIDE 15

Code Dependency Chain

IDatabaseClient<T> ICloudDatabase IProductDataRepository StockController

slide-16
SLIDE 16

Advantages

Integration tests run fully in memory Can be run often during development Easy to run during continuous integration

slide-17
SLIDE 17

Demo

t h s

Customize the test client using WithWebHostBuilder

  • Register a fake service
slide-18
SLIDE 18

ISe IServic iceColle llectio ion ISe IServic iceColle llectio ion ICloudDatabase -> CloudDatabase

Service Registration Order

ISe IServic iceColle llectio ion ICloudDatabase -> CloudDatabase ISomeOtherService -> SomeOtherService ISe IServic iceColle llectio ion ICloudDatabase -> CloudDatabase ISomeOtherService -> SomeOtherService ICloudDatabase -> FakeCloudDatabase Startup.cs

ConfigureServices

StockControllerTests.cs

ConfigureTestServices

slide-19
SLIDE 19

var client = _factory.WithWebHostBuilder(builder => { builder.ConfigureTestServices(services => { services.RemoveAll<IFoo>(); services.AddSingleton<IFoo,FakeFoo>(); }); }).CreateClient();

Managing Registered Services

slide-20
SLIDE 20
slide-21
SLIDE 21

t h s

Learned about Test Driven Development Applied the TDD cycle

  • Wrote a failing test
  • Implemented with minimal code
  • Confirmed the test passed
  • Refactored where necessary

Defined external boundary for testing Created a fake for a service Customized the test client to use the fake

Summary

slide-22
SLIDE 22

Up Next: Writing Integration Tests for ASP.NET Core Web APIs: Part 2