зеркало из
1
0
Форкнуть 0
This commit is contained in:
lukzas 2019-10-27 19:34:00 +01:00
Родитель e95493d5f6
Коммит b2e739b053
7 изменённых файлов: 264 добавлений и 0 удалений

Просмотреть файл

@ -5,3 +5,4 @@ Samples of NUnit Usage in C#
* **money** Implementation and tests of a currency conversion class
* **syntax** Examples of NUnit syntax in C#
* **ExpectedExceptionExample** Shows how to implement a custom attribute for NUnit
* **TimeoutRetryAttributeExample** Shows how to implement a custom retry attribute for NUnit that will be triggered only in case of timeout (useful with flaky tests due to poor network connection)

Просмотреть файл

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.572
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TimeoutRetryAttributeExample", "TimeoutRetryAttributeExample\TimeoutRetryAttributeExample.csproj", "{C9A7C840-8C27-4964-9BCE-9B05F5662199}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C9A7C840-8C27-4964-9BCE-9B05F5662199}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C9A7C840-8C27-4964-9BCE-9B05F5662199}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C9A7C840-8C27-4964-9BCE-9B05F5662199}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C9A7C840-8C27-4964-9BCE-9B05F5662199}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6917CCEF-6B80-445A-9968-6652195E26AD}
EndGlobalSection
EndGlobal

Просмотреть файл

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("TimeoutRetryAttributeExample")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TimeoutRetryAttributeExample")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c9a7c840-8c27-4964-9bce-9b05f5662199")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

Просмотреть файл

@ -0,0 +1,74 @@
using System;
using System.Text.RegularExpressions;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
namespace TimeoutRetryAttributeExample
{
/// <summary>
/// Specifies that a test method should be rerun on timeout set by MaxTime attribute up to the specified
/// maximum number of times. Failure will be reported on assertion fail or exception
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class TimeoutRetryAttribute : PropertyAttribute, IWrapSetUpTearDown
{
private int _tryCount;
public TimeoutRetryAttribute(int tryCount) : base(tryCount)
{
_tryCount = tryCount;
}
#region IWrapSetUpTearDown Members
public TestCommand Wrap(TestCommand command)
{
return new RetryCommand(command, _tryCount);
}
#endregion
#region Nested RetryCommand Class
public class RetryCommand : DelegatingTestCommand
{
private int _tryCount;
public RetryCommand(TestCommand innerCommand, int tryCount)
: base(innerCommand)
{
_tryCount = tryCount;
}
public override TestResult Execute(TestExecutionContext context)
{
// Check for attribute dependencies at [Test] level or parent when [TestCase]/[TestCaseSource] is used to generate test cases
if (!context.CurrentTest.Properties.Keys.Contains(PropertyNames.MaxTime) && !context.CurrentTest.Parent.Properties.Keys.Contains(PropertyNames.MaxTime))
{
throw new NullReferenceException($"{PropertyNames.MaxTime} attribute must be set along with TimeoutRetry");
}
int count = _tryCount;
while (count-- > 0)
{
context.CurrentResult = innerCommand.Execute(context);
// Skip retry only with passed assertions or if failure is different than timeout
if (context.CurrentResult.ResultState != ResultState.Failure || !Regex.IsMatch(context.CurrentResult.Message, "Elapsed time of [0-9.,]*ms exceeds maximum of [0-9]*ms")) // NUnit.Framework.Internal.Commands.MaxTimeCommand
break;
// Clear result for retry
if (count > 0)
context.CurrentResult = context.CurrentTest.MakeTestResult();
}
return context.CurrentResult;
}
}
#endregion
}
}

Просмотреть файл

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\NUnit.3.12.0\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.12.0\build\NUnit.props')" />
<Import Project="..\packages\NUnit3TestAdapter.3.15.1\build\net35\NUnit3TestAdapter.props" Condition="Exists('..\packages\NUnit3TestAdapter.3.15.1\build\net35\NUnit3TestAdapter.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C9A7C840-8C27-4964-9BCE-9B05F5662199}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TimeoutRetryAttributeExample</RootNamespace>
<AssemblyName>TimeoutRetryAttributeExample</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework, Version=3.12.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.12.0\lib\net45\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="TimeoutRetryAttribute.cs" />
<Compile Include="TimeoutRetryAttributeTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\NUnit3TestAdapter.3.15.1\build\net35\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit3TestAdapter.3.15.1\build\net35\NUnit3TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\NUnit.3.12.0\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.12.0\build\NUnit.props'))" />
</Target>
</Project>

Просмотреть файл

@ -0,0 +1,57 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Threading;
namespace TimeoutRetryAttributeExample
{
[TestFixture]
public class TimeoutRetryAttributeTests
{
readonly Dictionary<string, int> retryCount = new Dictionary<string, int>()
{
{ "RetryIsFiredDueToTimeout", 0 },
{ "RetryIsNotFiredDueToTimeout", 0 },
{ "RetryIsNotFiredDueToFailedAssertion", 0 },
{ "RetryIsNotFiredDueToException", 0 }
};
[Test]
[MaxTime(1000)]
[TimeoutRetry(3)]
public void RetryIsFiredDueToTimeout()
{
TestContext.Out.WriteLine($"Running test for the {++retryCount["RetryIsFiredDueToTimeout"]} time");
Thread.Sleep(3000);
}
[Test]
[MaxTime(3000)]
[TimeoutRetry(5)]
public void RetryIsNotFiredDueToLackOfTimeout()
{
TestContext.Out.WriteLine($"Running test for the {++retryCount["RetryIsNotFiredDueToTimeout"]} time");
Thread.Sleep(1000);
}
[Test]
[MaxTime(1000)]
[TimeoutRetry(10)]
public void RetryIsNotFiredDueToFailedAssertion()
{
TestContext.Out.WriteLine($"Running test for the {++retryCount["RetryIsNotFiredDueToFailedAssertion"]} time");
Thread.Sleep(3000);
Assert.Fail("I failed");
}
[Test]
[MaxTime(1000)]
[TimeoutRetry(2)]
public void RetryIsNotFiredDueToException()
{
TestContext.Out.WriteLine($"Running test for the {++retryCount["RetryIsNotFiredDueToException"]} time");
Thread.Sleep(3000);
throw new Exception("Exception thrown");
}
}
}

Просмотреть файл

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="3.12.0" targetFramework="net461" />
<package id="NUnit3TestAdapter" version="3.15.1" targetFramework="net461" />
</packages>