Saturday, October 21, 2017

Unit Test

.NET Testing with NUnit

The software we write should go through the testing phase. There are many testing frameworks available. Out of these testing frameworks, NUnit is one of famous, rich in features, developer's favourite automated testing framework.   
In this topic, I provide how to set up and key features of NUnit.

Enabling NUnit Test Execution in Visual Studio

I'm using Visual Studio 2017, select your test project, on reference, right click and select Manage Nuget. Install 
  • NUnit
  • Nunit Test Adapter


NUnit Attributes

[TestFixture] : This is annotation for test class
[Test]: This is annotation for test method
Assert: This is used tell test runner to tell whether the test is pass or fail. 

eg:-
    [TestFixture]
    public class CalculatorTests
    {
        [Test]
        public void ShouldSubtractTwoNegativeNumbers()
        {
            var sut = new Calculator();

            sut.Subtract(-5);
            sut.Subtract(-10);
            Assert.That(sut.CurrentValue, Is.EqualTo(15));
        }
    }

Attribute levels

We can set up attributes in 
  • Method Level
  • Class Level
  • Namespace or Assembly Level
  • Entire Assembly
Method Level
[SetUP]: Before each test run, setup method will execute.
[TearDown]: After each test executes, teardown method will execute.

Class Level
[TestFixtureSetUP]:  (Class level)
[TestFixtureTearDown]:  (Class level)

Namespace or Assembly Level
namespace Attributes.Tests.SomeNamespace
{
    [SetUpFixture]
    public class SetUpFixtureForSpecificNamespace
    {
        [SetUp]
        public void RunBeforeAnyTestsInSomeNamespace()
        {
            Console.WriteLine("Before any tests in SomeNamespace");
        }

        [TearDown]
        public void RunAfterAnyTestsInSomeNamespace()
        {
            Console.WriteLine("After all tests in SomeNamespace");
        }
    }
}


For Entire Assembly
// Notice no containing namespace for this class

[SetUpFixture]
public class SetUpFixtureForEntireAssembly
{
    [SetUp]
    public void RunBeforeAnyTestsInEntireAssembly()
    {
        Console.WriteLine("Before any tests in entire assembly");
    }

    [TearDown]
    public void RunAfterAnyTestsInInEntireAssembly()
    {
        Console.WriteLine("After all tests in entire assembly");
    }
}




Saturday, June 17, 2017

C# Enumerator

Recently when I was writing a function, it has for-each loop and if it's odd iteration result should be multiply by 1, if it's even it should be multiply by 3.

So our my mind default going to keep a one bool variable to check iteration is odd or even. This is not object oriented way of programming. When new requirements comes this has lot of changes. So I have though of use C# strong language features in order write my code more OO way.

So I have used IEnumerable, Have look at MulitplyingFactors function which has replaced the bool variable and that method is  extendable. I have removed tight couple IsOdd variable to separate function

Before
        static void Main(string[] args) {
            IEnumerable<int> list = new int[] { 2, 4, 5, 8, 1, 2, 8, 9 };
            Console.Out.WriteLine(GetValue(list));
            Console.ReadLine();
        }

        static int GetValue(IEnumerable<int> list) {
            int result=0;
            bool isOdd = false;

            foreach (int item in list) {
                factor.MoveNext();
                if(isOdd )
                      result += item;
                else
                      result += item *3;

                isOdd = !isOdd ;
            }
            return result;
        }

After
        static void Main(string[] args) {
            IEnumerable<int> list = new int[] { 2, 4, 5, 8, 1, 2, 8, 9 };
            Console.Out.WriteLine(GetValue(list));
            Console.ReadLine();
        }

        static int GetValue(IEnumerable<int> list) {
            int result=0;
            IEnumerator<int> factor = MulitplyingFactors.GetEnumerator();

            foreach (int item in list) {
                factor.MoveNext();
                result += item* factor.Current;
            }
            return result;
        }

        static IEnumerable<int> MulitplyingFactors {
            get
            {
                int factor = 3;
                while (true) {
                    yield return factor;
                    factor = 4 - factor;
                }
            }
        }