gpt4 book ai didi

c# - 我怎样才能使 WebTest 失败?

转载 作者:太空狗 更新时间:2023-10-29 17:54:28 28 4
gpt4 key购买 nike

我正在使用 Microsoft WebTest 并希望能够执行类似于 NUnit 的 Assert.Fail() 的操作。我想出的最好办法是 throw new webTestException() 但这在测试结果中显示为 Error 而不是 Failure

除了反射(reflection) WebTest 设置一个私有(private)成员变量来指示失败之外,还有什么我错过的吗?

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

编辑:好吧,现在我真的很困惑。我使用反射将 Outcome 属性设置为失败,但测试仍然通过了!

这是将 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");
}

Outcome 被设置为 Oucome.Fail 但显然 WebTest 框架并没有真正使用它来确定测试通过/失败结果。

最佳答案

设置Outcome property 失败:

Outcome = Outcome.Fail;

还有一个 Assert.Fail()Microsoft.VisualStudio.QualityTools.UnitTestFramework 程序集中。

关于c# - 我怎样才能使 WebTest 失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/224467/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com