我有一个自定义断言类,我目前正在进行单元测试。
public static class AssertionExtensions
{
public static void ShouldHaveAttributeApplied<T>(this T obj, Type attributeType)
{
Assert.IsNotNull(Attribute.GetCustomAttribute(typeof(T), attributeType));
}
}
这门课的测试是:
[TestMethod]
public void When_Called_For_Class_Validates_Pass_Correctly()
{
var obj = new FakeClass();
obj.ShouldHaveAttributeApplied(typeof(FakeClassAttribute));
}
[TestMethod]
public void When_Called_For_Class_Validates_Fail_Correctly()
{
var obj = new FakeClass();
obj.ShouldHaveAttributeApplied(typeof(UnappliedFakeClassAttribute));
}
测试When_Called_For_Class_Validates_Fail_Correctly
按预期失败,但如何将此失败标记为测试套件中的传递?
这是使用C#/ MSTest。
答案 0 :(得分:2)
将ExpectedException
属性添加到AssertionFailedException
的异常集使测试通过。
用法:
[TestMethod]
[ExpectedException(typeof(AssertFailedException))]
public void When_Called_For_Class_Validates_Fail_Correctly()
{
var obj = new FakeClass();
obj.ShouldHaveAttributeApplied(typeof(UnappliedFakeClassAttribute));
}