Coding Schools


 
Python | C Sharp | Azure AI | HTML | JavaScript | CSS | SQL Server
OOPS in C#
C# Data Types
Boxing and Unboxing in C#
Garbage Collection in C#
C# - Conditional Statements
C# - Loops
Interfaces in C#
Generics in C#
Collections in C#
C# 8.0 new features
C# Singleton Design Pattern
C# Factory Design Pattern
LINQ in C#
C# - Program to Find Prime Numbers
C# - Fibonacci Sequence
C# - Factorial of a number
C# - Recursive methods
C# - Anonymous Methods
C# - String Methods
C# TDD - XUnit
C# TDD - NUnit
C# Multiton Design Pattern
C# Facade Design Pattern
C# Abstract Factory Design Pattern
C# Decorator Design Pattern
C# Composite Design Pattern

C# TDD - NUnit



Test-Driven Development (TDD) is a software development approach in which tests are written before writing the actual code. In C#, NUnit is a popular testing framework used for TDD. It provides a powerful and flexible set of features to write and run tests.

Getting Started with NUnit

  1. Install NUnit: To use NUnit with a C# project, you need to add the NUnit and NUnit3TestAdapter NuGet packages. You can do this through the NuGet Package Manager in Visual Studio or by using the following commands in the Package Manager Console:

    sh
    Install-Package NUnit
    Install-Package NUnit3TestAdapter
    
  2. Create a Test Project: It's a good practice to create a separate test project in your solution. You can do this in Visual Studio by right-clicking your solution, selecting "Add" > "New Project...", and choosing "NUnit Test Project (.NET Core)".

  3. Write a Test: Create a new test class and write a test method. Following TDD, you should start by writing a failing test for a feature you want to implement.

    Example: Let's create a simple calculator with an addition feature.

    Test Class (CalculatorTests.cs):

    csharp
    using NUnit.Framework;
    
    [TestFixture]
    public class CalculatorTests
    {
        private Calculator _calculator;
    
        [SetUp]
        public void SetUp()
        {
            _calculator = new Calculator();
        }
    
        [Test]
        public void Add_TwoNumbers_ReturnsSum()
        {
            // Act
            var result = _calculator.Add(2, 3);
    
            // Assert
            Assert.AreEqual(5, result);
        }
    }
    
    • [TestFixture]: This attribute denotes a test class in NUnit.

    • [SetUp]: This attribute denotes a method that is run before each test. It is used to set up the necessary objects and values.

    • [Test]: This attribute denotes a test method in NUnit.

    • Assert: The Assert class is used to verify the outcome with assertions.

  4. Write the Implementation: Now that you have a failing test, write the minimum amount of code required to make the test pass.

    Implementation (Calculator.cs):

    csharp
    public class Calculator
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
    }
    
  5. Run the Test: Run your test in Visual Studio's Test Explorer or using the command line to see if it passes.

Advanced NUnit Features

  • Parameterized Tests: NUnit supports parameterized tests using [TestCase] attributes.

    Example:

    csharp
    [TestFixture]
    public class CalculatorTests
    {
        private Calculator _calculator;
    
        [SetUp]
        public void SetUp()
        {
            _calculator = new Calculator();
        }
    
        [TestCase(2, 3, 5)]
        [TestCase(-1, -1, -2)]
        [TestCase(0, 0, 0)]
        public void Add_MultipleCases_ReturnsSum(int a, int b, int expected)
        {
            // Act
            var result = _calculator.Add(a, b);
    
            // Assert
            Assert.AreEqual(expected, result);
        }
    }
    
  • Shared Context: You can share setup code across multiple test methods using the [SetUp] method and the [OneTimeSetUp] attribute for class-level setup.

    Example:

    csharp
    [TestFixture]
    public class CalculatorTests
    {
        private Calculator _calculator;
    
        [OneTimeSetUp]
        public void OneTimeSetUp()
        {
            _calculator = new Calculator();
        }
    
        [Test]
        public void Add_TwoNumbers_ReturnsSum()
        {
            var result = _calculator.Add(2, 3);
            Assert.AreEqual(5, result);
        }
    
        [Test]
        public void Add_NegativeNumbers_ReturnsSum()
        {
            var result = _calculator.Add(-1, -1);
            Assert.AreEqual(-2, result);
        }
    }
    

Summary

Using NUnit for TDD in C# promotes writing clean, tested, and maintainable code. By following the TDD cycle (write a failing test, implement the feature, refactor), you can ensure that your code works as expected and is easy to change.

Feel free to ask if you have any specific questions or need further examples on TDD with NUnit!




All rights reserved | Privacy Policy | Sitemap