C# 我怎样才能通过网络考试?

C# 我怎样才能通过网络考试?,c#,unit-testing,mstest,webtest,C#,Unit Testing,Mstest,Webtest,我正在使用Microsoft WebTest,希望能够执行类似于NUnit的Assert.Fail()的操作。我想到的最好方法是抛出新的webTestException(),但这在测试结果中显示为错误,而不是失败 除了反思WebTest设置私有成员变量以指示失败之外,我还遗漏了什么吗 编辑:我也使用了Assert.Fail()方法,但在WebTest中使用时,这仍然显示为错误而不是失败,并且output属性是只读的(没有公共setter) 编辑:好吧,现在我真的被难住了。我使用反射将结果属性设置

我正在使用Microsoft WebTest,希望能够执行类似于NUnit的
Assert.Fail()
的操作。我想到的最好方法是抛出新的webTestException(),但这在测试结果中显示为
错误
,而不是
失败

除了反思
WebTest
设置私有成员变量以指示失败之外,我还遗漏了什么吗

编辑:我也使用了
Assert.Fail()
方法,但在WebTest中使用时,这仍然显示为错误而不是失败,并且
output
属性是只读的(没有公共setter)

编辑:好吧,现在我真的被难住了。我使用反射将
结果
属性设置为失败,但测试仍然通过

下面是将Oucome设置为失败的代码:

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");
    }
public覆盖IEnumerator GetRequestEnumerator()
{
这个。失败();
返回新的WebTestRequest(“http://google.com");
}
Outcome
设置为
Oucome.Fail
,但显然WebTest框架并没有真正使用它来确定测试通过/失败的结果。

设置失败:


Microsoft.VisualStudio.QualityTools.UnitTestFramework程序集中还有一个新的验证规则。

结果属性将在vsts 2010:-)设置公共属性。

通过添加始终失败的验证规则,使测试始终失败。例如,您可以编写如下失败验证规则:

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;
    }
}
公共类CodedWebTest:WebTest
{
公共重写IEnumerator GetRequestEnumerator()
{
WebTestRequest1=新的WebTestRequest(“http://www.google.com");
FailValidationRule failValidation=新的FailValidationRule();
request1.ValidateResponse+=新的事件处理程序(failValidation.Validate);
收益返回请求1;
}
}

在PostWebTest事件处理程序中设置结果值。

在声明性测试(以及编码测试)中有效的解决方案是:

  • 编写一个验证规则,当上下文中存在某个上下文参数(例如“FAIL”)时,该规则将失败
  • 如果要触发失败,请设置上下文参数并调用WebTest.Stop()
  • 将验证规则添加为WebTest级别规则(而不是请求级别),以便它在所有请求上运行

我认为这是最简洁的方法。

首先,我正在使用VB.net,但我也试图在发现它不起作用之前将结果设置为失败(这让我来到这里)

我最终通过抛出一个异常来做到这一点:

Public Overrides Sub PostRequest(ByVal sender As Object, ByVal e As PostRequestEventArgs)

    If YourTest = True Then

        Throw New WebTestException("My test Failed")

    End If

    MyBase.PostRequest(sender, e)

  End Sub

我知道这个话题很老了,但我希望它能帮助别人:)

应该更清楚。。。这两种我都试过了。(经过编辑的问题反映了这一点)[Microsoft.VisualStudio.QualityTools.WebTestFramework,版本=9.0.0.0]Microsoft.VisualStudio.TestTools.WebTesting.WebTest.set_结果(Microsoft.VisualStudio.TestTools.WebTesting.Output值)是公开的,与我使用的dll无关。Oucome是一个公众的创造者,但没有公众的创造者。我使用反射来校准set_结果(这是私有的),但它仍然对测试结果没有影响(即,它直到不会失败)
Public Overrides Sub PostRequest(ByVal sender As Object, ByVal e As PostRequestEventArgs)

    If YourTest = True Then

        Throw New WebTestException("My test Failed")

    End If

    MyBase.PostRequest(sender, e)

  End Sub