首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何重试NUnit TestCase?

如何重试NUnit TestCase?
EN

Stack Overflow用户
提问于 2020-02-19 13:49:49
回答 2查看 3.3K关注 0票数 3

我想要设置一个设置,为每个测试设置一个所需的重试计数,例如,在实际失败之前,我可以重试所有失败的测试。我用这种方式构造了我的测试:

代码语言:javascript
复制
    [TestCase(“Some parameter”, Category = “Test category”, TestName = “Name of test”, Description = “Description of test”)]
    public void SomeTestName(string browser) {
    //Test script
    }

如果我使用的是Test而不是TestCase,我只需要添加一个Retry(1)属性,但是如何使用TestCase实现相同的行为呢?我已经浏览过NUnit retry dynamic attribute,它有一个非常简洁的解决方案,但不幸的是,当我试图将它应用于TestCase时,它没有效果。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-19 18:06:24

根据文档:“在测试方法上使用RetryAttribute来指定在失败时应该重新运行,最多最多一次。”

也就是说,参数是--不是--您可能认为的重试次数--而是运行测试和[Retry(1)]的尝试总数没有任何影响,无论您在哪里使用它。由于这是一个可能的混淆点,我只是编辑了这一页,给出一个明确的警告。

如果要尝试在类上使用RetryAttribute,则会收到编译器警告,因为它只能用于方法。但是,在NUnit中,方法可以表示单个测试或一组参数化测试。在参数化测试的情况下,该属性目前没有任何效果。

NUnit团队可以决定该属性适用于每个测试用例,并相应地修改nunit。TestCaseAttribute也可以接受一个指定重试计数的可选参数。对于一个长期的解决方案,你可能想问他们其中的一个或另一个选项。

在短期内,作为一种解决办法,您可以考虑从TestCaseAttribute中派生出您自己的属性。这里有一些(未经测试的)代码可以让你开始.

代码语言:javascript
复制
using System;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal.Commands;

namespace NUnit.Framework
{
  [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
  public class RetryTestCaseAttribute : TestCaseAttribute, IRepeatTest
  {
    // You may not need all these constructors, but use at least the first two
    public RetryTestCaseAttribute(params object[] arguments) : base(arguments) { }
    public RetryTestCaseAttribute(object arg) : base(arg) { }
    public RetryTestCaseAttribute(object arg1, object arg2) : base(arg1, arg2) { }
    public RetryTestCaseAttribute(object arg1, object arg2, object arg3) : base(arg1, arg2, arg3) { }

    public int MaxTries { get; set; }

    // Should work, because NUnit only calls through the interface
    // Otherwise, you would delegate to a `new` non-interface `Wrap` method.
    TestCommand ICommandWrapper.Wrap(TestCommand command)
    {
      return new RetryAttribute.RetryCommand(command, MaxTries);
    }
  }
}

您将按以下方式使用这个

代码语言:javascript
复制
[RetryTestCase("some parameter", MaxTries=3)]
public void SomeTestName(string browser)
{
  // Your test code
}

关于上述各点的说明:

  1. 我已经编译了这段代码,但还没有进行测试。如果您尝试过,请发表评论,特别是如果它需要modifications.
  2. The代码,它依赖于NUnit内部的一些知识,将来可能会中断。需要更全面的实施,以使其成为未来的证据。特别是,我使用了一个事实,即IRepeatTest是基于ICommandWrapper的,但没有添加任何方法。我认为这两个接口中的每一个都是我放置它们的地方,因为NUnit在代码中的不同点对它们进行了检查。
  3. --这段代码是向TestCaseAttribute添加重试计数所需代码的三倍!问一下NUnit项目,如果你想要这个特性,或者自己贡献它!
票数 6
EN

Stack Overflow用户

发布于 2020-07-29 10:06:52

对于任何想要看到@Charlie的TestCaseSource解决方案的修改版本的人来说。

代码语言:javascript
复制
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
    public class RetryTestCaseSourceAttribute : TestCaseSourceAttribute, IRepeatTest
    {
        #region constructors
        public RetryTestCaseSourceAttribute(string sourceName) : base(sourceName){}
        public RetryTestCaseSourceAttribute(Type sourceType) : base(sourceType){}
        public RetryTestCaseSourceAttribute(Type sourceType, string sourceName) : base(sourceType, sourceName){}
        public RetryTestCaseSourceAttribute(string sourceName, object[] methodParams) : base(sourceName, methodParams){}
        public RetryTestCaseSourceAttribute(Type sourceType, string sourceName, object[] methodParams) : base(sourceType, sourceName, methodParams){}
        #endregion

        #region repeat components
        public int MaxTries { get; set; }
        TestCommand ICommandWrapper.Wrap(TestCommand command) => new RetryAttribute.RetryCommand(command, MaxTries);
        #endregion
    }

然后,您可以使用:

代码语言:javascript
复制
[RetryTestCaseSource(nameof(GetBrowsers), MaxTries = 3)]
public void SomeTestName(string browser)
{
    // Your test code
}
public static IEnumerable<string> GetBrowsers()
{
    return new List<string>() {/* browsers */};
}        

该解决方案在NUnit 3.12.0中得到了验证。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60301860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档