👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Introducing TDD in C# .Net

Introducing TDD in C# .Net

TDD

2 Articles

Improve

In this article, let's learn about (TDD) Test Driven Development using C# .NET.

Table of Contents

  1. What is Test Driven Development?
  2. Basics of Test-Driven Development
  3. Advantages
  4. Disadvantages
  5. Summary

What is Test Driven Development?

TDD is a very powerful approach to build robust software. Before we develop the feature, we will write a unit test for the feature which means the unit test will drive our feature development.

Basics of Test-Driven Development

Let's see what is test-driven Development and explain to you the project scenario.

Test-Driven Development, or TDD for short, is a method used to write tests before we start our implementation. Before you start, you might list the requirements that need to be fulfilled in your application.

Requirements

Then you take the first requirement and write a failing test. The test fails and it is RED as you haven't developed it yet. But the test describes already what your code should do to fulfill the requirement. Now you need to make the test GREEN by writing the necessary code to make the test pass. After you write the code to make the test green you need to REFACTOR the code. Maybe you can simplify the code or extract few code lines into a method to make code more readable and maintainable. After you are done with the first requirement, you can continue with the next requirement. This means you iterate the entire cycle (Red -> Green -> Refactor) for another requirement and so on. So the tests are driving your development. This is the heart of TDD and known as TDD cycle.

TDD means writing tests to implement a requirement and continuously iterate through RED GREEN and REFACTOR cycles.

Red Green Refactor

Advantages

Advantages
  1. TDD makes you think with the needed API from the beginning. You need to think about what classes, properties, API's are needed. This will usually lead to great API design.
  2. After you know the class and properties, another big advantage is that you need to think about what the code should do than how it should do. As you start with test you don't need to have any idea about implementation. You just need to write a test for what the code should do. After writing the test you can think of requirements and their development.
  3. While thinking of your requirements, you get quick feedback about your requirements by running the test. The fact that you get quick feedback means you even don't need a fully working application at all. You just need a class library to build your business logic and don't need the entire project.
  4. This helps you create modular code. You can decouple the dependencies from the beginning and TDD makes you do that from the beginning. This decoupling of dependency makes you write a modular code by isolating the dependencies like a database that is not ready yet and the web API isn't ready when beginning the development.
  5. This leads to a maintainable codebase, as you will have one test per requirement. You can write code to add new functionality and run all the unit tests to ensure that the existing code doesn't break. You can be confident about your new code as well as the existing code.
  6. These tests will serve as good documentation. For example, the test for the code written by others will help you understand why the code has been written.

Disadvantages

The only disadvantage is that TDD is not so easy to start by writing tests for beginners. In fact, TDD is an mindset shift and art that every developer should master.

Summary

In this article we got introduced to TDD and learnt about Red, Green and Refactor phases of TDD along with its advantages and disadvantages. In next article lets learn about how to implement TDD using C# .NET in detail.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • TDD
  • Test Driven Development