所以在我的测试套件中,我有一个我的集成测试继承的抽象基类。此基类的每个派生都有自己的一组测试。子类所做的断言是通过基类上的受保护方法运行的。在这些断言期间,基类会记录字典中的哪些值已经过测试。在子类运行了所有测试之后,我希望基类运行一个测试来验证所有正确的测试内容。
一些免责声明:
目前,我已使用[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);
}
}
答案 0 :(得分:1)
这对我有用:
namespace ZZZ
public class ZZZZZ {
[Test]
public void ZZZZLastTest() {
// Whatever . . .
}
}
答案 1 :(得分:0)
没有明确的[LastTest]
- 类似属性我知道但我相信你可以选择使用[Suite]
。
我知道这个套件的JUnit方法会在定义时按照lineair顺序执行你的测试类(尽管不能保证执行一个类中的测试的顺序)。
@RunWith(Suite.class)
@SuiteClasses({ CalculatorTestAdd.class, CalculatorTestSubtract.class })
public class AllTests {
}
此处CalculatorTestSubtract
将在最后执行。记住这一点,我想说你应该创建一个套件,最后进行总结测试。
看NUnit documentation,我发现某些事情应该同样可能:
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
无法保证排序。