[英]How to force a test run last in visual studio using NUnit
因此,在我的測試套件中,我有一個抽象基類,我的集成測試繼承自該基類。 此基類的每個派生都有自己的一組測試。 子類做出的斷言通過基類上的受保護方法運行。 在這些斷言期間,基類記錄字典中的哪些值已經過測試。 在子類運行完所有測試之后,我希望基類運行一個測試,以驗證所有正確的東西均已通過測試。
一些免責聲明:
現在,我已經用[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.