milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home
  3. C#

NUnit - Get Started

Details
Written by: Stanko Milosev
Category: NUnit
Published: 29 May 2016
Last Updated: 08 November 2018
Hits: 3473

In my case I decided to go to NUnit download page page and download NUnit directly (NUnit.3.2.1.msi).

Start new console application. In the solution add new class library. Then, in that class library add reference to NUnit, in my case file is:

C:\Program Files (x86)\NUnit.org\framework\3.2.1.0\net-4.5\nunit.framework.dll

Also add reference in class library to console application (application which we are going to test)

We will also need NUnit Test Adapter, in order to see our tests in test explorer.

Go to Tools -> Extensions and Updates

Go to online and search for NUnit3 Test Adapter - in my case I needed NUnit3 because of VS 2015

Install it. Restart VS. Code in console application which I am going to test looks like:

namespace NUnit
{
  public class Program
  {
    static void Main(string[] args)
    {
    }

    public string ATest()
    {
      return "Hello World";
    }
  }
}

Class library looks like:

using NUnit.Framework;
using NUnit;

namespace NUnitTest
{
  [TestFixture]
  public class HelloWorldTest
  {
    Program source;

    [SetUp]
    public void Init()
    {
      source = new Program();
    }

    [Test]
    public void ShouldShowHelloWorld()
    {
      Assert.AreEqual("Hello World", source.ATest());
    }
  }
}

Now rebuild solution. Open Test Explorer (Test -> Window -> Test Explorer)

And run all tests:

Parameterized Tests

Details
Written by: Stanko Milosev
Category: NUnit
Published: 06 June 2014
Last Updated: 06 June 2014
Hits: 4017

For parameterized tests I've created solution with two projects, one is console application, and second one (where my tests are actually) is class library.

My main, console, application looks like this: 

static void Main(string[] args)
{
	Summation mySum = new Summation();
	Console.WriteLine(mySum.MySummation(10).ToString());
	Console.ReadKey();
}

Where class "Summation" is like:

public class Summation
{
	public int MySummation(int myValue)
	{
		return myValue + myValue;
	}
}

Now, let's say that we want to create unit test for 10, 20 and 30 values. Instead of writing three tests we will use parameterized tests like:

[Test]
[Sequential]
public void Summ_results([Values(10, 20, 30)] int sumResult)
{
	Summation mySum = new Summation();
	Assert.AreEqual(sumResult + sumResult, mySum.MySummation(sumResult));
}

Notice part:

[Values(10, 20, 30)] 

With that line and class attribute [Sequential] we said that our test will be executed three times where each time sumResult will be 10, 20 or 30.

Example project you can download from here.

Number of years of oldest child in a list of parents

Details
Written by: Stanko Milosev
Category: LINQ
Published: 09 August 2018
Last Updated: 23 August 2018
Hits: 2624
One example to find out number of years of oldest child in a list of parents.
using System;
using System.Collections.Generic;
using System.Linq;

namespace LINQtest
{

    public class Child
    {
        public int Age { get; set; }
    }

    public class Parent
    {
        public Child[] Kids { get; set; }
    }

    public class MyLinq
    {
        public List<Parent> Parents { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Parent parentOne = new Parent();
            parentOne.Kids = new Child[2];

            parentOne.Kids[0] = new Child();
            parentOne.Kids[0].Age = 1;

            parentOne.Kids[1] = new Child();
            parentOne.Kids[1].Age = 2;

            Parent parentTwo = new Parent();
            parentTwo.Kids = new Child[3];
            parentTwo.Kids[0] = new Child();
            parentTwo.Kids[0].Age = 5;
            parentTwo.Kids[1] = new Child();
            parentTwo.Kids[1].Age = 7;
            parentTwo.Kids[2] = new Child();
            parentTwo.Kids[2].Age = 6;

            Parent parentThree = new Parent();
            parentThree.Kids = new Child[2];
            parentThree.Kids[0] = new Child();
            parentThree.Kids[0].Age = 3;

            parentThree.Kids[1] = new Child();
            parentThree.Kids[1].Age = 4;

            MyLinq myLinq = new MyLinq();
            myLinq.Parents = new List();
            myLinq.Parents.Add(parentOne);
            myLinq.Parents.Add(parentTwo);
            myLinq.Parents.Add(parentThree);

            int maxAge = myLinq.Parents.Max(parent => parent.Kids.Max(kid => kid.Age));

            Console.WriteLine("Oldest kid is age of: " + maxAge);
            Console.WriteLine("Press any key");
            Console.ReadKey();
        }
    }
}

POI:

int maxAge = myLinq.Parents.Max(parent => parent.Kids.Max(kid => kid.Age));

Because for example code:

IEnumerable<int> maxAges = myLinq.Parents.Select(parent => parent.Kids.Max(kid => kid.Age));

Will return collection of ages in list of parents.

Simple example

Details
Written by: Stanko Milosev
Category: LINQ
Published: 04 June 2016
Last Updated: 04 June 2016
Hits: 3002

One dummy example of LINQ:

using System;
using System.Linq;

namespace linqExamples
{
  class Program
  {
    static void Main(string[] args)
    {
      string s = "Test";
      var test = s.Select(d => "b");
      foreach(var b in test)
      {
        Console.WriteLine(b);
      }
      Console.ReadKey();
    }
  }
}
  1. Writing Custom Control in .NET 6
  2. Creating custom control for Windows forms
  3. Read dataset's XML and display all contained tables
  4. Read dataset's XML and display all contained tables

Subcategories

WPF

Beginning

Code snippets

NUnit

LINQ

Windows Forms

Page 30 of 33

  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33