applying t est driven development tdd with integration t
play

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


  1. Applying T est Driven Development (TDD) with Integration T ests Steve Gordon MICROSOFT DEVELOPER TECHNOLOGIES MVP @stevejgordon www.stevejgordon.co.uk

  2. t h s Test Driven Development (TDD) Overview Applying TDD with integration tests Defining external boundaries Creating fakes of external services Replacing services with fakes by customizing the test client

  3. Test Driven Development

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

  5. Test Driven Development Tests come before implementation code Test coverage aligned to requirements Simplified implementation code Promotes refactoring to clean code

  6. Test Driven Development Cycle Refactor Red Green

  7. TDD Paradigm Mental shift from retrospective Requires perseverance to become testing productive

  8. t h s Demo Apply test driven development - Write a failing test (red) - Make the test pass (green) - Refactor the code

  9. Add GET endpoint /api/stock/total Return JSON (application/json) response Total is count of all stock of all products { "stockItemTotal": 100 }

  10. t h s Demo Continue development with TDD - Test and implement a final requirement Define a boundary for integration tests Create a fake of a dependency

  11. High-level Architecture SDK

  12. IDatabaseClient<T> ICloudDatabase Code Dependency Chain IProductDataRepository StockController

  13. IDatabaseClient<T> ICloudDatabase Code Dependency Chain IProductDataRepository StockController

  14. IDatabaseClient<T> ICloudDatabase Code Dependency Chain IProductDataRepository StockController

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

  16. t h s Demo Customize the test client using WithWebHostBuilder - Register a fake service

  17. Service Registration Order ConfigureTestServices ConfigureServices Startup.cs StockControllerTests.cs IServic ISe IServic IServic ISe ISe IServic ISe iceColle iceColle iceColle iceColle llectio llectio llectio llectio ion ion ion ion ICloudDatabase -> CloudDatabase ICloudDatabase -> CloudDatabase ICloudDatabase -> CloudDatabase ISomeOtherService -> SomeOtherService ISomeOtherService -> SomeOtherService ICloudDatabase -> FakeCloudDatabase

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

  19. t h s Learned about Test Driven Development Applied the TDD cycle Summary - 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

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

Recommend


More recommend