Do We Need Unit Test


Scenario 1(New Application/Module):-
Lets us assume below two classes which are exposed by a windows application:-
      public class Multiplication
    {
        public int Multiply(int x, int y)
        {
            return x * y;
        }
    }
    public class CalculatePower
    {
        public int RaiseTo(int number, int power)
        {
            if (power < 0)
            { throw new ArgumentException("Power cannot be negative"); }
            int result=1;
            Multiplication multiply=new Multiplication();
            for (int i = 1; i <= power; i++)
            {
                result = multiply.Multiply(result, number);
            }
            return result;
        }
    }
Now developer will have to wait till UI is ready for him to test his code "Hence Unit Test Is Needed In This Scenario" which will test his code without any interface.


Developer can simply write below unit tests(depends on business need and design) to check if his code is working.
    [TestClass]
    public class TestSet1
    {
        [TestMethod]
        public void MultiplyTwoNumbers()
        {
            Multiplication multiplication= new Multiplication();
            int result = multiplication.Multiply(2, 3);
            Assert.IsTrue(result == 6);
        }
        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void CalculatePowerNegativeparameterException()
        {
            CalculatePower power = new CalculatePower();
            int result = power.RaiseTo(-10, -20);
        }
        [TestMethod]       
        public void CalculatePower()
        {
            CalculatePower power = new CalculatePower();
            int result = power.RaiseTo(2, 3);
            Assert.IsTrue(result == 8);
        }
    }
Developer can write his code do unit test where he may fix bugs if any or move on to other task leading to better resource utilisation.

Scenario 2(Change In Existing Applcation/module):-
Now assume that there is bug in Multiply method and task has been assigned to a new developer he writes below code and there is no one to review his code.
      public class Multiplication
    {
        public int Multiply(int x, int y)
        {
            return x + y;
        }
    }
    public class CalculatePower
    {
        public int RaiseTo(int number, int power)
        {
            if (power < 0)
            { throw new ArgumentException("Power cannot be negative"); }
            int result=1;
            Multiplication multiply=new Multiplication();
            for (int i = 1; i <= power; i++)
            {
                result = multiply.Multiply(result, number);
            }
            return result;
        }
    }
Without Unit Test:
Without unit test one can input 2 and 2 as two arguments to see if his block of code is working which it will.
Its a busy day and he thinks thats enough testing as doing all cases manually is time consuming
Now he checks in broken code which will compile and work in few scenarios but will break in others.

With Unit Test:-
Developer will "write unit tests" / "execute set of existing tests" to see if functionality is right.
Not just that it also throws error for Calculate power thereby telling what is its impact on other modules.

Benefits Of Unit Testing:-
  1. ·       Robust code.
  2. ·       Test without external dependency.
  3. ·       More test cases can be executed in less time.
  4. ·       Early detection of bugs.
  5. ·       Impact on another module is also evaluated.
  6. ·       Efforts in Unit Testing pay in terms of software value when evaluated.


Comments

Popular posts from this blog

RabbitMQ setup and cluster configuration on a windows network

Gitflow using source tree

Component analysis for application security

Analyse log using plug and play FileBeat

Introduction to Blazor

What Interview Is/Is Not for an interviewer

.NET MAUI and Blazor to create windows/web and mobile application (.NET 6.0)

Azure

WCAG Accessibility

Clean Architecture