Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NUnit.Framework;
using TestNUnit;
 
namespace UnitTest
{
    [TestFixture]
    public class NUnit
    {
        MyTest test=null;
        [SetUp]
        public void Initialize()
        {
            test = new MyTest();
        }
        [TearDown]
        public void CleanUp()
        {
            test = null;
        }
        [Test]
 
        public void Addition()
        {
            Assert.AreEqual(5, test.Add(2, 3));
        }
        [Test]
 
        public void Substraction()
        {
            Assert.AreEqual(0, test.Substract(2, 2));
 
        }
        [Test]
 
        public void Divide()
        {
            Assert.AreEqual(1, test.Devide(2, 2));
        }
    }
}



in the above code i have 3 [Test] methods. when we run them they run in alphabetical order.

i want to run in user defined order.

first Subtraction(), second Divide() and third addition() method.

can any one pls help me
Posted
Updated 19-Feb-18 10:14am
v2

When creating tests with NUnit, they will execute in alphabetical order.

For more info, check out the Official NUnit documentation.

When creating a test case, they should be independent of each other. In the cases of wanting to place slower tests last, grouping similar, or whatever there are a couple of solutions.

As of NUnit 3.2 you can use the OrderAttribute.

C#
public class TestFixture
{
  [Test, Order(1)]
  public void FirstTest()
  {
    Assert.AreEqual(1, 1);
  }

  [Test, Order(2)]
  public void SecondTest()
  {
    Assert.AreEqual(2, 2);
  }


Another workaround for older versions is to use the TestCase attribute. However, this requires you to input an argument into the function. The TestName property will act as your sort order.

C#
public class TestFixture
{
  [TestCase(1, TestName = "Test1")]
  public void FirstTest(int unused)
  {
    Assert.AreEqual(1, 1);
  }

  [TestCase(2, TestName = "Test2")]
  public void SecondTest(int unused)
  {
    Assert.AreEqual(2, 2);
  }


And for yet another workaround for older NUnit version, you can use the Category attribute.

C#
public class TestFixture
{
  [Test]
  [Category("1st")]
  public void FirstTest()
  {
    Assert.AreEqual(1, 1);
  }

  [Test, Category("2nd")]
  public void SecondTest()
  {
    Assert.AreEqual(2, 2);
  }

  [Test, Category("3rd"), Description("Optional in-line attribute")]
  public void ThirdTest()
  {
    Assert.AreEqual(3, 3);
  }
 
Share this answer
 
Comments
Maciej Los 19-Feb-18 16:22pm    
Well... This question is 4 years old...
Damian Suess 12-Apr-18 9:00am    
It may be 4 years old, yet it's viable and searchable via google's top 10 results. Not sure why the down-vote since it is a valid solution and isn't a workaround.
Maciej Los 12-Apr-18 11:16am    
I have no idea who down-vote your answer. As to my observations about that, it's common practice... ;(
Upvoted to balance unfair down-vote.
Damian Suess 15-Apr-18 19:45pm    
Thanks, Maciej for the heads up. Thank you for that!
 
Share this answer
 
Just make sure that the names order alphabetically, i.e. start with 1_Substract, 2_Divide, 3_Add. In my experience, that simple trick works reliably.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900