Unit Testing 101: From Zero to Hero30 Aug 2021 #tutorial #csharp
Do you want to start writing unit tests? But, you don’t know where to start? Do you want to adopt unit testing in your team? I can help you.
If you’re a beginner or a seasoned developer new to unit testing, this is the place for you.
Write your first unit tests with MSTest It’s the starting point to write unit tests. No prerequisites needed.
When writing your unit tests, make sure you don’t duplicate logic in Asserts. That’s THE most common mistake on unit testing.
How to write good unit tests shows two common issues when writing unit tests: complex setup scenarios and hidden test values.
Make sure to always write a failing test first.
Read how to create fakes with Moq, an easy to use mocking library.
If you find yourself using lots of fakes, take advantage of automocking with TypeBuilder and AutoFixture to write simpler tests.
Last but not least, read all tips of this series on Unit Testing Best Practices. As an example, see how to refactor a real-world test to follow some of those best practices. Deep into assertions, check how to write better assertions and how to write custom assertions.
If you want to practice writing some unit tests, check my Unit Testing 101 repository over on GitHub.