Chapter 20. Testing your application

This chapter covers

  • Creating unit test projects with xUnit
  • Writing unit tests for custom middleware and MVC controllers
  • Using the Test Host package to write integration tests
  • Testing code dependent on EF Core with the in-memory database provider

When I first started programming, I didn’t understand the benefits of automated testing. It involved writing so much more code—wouldn’t it be more productive to be working on new features instead? It was only when my projects started getting bigger that I appreciated the advantages. Instead of having to manually run my app and test each scenario, I could press Play on a suite of tests and have my code tested for me automatically.

Testing is universally accepted as good practice, but how it fits into your development process can often turn into a religious debate. How many tests do you need? Is anything less than 100% coverage of your code base adequate? Should you write tests before, during, or after the main code?

20.1. An introduction to testing in ASP.NET Core

20.2. Unit testing with xUnit

20.3. Unit testing custom middleware

20.4. Unit testing MVC controllers

20.5. Integration testing: testing your whole app with a Test Host

20.6. Isolating the database with an in-memory EF Core provider