Test Driven Development is a technique that will help you craft better software. When I started out I didn't really get TDD. I'd write a handful of unit tests on a project, those tests we're all a bit useless and I never saw the benefit. If you're in the same camp then this section will help explain the benefits of unit testing. In this section, you will learn about the frameworks I use, how to write tests for hard to test code and how to structure your tests so they are not so brittle. My approach to TDD maybe slightly more pragmatic. I admit in most situations I don't start off writing a test first. When I'm designing something I prefer to work in iterations. My initial iteration might be to get something working (usually within a few hours max). After I have a better understanding of the project, I'll start to refactor it into a better design. It's this phase that I'll start to write my tests. Using tools like dotCover, or, statistical analysis tools, like sonrQube . I use software to help iterate my design until I' happy with the code coverage. This approach is probably more pragmatic than some of the purist TDD material you may bump into. The main reason why I think TDD is so beneficial is that it forces you to write better code. If a method isn't testable, you are forced to decouple it, so that it is. It's this process of forcing you to re-design things regularly that will help you write better code. Having high coverage does mean that you cab higher confidence when releasing your new code to your production environment but for me, this is more of an additional benefit, rather than the main star. If all this sounds good to you, I recommend you get going today.
Writing clean code is an art form. If you care about software development and being a good coder, your number one aim should be to write good code. Clean code means different things for different people. In this section, I will cover some of my tips for writing code that I think is clean
Nunit is the most widely used testing framework with .NET. It's free, easy to install and easy to write tests with. In this section, you will how to install NUnit and how to start writing tests with it,
When you start writing tests, applying SOLID principles and dependency injection, you'll need to use a mocking framework in order to write your tests. Moq, the best free mocking framework for .NET. In this section, I'll cover everything you need to get up and running with Moq.
Unit tests help document your code base. If you ensure you apply a good naming strategy, people who work on the project in the future should be able to look at the tests around a module and understand what it does, how they can use the module themselves and any limitations within the code. If you write hard to understand test this won't be possible. Fluent Assertions is a Nuget package, that you can learn easily within less than 30 minutes that will make your tests easier to read. In this section, you will learn how to install it and how to write your tests using it.
Autofixture is another Nuget package that will help you to write better tests. After you start writing tests you will notice, very quickly that you have to write a lot of set-up code. The structure of most tests will be to initialise some input data, pass that data into a method and then test the value returned by the method does what it wants you to. Autofixture can help automate the set-up data code. Instead of manually having to write this set-up code, Autofixture will do it automatically for you. In this section, you will learn how to use Autofixture on your project.
Getting good test coverage on some methods can be hard, especially legacy code, or frameworks supplied by third-parties. This section covers a number of code snippets that will allow you to test some popular third-party .NET Nuget packages.
Unit tests are not the only way you can test your code. You can also write integration tests and acceptance tests that will test your website rather than your C# code. Selenium is another tool you can use to help ensure your code does what it meant to. In a lot of companies, the QA person might write all your Selenium tests, however, its also a good idea if the developers can also write some tests, as Selenium tests can be written within .NET. In this section, you will learn about Selenium.