Unit testing in .NET and .NET 5
Every developer want clean code that works. So how does one get to maintainable and working code? And
moreover: keep it working.
- What every developer wants: Quality code that works; and keeps on working
- Finding bugs: not just in code. - Or how NASA lost a 125.000.000$ Mars orbiter
- What is unit testing? And what is a good unit test?
- Understanding the difference between a unit test and an integration test
- Test Driven Development - Should you be doing it?
- Role-playing: Marge, Homer and Bart
- The Triple-A of unit testing - and what has cooking to do with this?
Unit testing with Visual Studio with MSTest and XUnit
When Unit Testing, you will have to decide on frameworks. There are several ones out there, which one do I need?
Visual Studio comes with a built in framework called MSTest, but you also have others, like the popular XUnit.
In this module, we will discuss pros and cons of these frameworks, and go into some best practices.
How do I know if I have tested enough? With code coverage you can see which paths in your code you have tested,
and easily discover untested conditional logic.
- MSUnit - Built-in into Visual Studio
- Building and running Unit Tests with MSUnit
- Using the Test Explorer Window
- Using Test Settings
- Live unit testing with Visual Studio
- Unit Testing best practices with XUnit
- Facts and Theories
- Unit testing parallelism
- Measuring and improving Code Coverage
Writing testable code
If you want your code to be testable, you have to engineer your code for testing. You will learn about Stubs and
Mocks, and how to replace dependencies that might interfere with testing.
- Testing dependencies - and the art of writing testable code
- The difference between a Stub and a Mock
- How to replace dependencies with stub and mock objects
- The Extract and Override pattern
- S.O.L.I.D. Priciples
- Dependency injection
- How the MVVM pattern facilitates UI testing
- Replacing configuration in tests
- Testing database code
Unit Testing ASP.NET Core Web Applications
When creating modern web applications with ASP.NET MVC, it's not that straightforward to unit test the Controllers
and Views. How does one validate wether the ActionResult returned by the Controller action is the one you wanted, as
well as whether the View is correct.
- Challenges when unit testing MVC applications
- Writing unit test for your MVC controllers
- Unit testing your views
- Unit testing ApiControllers
Isolation Frameworks: MOQ and Microsoft Fakes.
Building Stubs and Mocks can be a lot of work without an Isolation Framework. MOQ is an isolation framework that
allows you to easily build the Fake objects you need for building great
And how do you test 'untestable' code? Code where dependencies have been glued into the class you want to test? Then
need some magic: Microsoft Fakes. This product allows you to replace any class's implementation, making it easier to
test legacy code and other difficult to test code.
- Understanding Isolation Frameworks
- Building Stubs and Mocks with MOQ
- Checking arguments and return values
- Using Linq to Mocks
- What makes Microsoft Fakes so special
- Testing legacy/untestable code
- Building Stubs and Mocks with Fakes
Automatic Regression Testing
So your software works! Great! But how do you know if it will work next week, or next month?
Did you break something adding some new cool feature? Test everyting again? Of course, but
now we will make a machine do it automatically every time a team member makes a change.
- What is a regression?
- Using an automated build system
- Continuous integration
- Running integration tests
- Automatic regression tests
Writing solid, maintainable code that works. This is what every developer wants. This guides you into the art of unit
testing, where you learn to build testable code and various techniques to give your code a good spin. With unit
testing, comes unit testing frameworks. We'll discuss how unit testing frameworks work and how they help you.
Frameworks that will be considered are MSTest and XUnit. Of course we will also observe the need for Stub
and Mock objects. Once you have your unit tests ready you will want to run them automatically,
for example to do regression testing, using Continous Integration. At the end of this course you will go
home with a solid and practical understanding of unit testing and how to apply this in real life. U2U is known for
its real-life approuch to training, so each chapter is accompanied with lots of hands-on labs.
This course is intended for experienced programmers who are very familiar with VB.NET or C# and have a working
experience with the .NET (Framework or .NET 5).