簡體   English   中英

如何使用NUnit在Visual Studio中強制最后一次運行測試

[英]How to force a test run last in visual studio using NUnit

因此,在我的測試套件中,我有一個抽象基類,我的集成測試繼承自該基類。 此基類的每個派生都有自己的一組測試。 子類做出的斷言通過基類上的受保護方法運行。 在這些斷言期間,基類記錄字典中的哪些值已經過測試。 在子類運行完所有測試之后,我希望基類運行一個測試,以驗證所有正確的東西均已通過測試。

一些免責聲明:

  1. 是的,我知道這是一個有序的測試,並且這些都被拒絕了。 但是,這還是我想做的事情。
  2. 從某種意義上說,我知道這是測試我的測試套件的測試。 盡管這常常令人不悅,但我發現它很有用。 如果您希望您的測試真正地成為您的文檔,那么最好進行一些基本的測試來驗證有關文檔的一些基本內容-例如,它是完整的。 (在大多數項目中,這可能是過大的,並不值得。但是,在這個特定項目中,它既是個人項目,也是在100%代碼覆蓋率下進行的實驗。)

現在,我已經用[Test][TestFixtureTearDown]標記了摘要測試,這確實使測試在最后運行。 但是,這也意味着當測試失敗時,測試套件會因為拆卸失敗而生氣。 在理想的世界中,我想要的是[RunLast]之類的東西。 關於如何實現這一目標的任何想法?

當前代碼示例:

[TestFixture]
public abstract class AttributesTests : IntegrationTests
{
    [Inject]
    public IAttributesMapper AttributesMapper { get; set; }

    protected abstract String tableName { get; }

    private Dictionary<String, IEnumerable<String>> table;
    private List<String> testedNames;

    [TestFixtureSetUp]
    public void FixtureSetup()
    {
        testedNames = new List<String>();
    }

    [SetUp]
    public void Setup()
    {
        table = AttributesMapper.Map(tableName);
    }

    [Test, TestFixtureTearDown]
    public void AllNamesTested()
    {
        var missingNames = table.Keys.Except(testedNames);
        Assert.That(missingNames, Is.Empty, tableName);
    }

    [Test, TestFixtureTearDown]
    public void NoNamesTestedNultipleTimes()
    {
        var duplicateNames = testedNames.Where(n => testedNames.Count(cn => cn == n) > 1).Distinct();
        Assert.That(duplicateNames, Is.Empty, tableName);
    }

    protected void AssertAttributes(String name, IEnumerable<String> attributes)
    {
        testedNames.Add(name);

        Assert.That(table.Keys, Contains.Item(name), tableName);

        foreach (var attribute in attributes)
        {
            Assert.That(table[name], Contains.Item(attribute));

            var actualAttributeCount = table[name].Count(a => a == attribute);
            var expectedAttributeCount = attributes.Count(a => a == attribute);
            Assert.That(actualAttributeCount, Is.EqualTo(expectedAttributeCount));
        }

        var extraAttributes = table[name].Except(attributes);
        Assert.That(extraAttributes, Is.Empty);
    }
}

這對我有用:

namespace ZZZ
public class ZZZZZ {
  [Test]
  public void ZZZZLastTest() {
    // Whatever . . . 
  }
}

我不知道沒有[LastTest]的顯式屬性,但我相信您可以改用[Suite]

我知道這種JUnit套件方法將按照定義的lineair順序執行測試類(盡管不能保證在一個類中執行測試的順序)。

@RunWith(Suite.class)
@SuiteClasses({ CalculatorTestAdd.class, CalculatorTestSubtract.class })
public class AllTests {

}

在這里, CalculatorTestSubtract將最后執行。 記住這一點,我想說您應該創建一個在最后進行摘要測試的套件。

查看NUnit文檔 ,我發現應該同樣可行:

namespace NUnit.Tests
{
  using System;
  using NUnit.Framework;

  private class AllTests
  {
    [Suite]
    public static IEnumerable Suite
    {
      get
      {
        ArrayList suite = new ArrayList();
        suite.Add(new OneTestCase());
        suite.Add(new AssemblyTests());
        suite.Add(new NoNamespaceTestFixture());
        return suite;
      }
    }
  }
}

您必須進行一些測試以查看它們是否可以線性執行,因為不能保證對ArrayList進行排序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM