我想要设置一个设置,为每个测试设置一个所需的重试计数,例如,在实际失败之前,我可以重试所有失败的测试。我用这种方式构造了我的测试:
[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时,它没有效果。
发布于 2020-02-19 18:06:24
根据文档:“在测试方法上使用RetryAttribute来指定在失败时应该重新运行,最多最多一次。”
也就是说,参数是--不是--您可能认为的重试次数--而是运行测试和[Retry(1)]的尝试总数没有任何影响,无论您在哪里使用它。由于这是一个可能的混淆点,我只是编辑了这一页,给出一个明确的警告。
如果要尝试在类上使用RetryAttribute,则会收到编译器警告,因为它只能用于方法。但是,在NUnit中,方法可以表示单个测试或一组参数化测试。在参数化测试的情况下,该属性目前没有任何效果。
NUnit团队可以决定该属性适用于每个测试用例,并相应地修改nunit。TestCaseAttribute也可以接受一个指定重试计数的可选参数。对于一个长期的解决方案,你可能想问他们其中的一个或另一个选项。
在短期内,作为一种解决办法,您可以考虑从TestCaseAttribute中派生出您自己的属性。这里有一些(未经测试的)代码可以让你开始.
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);
}
}
}您将按以下方式使用这个
[RetryTestCase("some parameter", MaxTries=3)]
public void SomeTestName(string browser)
{
// Your test code
}关于上述各点的说明:
IRepeatTest是基于ICommandWrapper的,但没有添加任何方法。我认为这两个接口中的每一个都是我放置它们的地方,因为NUnit在代码中的不同点对它们进行了检查。TestCaseAttribute添加重试计数所需代码的三倍!问一下NUnit项目,如果你想要这个特性,或者自己贡献它!发布于 2020-07-29 10:06:52
对于任何想要看到@Charlie的TestCaseSource解决方案的修改版本的人来说。
[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
}然后,您可以使用:
[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中得到了验证。
https://stackoverflow.com/questions/60301860
复制相似问题