P M A J E W S K I

Please Wait For Loading

Why is testing our apps so important? - Software Developer's Tour

    You Are Currently Here!
  • Home
  • TutorialsWhy is testing our apps so important?
tests

Why is testing our apps so important?

Hi, today I would like to address the next topic – testing. 

The example that will be shown today is trivial, but it will effectively illustrate what can go wrong due to writing target code too quickly without testing.

There are many types of tests but today, I’ll focus on unit tests.

Firstly, let’s take a look at the simple project structure presented below.

project_structure

We have 3 projects.

  1. Calculator.Console – it’s console project application.
  2. Calculator.Engine – it’s a library application; this is where all of our application logic will reside.
    1. Calculator.Engine.Tests – the final project is where we will consolidate all our tests related to the Calculator.Engine project.

[Calculator.Console] Program.cs

using Calculator.Engine;

Console.WriteLine("Function -> Add");
Console.Write("X = ");
var x = double.Parse(Console.ReadLine());
Console.Write("Y = ");
var y = double.Parse(Console.ReadLine());
var z = MathEngine.Add(x, y);

Console.WriteLine($"Output = {z}");

The application is quite basic, lacking any form of validation etc.

It prompts the user for two numbers and returns the sum of these elements as the result.

[Calculator.Engine] MathEngine.cs

namespace Calculator.Engine;

public class MathEngine
{
    public static double Add(double x, double y)
    {
        return x - y;
    } 
}

This project is a brain of our application. As you can see the result of Add operation is wrong (x – y instead of x + y) but developer doesn’t know that.

[Calculator.Engine.Tests] AddTests

using Calculator.Engine;
using FluentAssertions;

namespace Calculator.Tests
{
    public class Tests
    {
        [Test]
        public void Returns_Valid()
        {
            // Arrange
            var x = 2;
            var y = 3;

            // Act
            var z = MathEngine.Add(x, y);

            // Assert
            z.Should().Be(5, because: "2 + 3 = 5");
        }

        [Test]
        public void Returns_Valid_X_Negative_Y_Positive()
        {
            // Arrange
            var x = -2;
            var y = 3;

            // Act
            var z = MathEngine.Add(x, y);

            // Assert
            z.Should().Be(1, because: "-2 + 3 = 1");
        }
    }
}

This project uses FluentAssertion overriding base assertions that comes with NUnit.

The first method, Returns_Valid, initializes the components required for addition. The result of Add operation should be 5 but, in MathEngine is – instead of +. Second method does the same but first component is negative.

Let’s then look at the test results in this form.

tests_result

FluentAssertions is named “Fluent”, because you can read the code like speech in real language.

We can see that both tests have failed. it is now possible to investigate to find the error.

[Calculator.Engine] MathEngine.cs

namespace Calculator.Engine;

public class MathEngine
{
    public static double Add(double x, double y)
    {
        return x + y;
    } 
}

When I fix the bug with replacing – with +, I’ll start the tests again and see what happens.

test_success

Everything works fine. The tests have passed, and the code is correct now.

Summary

This trivial example of a unit test can show us how important testing our applications is. Without tests, this code would likely be returned to development as a failed task or bug.

leave a comment