Showing posts with label Unit Testing. Show all posts
Showing posts with label Unit Testing. Show all posts

Sunday, March 24, 2019

Moq Sample:

Class to be unit tested:

public class A
{
    B _objectB;
    C _objectC;

    public A(B objectB, C objectC)
    {
        _objectB = objectB;
        _objectC = objectC;
    }
    public virtual int Operation(int number1, int number2)
    {
        int result = 0;

        result += _objectB.Multiply(number1, number2);
        result += _objectC.Divide(number1, number2);

        return result;
    }
}

Unit testing with mocking:

using Moq;
using Xunit;

public class ATest
{
    [Fact]
    public void Operation_MockDependencies_ShouldPass()
    {
        //Arrange
        Mock mockB = new Mock();
        Mock mockC = new Mock();
        var sut = new A(mockB.Object, mockC.Object);
        int number1 = 10;
        int number2 = 5;

        mockB.Setup(x => x.Multiply(number1, number2)).Returns(10);
        mockC.Setup(x => x.Divide(number1, number2)).Returns(25);

        //Act
        var result = sut.Operation(number1, number2);

        //Assert
        Assert.Equal(35, result);
    }
}

What is Moq


  • Moq is a mocking framework for .NET
  • Pronounce as “Mock-you” or “Mock”
  • It helps to isolate dependencies in unit testing
  • Nuget packages
  • Moq (4.10.1)
  • Reference: https://github.com/moq/moq4


xUnit sample

Class to be unit tested:

public class A
{
    B _objectB;
    C _objectC;

    public A(B objectB, C objectC)
    {
        _objectB = objectB;
        _objectC = objectC;
    }
    public virtual int Operation(int number1, int number2)
    {
        int result = 0;

        result += _objectB.Multiply(number1, number2);
        result += _objectC.Divide(number1, number2);

        return result;
    }
}

Unit test:

using Xunit;
public class ATest
{
    [Fact]
    public void Operation_WithCorrectParameters_ShouldPass()
    {
        //Arrange
        int number1 = 10;
        int number2 = 5;
        var sut = new A(new B(), new C());

        //Act
        var result = sut.Operation(number1, number2);

        //Assert
        Assert.Equal(52, result);
    }

}

What is xUnit


  • xUnit is unit testing tool for .NET framework
  • It is free & Open Source (Licensed under Apache)
  • Supports .NET core
  • Nuget packages
  • xunit (2.4.1)
  • Xunit.runner.visualstudio (2.4.1)
  • Reference: https://xunit.github.io/


Tuesday, October 19, 2010

unit testing void methods

Assert class does not allow us to test void methods. So the work around would be to throw Exceptions from the void method, and handle it in your test class.

NUnit.Framework provides a method level attibute - ExpectedException for handling exceptions in your test class.


Reference: http://www.nunit.org/index.php?p=exception&r=2.2.10




Wednesday, December 16, 2009

How much unit test coverage you need?

Not less than 80% of coverage is advisable.

Effective test should consider not only code, but also input parameters, and application state which could also affect the behavior.

  1. test success condition
  2. test failure condition
  3. test boundary condition

http://www.codeproject.com/KB/testing/UnitTestVS2008.aspx

    Thursday, August 30, 2007

    Unit testing in .NET

    NUnit: Unit testing framework for .NET

    I had only theoretical knowledge about the unit testing until I came across NUnit. Later I've realized that NUnit can make a huge impact in the quality of the code as well as the project and how helpful it is for a .NET developer. Its very hard to maintain huge chunks of code and it will become real difficult to keep track of changes in a huge project. NUnit will come into picture here.
    TDD (Test driven development) and extreme programming:
    According to Aslam : "you have a requirement, you will start writing test code for the requirement before writing the actual code, obviously test fails when trying to run the test because the actual code is missing , then you will start writing the actual code.This method is called as TDD."
    isn't TDD crazy!!? :)
    Let me conclude, Test driven development involves 3 stages,
    1.write a test 2.write code to pass the test 3.refactor the code to make it simpler and flexible

    When TDD is getting used? Suppose the changes in requirement is so frequent and thus it results an overhead for the developer to keep track of the changes in code. TDD is a solution for the above mentioned problem.
    And the only way to understand TDD is to do it.
    TDD(test driven development) is an extreme programming technique. There will be a corresponding test code for every functionality in the actual code. Now comes the question, how will you write the unit test code in .NET? And the answer is NUnit.
    Here is a scenario on how to use NUnit.
    1. Account.cs :
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace bank
    {
    public class Account
    {
    private float balance;
    public void Deposit(float amount)
    {
    balance += amount;
    }

    public void Withdraw(float amount)
    {
    balance -= amount;
    }

    public void TransferFunds(Account destination, float amount)
    {
    }

    public float Balance
    {
    get { return balance; }
    }
    }
    }
    2. AccountTest.cs : unit test for Account.cs
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace bank
    {
    using NUnit.Framework;

    [TestFixture]
    public class AccountTest
    {
    [Test]
    public void TransferFunds()
    {
    Console.Out.WriteLine("start testing...");
    Console.Out.WriteLine("creating source object...");
    Account source = new Account();
    source.Deposit(200.00F);
    Console.Out.WriteLine("creating destination object...");
    Account destination = new Account();
    destination.Deposit(150.00F);
    Console.Out.WriteLine("transfer funds...");
    source.TransferFunds(destination, 100.00F);
    Assert.AreEqual(150.00F, destination.Balance);
    Assert.AreEqual(200.00F, source.Balance);
    Console.Out.WriteLine("end testing...");
    }
    }
    }

    3. Run the test (using GUI-Runner ie, nunit-gui.exe)



    Here test is successful and thus indicating in green color.


    Reference: http://www.nunit.org/


    And
    NUnit was my first work in IBM, and Thanks to Aslam(TPM) for
    giving me the opportunity to dig into NUnit. And later my learnings are
    recognized and added as an IBM asset in code library!! :)