需要知道NUnit中执行属性的顺序 [英] Need to know order of execution attribute in NUnit

查看:123
本文介绍了需要知道NUnit中执行属性的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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));
        }
    }
}







以上代码我有3 [测试]方法。当我们运行它们时,它们按字母顺序运行。



我想以用户定义的顺序运行。



第一个减法(),第二个Divide()和第三个加法()方法。



可以任意请帮助我




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

推荐答案

使用NUnit创建测试时,它们将按字母顺序执行。



有关详细信息,请查看官方NUnit文档。



创建测试用例时,它们应该相互独立。如果想要最后放置较慢的测试,分组相似,或者有几个解决方案。



从NUnit 3.2开始,您可以使用 OrderAttribute



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.

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

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





旧版本的另一种解决方法是使用 TestCase属性。但是,这需要您在函数中输入参数。 TestName 属性将作为您的排序顺序。





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.

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);
  }





对于较旧的NUnit版本的另一种解决方法,您可以使用类别属性。





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

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);
  }


阅读: http://stackoverflow.com/questions/1078658/nunit-test-run-order [ ^ ]


只需确保名称按字母顺序排序,即以1_Substract,2_Divide,3_Add开头。根据我的经验,这个简单的技巧可靠地运作。
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.


这篇关于需要知道NUnit中执行属性的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆