Skip to content

Commit

Permalink
Merge pull request #12 from couven92/assert
Browse files Browse the repository at this point in the history
Skip tests even if SkipException is coming from within Assert.Throws
  • Loading branch information
AArnott authored Oct 3, 2018
2 parents 8eaf385 + aae5999 commit c7f20ea
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<BaseIntermediateOutputPath>$(MSBuildThisFileDirectory)..\obj\$(MSBuildProjectName)\</BaseIntermediateOutputPath>
<OutputPath>$(MSBuildThisFileDirectory)..\bin\$(MSBuildProjectName)\$(Configuration)\</OutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Nerdbank.GitVersioning" Version="1.6.25" PrivateAssets="all" />
<PackageReference Include="Nerdbank.GitVersioning" Version="2.2.13" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="$(MSBuildThisFileDirectory)stylecop.json" />
Expand Down
10 changes: 10 additions & 0 deletions src/Xunit.SkippableFact.Tests/SampleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,15 @@ public void SkipTheoryMaybe(bool skip)
{
Skip.If(skip, "I was told to.");
}

[SkippableFact]
public void SkipInsideAssertThrows()
{
Assert.Throws<Exception>(new Action(() =>
{
Skip.If(true, "Skip inside Assert.Throws");
throw new Exception();
}));
}
}
}
13 changes: 8 additions & 5 deletions src/Xunit.SkippableFact.Tests/Xunit.SkippableFact.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
<ProjectReference Include="..\Xunit.SkippableFact\Xunit.SkippableFact.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Validation" Version="2.4.15" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="Validation" Version="2.4.18" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
</ItemGroup>
</Project>
</Project>
26 changes: 24 additions & 2 deletions src/Xunit.SkippableFact/Sdk/SkippableTestMessageBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,36 @@ public bool QueueMessage(IMessageSinkMessage message)
if (failed != null)
{
var outerException = failed.ExceptionTypes.FirstOrDefault();
if (outerException != null && Array.IndexOf(this.SkippingExceptionNames, outerException) >= 0)
bool skipTest = false;
string skipReason = null;
switch (outerException)
{
case string _ when this.ShouldSkipException(outerException):
skipTest = true;
skipReason = failed.Messages.FirstOrDefault();
break;
case "Xunit.Sdk.ThrowsException" when failed.ExceptionTypes.Length > 1:
outerException = failed.ExceptionTypes[1];
if (this.ShouldSkipException(outerException))
{
skipTest = true;
skipReason = (failed.Messages?.Length ?? 0) > 1 ? failed.Messages[1] : null;
}

break;
}

if (skipTest)
{
this.SkippedCount++;
return this.inner.QueueMessage(new TestSkipped(failed.Test, failed.Messages[0]));
return this.inner.QueueMessage(new TestSkipped(failed.Test, skipReason));
}
}

return this.inner.QueueMessage(message);
}

private bool ShouldSkipException(string exceptionType) =>
Array.IndexOf(this.SkippingExceptionNames, exceptionType) >= 0;
}
}
6 changes: 3 additions & 3 deletions src/Xunit.SkippableFact/Xunit.SkippableFact.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
<PackageTags>xunit testing skipping</PackageTags>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Validation" Version="2.4.15" PrivateAssets="compile;contentfiles;analyzers;build" />
<PackageReference Include="Validation" Version="2.4.18" PrivateAssets="compile;contentfiles;analyzers;build" />
<PackageReference Include="xunit.extensibility.execution" Version="2.1.0" Condition=" '$(TargetFramework)' == 'net45' " />
<PackageReference Include="xunit.extensibility.execution" Version="2.2.0" Condition=" '$(TargetFramework)' != 'net45' " />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.0" PrivateAssets="all" />
<PackageReference Include="xunit.extensibility.execution" Version="2.4.0" Condition=" '$(TargetFramework)' != 'net45' " />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" PrivateAssets="all" />
<PackageReference Update="NETStandard.Library" PrivateAssets="all" />
</ItemGroup>
<Target Name="SetNuSpecProperties" BeforeTargets="GenerateNuspec" DependsOnTargets="GetBuildVersion">
Expand Down

0 comments on commit c7f20ea

Please sign in to comment.