重试一个Visual Studio C#TestMethod的 [英] Retry a Visual Studio C# TestMethod

查看:447
本文介绍了重试一个Visual Studio C#TestMethod的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何内置的机制为重试在C#中的Visual Studio 2008的单元测试框架测试。在点
为例,我有一个C#单元测试,看起来是这样的:

I'm curious to know if there's any built-in mechanism to retry tests in the Visual Studio 2008 unit testing framework for C#. Case in point, I have a C# unit test which looks something like:

[TestMethod]
public void MyMethod() {
    DoSomething();
    Assert.Something();
}

现在,偶尔 DoSomething的()表现不好;在这种情况下,我想在到达之前断言重新运行 DoSomething的()方法。很显然,我可以这样做:

Now, occasionally DoSomething() performs badly; in that case I would like to rerun the DoSomething() method before reaching the assertion. Obviously I can do something like:

...
do {
    Initialization();
    DoSomething();
} while (PerformedOK() == false);
Assert.Something();
...



虽然这是因为所添加的循环和重复的有点麻烦。测试初始化​​,否则将通过其他方法/类的构造完全处理

Though this is a bit cumbersome because of the added loop and repeating the test initialization which would otherwise be completely handled by other methods / class constructor.

我的问题是,是否有重试一个更方便的机制的考验,是这样的:

My question is whether there is a more convenient mechanism for retrying a test, something like:

DoSomething();
if (PerformedOK() == false) Retry();
else Assert.Something();



,它会自动重试测试的没有的注册它是失败的,而执行所有常规初始化代码像往常一样。

which will automatically retry the test without registering it as a failure, while performing all the regular initialization code as usual.

推荐答案

...认真

偶尔DoSomething的()执行
翻江倒海

"occasionally DoSomething() performs badly"

一个测试应该是绿色的每次。如果测试的代码执行,有时翻江倒海,那么你需要修复你的代码,隔离不同的行为。你应该有两个测试,一是在那里它断言正确的,当DoSomething的失败(并且应该失败),和一个在那里它断言正确的,当DoSomething的是确定的(并且应该是确定)。

A test should be green every time. If the tested code sometimes perform "badly", then you need to fix your code, isolating the different behavior. You should have two test, one where it Asserts correct when DoSomething fails (and is supposed to fail), and one where it Asserts correct when DoSomething is ok (and is supposed to be ok).

在测试中有重试逻辑是错误的海事组织。你应该总是断言对预期的结果,你应该能够隔离和仪器的代码返回你所期望的。

Having retry logic in a test is just wrong imo. You should always Assert on the expected outcome, and you should be able to isolate and instrument your code to return what you expect.

您可以创建一个循环包装它接受任何方法中并调用它的x次,或直至它成功。你也可以有循环功能调用你的init,或者通过它作为一个单独的参数。该方法还可以,如果成功返回一个布尔值。更改为满足您的需要签名。

You could create a loop wrapper which takes whatever method in and calls it X number of times, or until it succeeds. You could also have the Loop function call your init, or pass it as a separate argument. The method could also return a bool if successful. Change the signature to fit your needs.

[TestMethod]
public void something()
{
   Loop.LoopMe(TestMethod,3);            
   Assert.Something();
}

class Loop
{
    public static void LoopMe(Action action, int maxRetry )
    {
        Exception lastException = null;
        while (maxRetry > 0)
        {
            try
            {
                action();
                return;
            }
            catch (Exception e)
            {
                lastException = e;
                maxRetry--;                    
            }                
        }
        throw lastException;
    }



}

}

这篇关于重试一个Visual Studio C#TestMethod的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆