如何重试 NUnit 测试用例?
How to retry a NUnit TestCase?
我想实现一个设置,我可以在其中为每个测试设置所需的重试次数,这样我就可以在实际失败之前重试所有失败的测试一次。我以这种方式构建我的测试:
[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] 实现相同的行为?我已经浏览了 ,它有一个非常巧妙的解决方案,但不幸的是,当我尝试将它应用于 [TestCase]
时它没有任何效果
根据文档:"RetryAttribute is used on a test method to specify that it should be rerun if it fails, up to a maximum number of times."
也就是说,参数是不是你可能认为的重试次数,而是运行测试和[Retry(1)]
的总尝试次数根本没有效果,无论你在哪里使用它。由于这可能会造成混淆,我只是编辑了该页面以给出明确的警告。
如果您尝试在 class 上使用 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
}
关于上面的一些注意事项:
我已经编译了这段代码,但还没有测试过。如果您试用了,请post发表评论,尤其是需要修改的时候。
该代码依赖于 NUnit 内部的一些知识,将来可能会中断。需要更全面的实施才能使其永不过时。特别是,我使用了 IRepeatTest
基于 ICommandWrapper
但未添加任何方法这一事实。我相信这两个接口中的每一个在我放置它们的地方都是需要的,因为 NUnit 在其代码的不同点检查它们。
与将重试计数添加到 TestCaseAttribute
所需的代码行数相比,此代码的行数大约是其三倍!询问 NUnit 项目是否需要该功能 - 或者自己贡献!
这里的任何人都希望看到@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 进行验证。
我想实现一个设置,我可以在其中为每个测试设置所需的重试次数,这样我就可以在实际失败之前重试所有失败的测试一次。我以这种方式构建我的测试:
[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] 实现相同的行为?我已经浏览了
根据文档:"RetryAttribute is used on a test method to specify that it should be rerun if it fails, up to a maximum number of times."
也就是说,参数是不是你可能认为的重试次数,而是运行测试和[Retry(1)]
的总尝试次数根本没有效果,无论你在哪里使用它。由于这可能会造成混淆,我只是编辑了该页面以给出明确的警告。
如果您尝试在 class 上使用 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
}
关于上面的一些注意事项:
我已经编译了这段代码,但还没有测试过。如果您试用了,请post发表评论,尤其是需要修改的时候。
该代码依赖于 NUnit 内部的一些知识,将来可能会中断。需要更全面的实施才能使其永不过时。特别是,我使用了
IRepeatTest
基于ICommandWrapper
但未添加任何方法这一事实。我相信这两个接口中的每一个在我放置它们的地方都是需要的,因为 NUnit 在其代码的不同点检查它们。与将重试计数添加到
TestCaseAttribute
所需的代码行数相比,此代码的行数大约是其三倍!询问 NUnit 项目是否需要该功能 - 或者自己贡献!
这里的任何人都希望看到@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 进行验证。