-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from skbkontur/nunit-retries
add retries
- Loading branch information
Showing
29 changed files
with
658 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
<Project> | ||
|
||
<PropertyGroup> | ||
<LangVersion>9</LangVersion> | ||
<Nullable>enable</Nullable> | ||
<NoWarn>8618</NoWarn> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<LangVersion>9</LangVersion> | ||
<Nullable>enable</Nullable> | ||
<NoWarn>8618</NoWarn> | ||
</PropertyGroup> | ||
|
||
<!-- source line mappings are not supported in portable pdb format yet (https://github.com/dotnet/core/blob/master/Documentation/diagnostics/portable_pdb.md) --> | ||
<PropertyGroup> | ||
<DebugType>full</DebugType> | ||
<DebugSymbols>true</DebugSymbols> | ||
</PropertyGroup> | ||
<!-- source line mappings are not supported in portable pdb format yet (https://github.com/dotnet/core/blob/master/Documentation/diagnostics/portable_pdb.md) --> | ||
<PropertyGroup> | ||
<DebugType>full</DebugType> | ||
<DebugSymbols>true</DebugSymbols> | ||
</PropertyGroup> | ||
|
||
<!-- include pdbs into nuget package --> | ||
<PropertyGroup> | ||
<RepositoryType>git</RepositoryType> | ||
<RepositoryUrl>https://github.com/skbkontur/nunit-middlewares</RepositoryUrl> | ||
<PackageProjectUrl>$(RepositoryUrl)</PackageProjectUrl> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder> | ||
</PropertyGroup> | ||
<!-- include pdbs into nuget package --> | ||
<PropertyGroup> | ||
<RepositoryType>git</RepositoryType> | ||
<RepositoryUrl>https://github.com/skbkontur/nunit-extensions</RepositoryUrl> | ||
<PackageProjectUrl>$(RepositoryUrl)</PackageProjectUrl> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
|
||
using NUnit.Framework; | ||
using NUnit.Framework.Internal; | ||
|
||
using SkbKontur.NUnit.Retries; | ||
|
||
namespace SkbKontur.NUnit.Extensions.Tests.Retries | ||
{ | ||
[RetryOnError(3)] | ||
public class FixtureWithRetries | ||
{ | ||
[Test] | ||
public void Test1() | ||
{ | ||
if (TestExecutionContext.CurrentContext.CurrentRepeatCount is 0 or 1) | ||
{ | ||
Assert.Fail("Third time's a Charm"); | ||
} | ||
} | ||
|
||
[Test] | ||
[RetryOnError(4)] | ||
public void Test2() | ||
{ | ||
if (TestExecutionContext.CurrentContext.CurrentRepeatCount is 0 or 1 or 2) | ||
{ | ||
throw new Exception("Forth time's a Charm, now with exception"); | ||
} | ||
} | ||
|
||
[Test] | ||
[NoRetry] | ||
public void Test3() | ||
{ | ||
Assert.Pass(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using NUnit.Framework; | ||
|
||
using SkbKontur.NUnit.Retries; | ||
|
||
namespace SkbKontur.NUnit.Extensions.Tests.Retries | ||
{ | ||
[SetUpFixture] | ||
[RetryOnError(2)] | ||
public class RetrySuite | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using NUnit.Framework; | ||
using NUnit.Framework.Internal; | ||
|
||
using SkbKontur.NUnit.Retries; | ||
|
||
namespace SkbKontur.NUnit.Extensions.Tests.Retries | ||
{ | ||
public class TestWithRetries | ||
{ | ||
[Test] | ||
public void Test1() | ||
{ | ||
if (TestExecutionContext.CurrentContext.CurrentRepeatCount is 0) | ||
{ | ||
Assert.Fail("Second time's a Charm"); | ||
} | ||
} | ||
|
||
[Test] | ||
[RetryOnError(3)] | ||
public void Test2() | ||
{ | ||
if (TestExecutionContext.CurrentContext.CurrentRepeatCount is 0 or 1) | ||
{ | ||
Assert.Fail("Third time's a Charm"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
using NUnit.Framework.Interfaces; | ||
|
||
namespace SkbKontur.NUnit.Retries | ||
{ | ||
public sealed class CustomAttributeMethodWrapper : IMethodInfo | ||
{ | ||
public CustomAttributeMethodWrapper(IMethodInfo baseInfo, params Attribute[] extraAttributes) | ||
{ | ||
this.baseInfo = baseInfo; | ||
this.extraAttributes = extraAttributes; | ||
} | ||
|
||
public ITypeInfo TypeInfo => baseInfo.TypeInfo; | ||
public MethodInfo MethodInfo => baseInfo.MethodInfo; | ||
public string Name => baseInfo.Name; | ||
public bool IsAbstract => baseInfo.IsAbstract; | ||
public bool IsPublic => baseInfo.IsPublic; | ||
public bool IsStatic => baseInfo.IsStatic; | ||
public bool ContainsGenericParameters => baseInfo.ContainsGenericParameters; | ||
public bool IsGenericMethod => baseInfo.IsGenericMethod; | ||
public bool IsGenericMethodDefinition => baseInfo.IsGenericMethodDefinition; | ||
public ITypeInfo ReturnType => baseInfo.ReturnType; | ||
|
||
public T[] GetCustomAttributes<T>(bool inherit) where T : class | ||
{ | ||
var bases = baseInfo.GetCustomAttributes<T>(inherit); | ||
var extras = extraAttributes.OfType<T>().ToArray(); | ||
|
||
return !extras.Any() | ||
? bases | ||
: !bases.Any() | ||
? extras | ||
: MergeAttributes(bases, extras); | ||
} | ||
|
||
public bool IsDefined<T>(bool inherit) where T : class | ||
=> baseInfo.IsDefined<T>(inherit) || extraAttributes.OfType<T>().Any(); | ||
|
||
public Type[] GetGenericArguments() => baseInfo.GetGenericArguments(); | ||
public IParameterInfo[] GetParameters() => baseInfo.GetParameters(); | ||
public object? Invoke(object? fixture, params object?[]? args) => baseInfo.Invoke(fixture, args); | ||
public IMethodInfo MakeGenericMethod(params Type[] typeArguments) => baseInfo.MakeGenericMethod(typeArguments); | ||
|
||
private static T[] MergeAttributes<T>(T[] bases, T[] extras) where T : class | ||
{ | ||
var baseTypes = new HashSet<Type>(bases.Select(x => x.GetType())); | ||
return bases | ||
.Concat(extras.Where(e => !baseTypes.Contains(e.GetType()))) | ||
.ToArray(); | ||
} | ||
|
||
private readonly IMethodInfo baseInfo; | ||
private readonly Attribute[] extraAttributes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
|
||
using NUnit.Framework.Internal; | ||
|
||
namespace SkbKontur.NUnit.Retries | ||
{ | ||
public interface IRetryStrategy | ||
{ | ||
public int TryCount { get; } | ||
public bool ShouldRetry(TestResult result); | ||
public void OnTestFailed(TestExecutionContext context, DateTimeOffset start); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<AssemblyName>SkbKontur.NUnit.Retries</AssemblyName> | ||
<RootNamespace>SkbKontur.NUnit.Retries</RootNamespace> | ||
<PackageId>SkbKontur.NUnit.Retries</PackageId> | ||
<PackageDescription>Retries for NUnit</PackageDescription> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
<PackageTags>NUnit Retry</PackageTags> | ||
<Authors>Pavel Vostretsov</Authors> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="NUnit" Version="3.13.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="README.md" Pack="true" PackagePath="\" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System; | ||
|
||
namespace SkbKontur.NUnit.Retries | ||
{ | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] | ||
public class NoRetryAttribute : Attribute | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# NUnit.Retries | ||
|
||
[![NuGet Status](https://img.shields.io/nuget/v/SkbKontur.NUnit.Retries.svg)](https://www.nuget.org/packages/SkbKontur.NUnit.Retries/) | ||
[![Build status](https://github.com/skbkontur/nunit-extensions/actions/workflows/actions.yml/badge.svg)](https://github.com/skbkontur/nunit-extensions/actions) | ||
|
||
Couple of helpful attributes for test retries: | ||
- `RetryOnErrorAttribute` is like NUnit's own `RetryAttribute`, but it can be applied to whole Fixture/Suite/Assembly, and supports retry after exceptions in test, not only assertion failures | ||
- On top of that, `RetryOnTeamCityAttribute` also supports TeamCity's [test retry](https://www.jetbrains.com/help/teamcity/2022.10/build-failure-conditions.html#test-retry) feature | ||
- `NoRetryAttribute` for disabling retries | ||
|
||
Attributes can be overriden on any level, e.g. | ||
- MyAssembly.dll: `[RetryOnError(2)]` | ||
- MySuite.cs: `[NoRetry]` | ||
- MyTestFixture.cs: `[RetryOnTeamCity(3)]` | ||
- MyTestMethod(): `[RetryOnError(4)]` | ||
|
||
This means we have two retries on assembly level in `MyAssembly.dll`, but no retries in `MySuite`, | ||
if `MyTestFixture` is also in `MySuite`, previous attributes are overriden by `RetryOnTeamCity`, | ||
and method `MyTestMethod` in `MyTestFixture` is retried 4 times. |
Oops, something went wrong.