我正在使用微软的WebTest,并希望能够做一些类似于NUnit的Assert.Fail()。我想出的最好方法是使用throw new webTestException(),但是测试结果中显示的是Error而不是Failure。
除了在WebTest上设置一个私有成员变量以指示故障之外,还有什么是我遗漏的吗?
编辑:我也使用了Assert.Fail()方法,但是当从WebTest内部使用时,这仍然显示为错误而不是失败,并且Outcome属性是只读的(没有公共设置程序)。
编辑:好吧,现在我真的很困惑。我使用反射将Outcome属性设置为“失败”,但测试仍然通过!
下面是将Oucome设置为failed的代码:
public static class WebTestExtensions
{
public static void Fail(this WebTest test)
{
var method = test.GetType().GetMethod("set_Outcome", BindingFlags.NonPublic | BindingFlags.Instance);
method.Invoke(test, new object[] {Outcome.Fail});
}
}下面是我试图失败的代码:
public override IEnumerator<WebTestRequest> GetRequestEnumerator()
{
this.Fail();
yield return new WebTestRequest("http://google.com");
}Outcome正在被设置为Oucome.Fail,但显然WebTest框架并没有真正使用它来确定测试通过/失败的结果。
发布于 2008-10-22 04:15:15
将结果属性设置为失败
Outcome = Outcome.Fail;Microsoft.VisualStudio.QualityTools.UnitTestFramework程序集中也有一个Assert.Fail()。
发布于 2009-06-25 06:25:17
“结果”属性将为公众设置vst 2010 :-)
发布于 2010-05-06 14:53:16
通过添加总是失败的验证规则,可以使测试始终失败。例如,您可以编写如下失败验证规则:
public class FailValidationRule : ValidationRule
{
public override void Validate(object sender, ValidationEventArgs e)
{
e.IsValid = false;
}
}然后将新的验证规则附加到webtest的ValidateResponse事件中,如下所示:
public class CodedWebTest : WebTest
{
public override IEnumerator<WebTestRequest> GetRequestEnumerator()
{
WebTestRequest request1 = new WebTestRequest("http://www.google.com");
FailValidationRule failValidation = new FailValidationRule();
request1.ValidateResponse += new EventHandler<ValidationEventArgs>(failValidation.Validate);
yield return request1;
}
}https://stackoverflow.com/questions/224467
复制相似问题