Fundamental automated testing practices

Wednesday, August 18, 2020 5:45 PM

Fundamental's with xUnit

Unit Tests tests a small unit of code to validate code is performing the function requested in the class, method or function. 

Benefits of Unit Testing:

  • Saves time -  steers developers from illusion of perfect production code.
  • Aid refactoring code, regression - with poor automation or poor continuous integration / continuous delivery (CI/CD) can add many hours. Consider bugs, from customer to supports
  • Catch bugs faster with higher efficiency
  • Well written tests server as documentation of code
  • The earlier that a bug is discovered in the development process, the less expensive it will be to fix
Test-Driven Development (TDD)
  • Create unit tests before they write code 

Sample 1 - simple calculation
  • Create Folder Test with two Sub Folders CalcLib & CalcLibUTest
    • In CalcLib: dotnet new classlib //rename Class1 to Calculator and build
namespace CalcLib
{
    public class Calculator
    {
        public double Add(double a,double b)
        {
            //error code for test
            return a * b;
        }
    }
}

    • In CalcLibUtest: dotnet new xunit
    • In CalcLibUtest project file set reference:

<ItemGroup> 
    <ProjectReference
      Include="..\CalcLib\CalcLib.csproj" />
  </ItemGroup>
  • Write Unit tests
    • Arrange: Declare and instantiate variables for IO
    • Act: Execute unit in test (Here we are calling method we want to test)
    • Assert: Make one or more Assertions about output. (Assertion is a belief; if not true = failed test 
 public class CalcUnitTest
    {
        [Fact]
        public void Test1()
        {
            //arrange
            double a = 2;
            double b2;
            double expected = 4;

            var calc  =new CalcLib.Calculator();

            //act
            double actual = calc.Add(a,b);

            //assert
            Assert.Equal(expected,actual);    
        }

        [Fact]
        public void TestAdding2And3()
        {
            //arrange
            double a = 2;
            double b = 3;
            double expected = 5;
            
            var calc = new CalcLib.Calculator();
            //act
            double actual = calc.Add(a,b);
           //assert
            Assert.Equal(expected,actual); 
        }

    }
    • Run Unit tests
      • dotnet test

- Correct method & retest!

Sample 2 - Temperature Converter
  • Files Folders speratoed in scr and test folders
  • dotnet new sln -o TempConverter && cd ./TempConverter/

  • Start with Test ( TDD rule 1)
dotnet new xunit -o TempConverter.Tests
  • Add Test to project sln
  • dotnet sln add ./SpeedConverter.Tests/SpeedConverter.Tests.csproj
  • Follow Red-Green-Refactor Approach
  • i.e. Start with failing test
    using System;
    using Xunit;
    using TempConverter;

    namespace TempConverter.Tests
    {
        public class TempConverterIDisposable
        {
            [Fact]
            public void ConvertTemp_0_0()
            {
                //arrange
                //70 = 21.11
                var converttemp = new ConvertTemp();

                double temp = 36.00;
                //act
                var result = converttemp.tempConverter(temp);
                //assert
                Assert.Equal(96.8,result);
            }       

           [Theory] //Theory atrribute
            [InlineData(36,96.8)]
            [InlineData(101,38.33)]

            public void ConvertTempTest(double input,double expected)
            {
                var converttnew ConvertTemp();
                var temp = convertt.tempConverter(input);
                temp = Math.Round(temp2); //round 38.333333..
                Assert.Equal(expected,temp);            
            }

            

            public void Dispose()
            {
                //no action now - as part of Teardown eg setting data to known state
            }
        }
    }
  •  Add src project

dotnet new classlib -o TempConverter.Code
dotnet sln add ./TempConverter.Code/TempConverter.Code.csproj

  • Program.cs

using System;
using static System.Console;
using TempConverter;

namespace TempConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Temp for conversion");
            var temp =0.00;
            var result = 0.00;
            var tempconverted = new ConvertTemp();

            try
            {
                  temp = Convert.ToDouble(ReadLine());                          

                  result = tempconverted.tempConverter(temp);
            }            
            catch (ArgumentException ex)
            {
           
                Console.WriteLine(ex);
            }

            System.Console.WriteLine($" {temp} converted = {result:N2}");

        }
    }
}

  • ConvertTemp Class
using System;
using System.Linq;

namespace TempConverter
{
    public class ConvertTemp
    {
        public double tempConverter(double temp)
        {
            if(temp > 0.00)
            {
                if(temp < 50.00)
                {
                    return (double)(temp * 1.8)+32;                  
                }
                else
                {
                    return (double)(temp - 32)/1.8;
                }                
            }
            else return 0.00;
        }
       
    }
}
  • Run tests 

Comments