Test Driven Development in C#
GETTING STARTED WITH TEST DRIVEN DEVELOPMENT
Thomas Claudius Huber
SOFTWARE DEVELOPER
@thomasclaudiush www.thomasclaudiushuber.com
Module What is Test Driven Development?
Outline The Wired Brain Coffee scenario
How this course is structured
The first requirement
- Test
- Implement
- Refactor
What Is Test Driven Development (TDD)?
Requirements
1st requirement
2nd requirement
3rd requirement
What Is Test Driven Development (TDD)?
Red Green
Write a Write code to
failing test pass the test
Refactor
Tests are driving
your implementation
What Is Test Driven Development (TDD)?
TDD Cycle
Red Green
Refactor
Advantages of Test Driven Development
Red Green
Refacto
r
Think about
Think about
what the code Get fast feedback
the APIs should do
Create Write Tests are
modular code maintainable code documentation
Not so easy
to start with a test
TDD is a skill
every developer
should have
The Wired Brain Coffee Scenario
In their shop in
A company that runs
Zurich they have
several coffee shops
desks for customers
They want a web application They ask you to build the
where customers can book a business logic and parts of the
desk for a full day web application
The Planned Architecture
DeskBooker solution
DeskBooker.Core
DeskBooker.Web (.NET Core class library) DeskBooker.DataAccess
(ASP.NET Core) (Entity Framework Core)
DeskBookingRequestProcessor
How This Course Is Structured
DeskBooker solution
DeskBooker.Web.Tests DeskBooker.Core.Tests DeskBooker.
DataAccess.Tests
DeskBooker.Web DeskBooker.Core
(ASP.NET Core) (.NET Core class library) DeskBooker.DataAccess
BookDeskModel DeskBookingRequestProcessor (Entity Framework Core)
Getting Started with Test Driven Development
Testing and Implementing Business Logic
Adding Features in an ASP.NET Core App
Understand the First Requirement
DeskBooking
RequestProcessor
Understand the First Requirement
DeskBooking DeskBooking DeskBooking
Request RequestProcessor Result
FirstName FirstName
LastName LastName
BookDesk
Email Email
Date Date
Understand the First Requirement
DeskBooking DeskBooking DeskBooking
Request RequestProcessor Result
FirstName FirstName
LastName LastName
BookDesk
Email Email
Date Date
Return DeskBookingResult with request values
Demo
Implement the requirement with TDD
- Create a red unit test
- Write code to make the test green
- Refactor the code
Test and Implement the Next Requirement
DeskBooking
RequestProcessor
null BookDesk
Throw ArgumentNullException
Demo
Implement the requirement with TDD
- Test if Exception is thrown
- Throw the Exception
- Refactor the code
Test Driven Development
- Red
Summary - Green
- Refactor
Advantages of TDD
- Get fast feedback
- Write modular and maintainable code
The Wired Brain Coffee scenario
- DeskBookingRequestProcessor class