Unit testing in .NET
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
Finding bugs: not just in code. - Or how NASA lost a 125.000.000$ Mars
- 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
- Fluent Assertions
- Measuring and improving Code Coverage
- LAB: Implementing a library with testing
More loose coupling using dependency injection
When writing code, you have to make sure you are not introducing
unnecessary dependencies on other objects. Dependencies will just cost you
on the long run since they are harder to maintain, not easily testable,
exchangeable, ... . If you need to depend on another, then depend on the
interface. Interfaces describe the dependency, but do not force it. Now,
you don't want those dependencies to be filled hard coded and this is
where dependency injection (DI) can help you. Dependency injection can
help you at runtime dynamically deciding which implementations to plug
into the dependencies.
- What is tight coupling and how to prevent it?
- Using Inversion of Control (IoC) containers.
- Constructor and Property injection.
- Microsoft.Extensions.DependencyInjection as an example.
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 Fake, a Stub and a Mock
- How to replace dependencies with stub and mock objects
- The Extract and Override pattern
- How the MVVM pattern facilitates UI testing
- Replacing configuration in tests
- LAB: Use Extract and Override to test legacy code
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 tests. And how do you test
'untestable' code? Code where dependencies have been glued into the class
you want to test? Then you 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
- LAB: Using MOQ
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
- Using the ASP.NET Core testing framework
- End-to-end testing
- LAB: Implementing integration and end-to-end tests
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
- Automatically running your regression tests using GitHub Actions
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 Fake, 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 .NET.