Skip to content

Commit 70bc7e0

Browse files
committed
Add DynamicSkipExample
1 parent 49b3a24 commit 70bc7e0

File tree

10 files changed

+273
-0
lines changed

10 files changed

+273
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\packages\xunit.core.2.1.0-beta4-build3042\build\portable-net45+dnxcore50+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props" Condition="Exists('..\packages\xunit.core.2.1.0-beta4-build3042\build\portable-net45+dnxcore50+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props')" />
4+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
5+
<PropertyGroup>
6+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
7+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
8+
<ProjectGuid>{DC430BD1-F360-4866-8B5C-ADAB8B82CC81}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>DynamicSkipExample</RootNamespace>
12+
<AssemblyName>DynamicSkipExample</AssemblyName>
13+
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
<NuGetPackageImportStamp>025406ca</NuGetPackageImportStamp>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="System" />
36+
<Reference Include="System.Core" />
37+
<Reference Include="System.Xml.Linq" />
38+
<Reference Include="System.Data.DataSetExtensions" />
39+
<Reference Include="Microsoft.CSharp" />
40+
<Reference Include="System.Data" />
41+
<Reference Include="System.Xml" />
42+
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
43+
<HintPath>..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll</HintPath>
44+
<Private>True</Private>
45+
</Reference>
46+
<Reference Include="xunit.assert, Version=2.1.0.3042, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
47+
<HintPath>..\packages\xunit.assert.2.1.0-beta4-build3042\lib\portable-net45+dnxcore50+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.assert.dll</HintPath>
48+
<Private>True</Private>
49+
</Reference>
50+
<Reference Include="xunit.core, Version=2.1.0.3042, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
51+
<HintPath>..\packages\xunit.extensibility.core.2.1.0-beta4-build3042\lib\portable-net45+dnxcore50+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.dll</HintPath>
52+
<Private>True</Private>
53+
</Reference>
54+
<Reference Include="xunit.execution.desktop, Version=2.1.0.3042, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
55+
<HintPath>..\packages\xunit.extensibility.execution.2.1.0-beta4-build3042\lib\net45\xunit.execution.desktop.dll</HintPath>
56+
<Private>True</Private>
57+
</Reference>
58+
</ItemGroup>
59+
<ItemGroup>
60+
<Compile Include="Samples.cs" />
61+
<Compile Include="SkippableFactAttribute.cs" />
62+
<Compile Include="Properties\AssemblyInfo.cs" />
63+
<Compile Include="XunitExtensions\SkippableFactDiscoverer.cs" />
64+
<Compile Include="XunitExtensions\SkippableFactMessageBus.cs" />
65+
<Compile Include="XunitExtensions\SkippableFactTestCase.cs" />
66+
<Compile Include="SkipTestException.cs" />
67+
</ItemGroup>
68+
<ItemGroup>
69+
<None Include="packages.config" />
70+
</ItemGroup>
71+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
72+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
73+
<PropertyGroup>
74+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
75+
</PropertyGroup>
76+
<Error Condition="!Exists('..\packages\xunit.core.2.1.0-beta4-build3042\build\portable-net45+dnxcore50+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.core.2.1.0-beta4-build3042\build\portable-net45+dnxcore50+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props'))" />
77+
</Target>
78+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
79+
Other similar extension points exist, see Microsoft.Common.targets.
80+
<Target Name="BeforeBuild">
81+
</Target>
82+
<Target Name="AfterBuild">
83+
</Target>
84+
-->
85+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("DynamicSkipExample")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("DynamicSkipExample")]
13+
[assembly: AssemblyCopyright("Copyright © 2015")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("b2adf4bf-4e46-4ba0-8981-bda53c6a5f89")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

DynamicSkipExample/Samples.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Xunit;
2+
3+
namespace DynamicSkipExample
4+
{
5+
public class Samples
6+
{
7+
[SkippableFact]
8+
public void Passing() { }
9+
10+
[SkippableFact(Skip = "I never feel like it")]
11+
public void StaticallySkipped() { }
12+
13+
[SkippableFact]
14+
public void DynamicallySkipped()
15+
{
16+
// You could hide this behind something like "Assert.Skip", by bring in the assertion
17+
// library as source and extending the Assert class.
18+
throw new SkipTestException("I don't feel like it right now, ask again later");
19+
}
20+
21+
[SkippableFact]
22+
public void Failing()
23+
{
24+
Assert.True(false);
25+
}
26+
}
27+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace DynamicSkipExample
4+
{
5+
public class SkipTestException : Exception
6+
{
7+
public SkipTestException(string reason)
8+
: base(reason) { }
9+
}
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Xunit;
2+
using Xunit.Sdk;
3+
4+
namespace DynamicSkipExample
5+
{
6+
[XunitTestCaseDiscoverer("DynamicSkipExample.XunitExtensions.SkippableFactDiscoverer", "DynamicSkipExample")]
7+
public class SkippableFactAttribute : FactAttribute { }
8+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Collections.Generic;
2+
using Xunit.Abstractions;
3+
using Xunit.Sdk;
4+
5+
namespace DynamicSkipExample.XunitExtensions
6+
{
7+
public class SkippableFactDiscoverer : IXunitTestCaseDiscoverer
8+
{
9+
readonly IMessageSink diagnosticMessageSink;
10+
11+
public SkippableFactDiscoverer(IMessageSink diagnosticMessageSink)
12+
{
13+
this.diagnosticMessageSink = diagnosticMessageSink;
14+
}
15+
16+
public IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
17+
{
18+
yield return new SkippableFactTestCase(diagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), testMethod);
19+
}
20+
}
21+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Linq;
2+
using Xunit.Abstractions;
3+
using Xunit.Sdk;
4+
5+
namespace DynamicSkipExample.XunitExtensions
6+
{
7+
public class SkippableFactMessageBus : IMessageBus
8+
{
9+
readonly IMessageBus innerBus;
10+
11+
public SkippableFactMessageBus(IMessageBus innerBus)
12+
{
13+
this.innerBus = innerBus;
14+
}
15+
16+
public int DynamicallySkippedTestCount { get; private set; }
17+
18+
public void Dispose() { }
19+
20+
public bool QueueMessage(IMessageSinkMessage message)
21+
{
22+
var testFailed = message as ITestFailed;
23+
if (testFailed != null)
24+
{
25+
var exceptionType = testFailed.ExceptionTypes.FirstOrDefault();
26+
if (exceptionType == typeof(SkipTestException).FullName)
27+
{
28+
DynamicallySkippedTestCount++;
29+
return innerBus.QueueMessage(new TestSkipped(testFailed.Test, testFailed.Messages.FirstOrDefault()));
30+
}
31+
}
32+
33+
// Nothing we care about, send it on its way
34+
return innerBus.QueueMessage(message);
35+
}
36+
}
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Xunit.Abstractions;
5+
using Xunit.Sdk;
6+
7+
namespace DynamicSkipExample.XunitExtensions
8+
{
9+
public class SkippableFactTestCase : XunitTestCase
10+
{
11+
[Obsolete("Called by the de-serializer; should only be called by deriving classes for de-serialization purposes")]
12+
public SkippableFactTestCase() { }
13+
14+
public SkippableFactTestCase(IMessageSink diagnosticMessageSink, TestMethodDisplay defaultMethodDisplay, ITestMethod testMethod, object[] testMethodArguments = null)
15+
: base(diagnosticMessageSink, defaultMethodDisplay, testMethod, testMethodArguments) { }
16+
17+
public override async Task<RunSummary> RunAsync(IMessageSink diagnosticMessageSink,
18+
IMessageBus messageBus,
19+
object[] constructorArguments,
20+
ExceptionAggregator aggregator,
21+
CancellationTokenSource cancellationTokenSource)
22+
{
23+
var skipMessageBus = new SkippableFactMessageBus(messageBus);
24+
var result = await base.RunAsync(diagnosticMessageSink, skipMessageBus, constructorArguments, aggregator, cancellationTokenSource);
25+
if (skipMessageBus.DynamicallySkippedTestCount > 0)
26+
{
27+
result.Failed -= skipMessageBus.DynamicallySkippedTestCount;
28+
result.Skipped += skipMessageBus.DynamicallySkippedTestCount;
29+
}
30+
31+
return result;
32+
}
33+
}
34+
}

DynamicSkipExample/packages.config

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="xunit" version="2.1.0-beta4-build3042" targetFramework="net451" />
4+
<package id="xunit.abstractions" version="2.0.0" targetFramework="net451" />
5+
<package id="xunit.assert" version="2.1.0-beta4-build3042" targetFramework="net451" />
6+
<package id="xunit.core" version="2.1.0-beta4-build3042" targetFramework="net451" />
7+
<package id="xunit.extensibility.core" version="2.1.0-beta4-build3042" targetFramework="net451" />
8+
<package id="xunit.extensibility.execution" version="2.1.0-beta4-build3042" targetFramework="net451" />
9+
</packages>

Samples.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestOutputExample", "TestOu
3333
EndProject
3434
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PartialTrustExample", "PartialTrustExample\PartialTrustExample.csproj", "{19F535B4-8B47-4F45-A5BF-7DEAB9CBB06F}"
3535
EndProject
36+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DynamicSkipExample", "DynamicSkipExample\DynamicSkipExample.csproj", "{DC430BD1-F360-4866-8B5C-ADAB8B82CC81}"
37+
EndProject
3638
Global
3739
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3840
Debug|Any CPU = Debug|Any CPU
@@ -99,6 +101,10 @@ Global
99101
{19F535B4-8B47-4F45-A5BF-7DEAB9CBB06F}.Debug|Any CPU.Build.0 = Debug|Any CPU
100102
{19F535B4-8B47-4F45-A5BF-7DEAB9CBB06F}.Release|Any CPU.ActiveCfg = Release|Any CPU
101103
{19F535B4-8B47-4F45-A5BF-7DEAB9CBB06F}.Release|Any CPU.Build.0 = Release|Any CPU
104+
{DC430BD1-F360-4866-8B5C-ADAB8B82CC81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
105+
{DC430BD1-F360-4866-8B5C-ADAB8B82CC81}.Debug|Any CPU.Build.0 = Debug|Any CPU
106+
{DC430BD1-F360-4866-8B5C-ADAB8B82CC81}.Release|Any CPU.ActiveCfg = Release|Any CPU
107+
{DC430BD1-F360-4866-8B5C-ADAB8B82CC81}.Release|Any CPU.Build.0 = Release|Any CPU
102108
EndGlobalSection
103109
GlobalSection(SolutionProperties) = preSolution
104110
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)