Skip to content

Commit c7f20ea

Browse files
authored
Merge pull request #12 from couven92/assert
Skip tests even if SkipException is coming from within Assert.Throws
2 parents 8eaf385 + aae5999 commit c7f20ea

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

src/Directory.Build.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
12
<Project>
23
<PropertyGroup>
34
<BaseIntermediateOutputPath>$(MSBuildThisFileDirectory)..\obj\$(MSBuildProjectName)\</BaseIntermediateOutputPath>
45
<OutputPath>$(MSBuildThisFileDirectory)..\bin\$(MSBuildProjectName)\$(Configuration)\</OutputPath>
56
</PropertyGroup>
67
<ItemGroup>
7-
<PackageReference Include="Nerdbank.GitVersioning" Version="1.6.25" PrivateAssets="all" />
8+
<PackageReference Include="Nerdbank.GitVersioning" Version="2.2.13" PrivateAssets="all" />
89
</ItemGroup>
910
<ItemGroup>
1011
<AdditionalFiles Include="$(MSBuildThisFileDirectory)stylecop.json" />

src/Xunit.SkippableFact.Tests/SampleTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,15 @@ public void SkipTheoryMaybe(bool skip)
6363
{
6464
Skip.If(skip, "I was told to.");
6565
}
66+
67+
[SkippableFact]
68+
public void SkipInsideAssertThrows()
69+
{
70+
Assert.Throws<Exception>(new Action(() =>
71+
{
72+
Skip.If(true, "Skip inside Assert.Throws");
73+
throw new Exception();
74+
}));
75+
}
6676
}
6777
}

src/Xunit.SkippableFact.Tests/Xunit.SkippableFact.Tests.csproj

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
<ProjectReference Include="..\Xunit.SkippableFact\Xunit.SkippableFact.csproj" />
1313
</ItemGroup>
1414
<ItemGroup>
15-
<PackageReference Include="Validation" Version="2.4.15" />
16-
<PackageReference Include="xunit" Version="2.2.0" />
17-
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
18-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
15+
<PackageReference Include="Validation" Version="2.4.18" />
16+
<PackageReference Include="xunit" Version="2.4.0" />
17+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0">
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
19+
<PrivateAssets>all</PrivateAssets>
20+
</PackageReference>
21+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
1922
</ItemGroup>
20-
</Project>
23+
</Project>

src/Xunit.SkippableFact/Sdk/SkippableTestMessageBus.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,36 @@ public bool QueueMessage(IMessageSinkMessage message)
5858
if (failed != null)
5959
{
6060
var outerException = failed.ExceptionTypes.FirstOrDefault();
61-
if (outerException != null && Array.IndexOf(this.SkippingExceptionNames, outerException) >= 0)
61+
bool skipTest = false;
62+
string skipReason = null;
63+
switch (outerException)
64+
{
65+
case string _ when this.ShouldSkipException(outerException):
66+
skipTest = true;
67+
skipReason = failed.Messages.FirstOrDefault();
68+
break;
69+
case "Xunit.Sdk.ThrowsException" when failed.ExceptionTypes.Length > 1:
70+
outerException = failed.ExceptionTypes[1];
71+
if (this.ShouldSkipException(outerException))
72+
{
73+
skipTest = true;
74+
skipReason = (failed.Messages?.Length ?? 0) > 1 ? failed.Messages[1] : null;
75+
}
76+
77+
break;
78+
}
79+
80+
if (skipTest)
6281
{
6382
this.SkippedCount++;
64-
return this.inner.QueueMessage(new TestSkipped(failed.Test, failed.Messages[0]));
83+
return this.inner.QueueMessage(new TestSkipped(failed.Test, skipReason));
6584
}
6685
}
6786

6887
return this.inner.QueueMessage(message);
6988
}
89+
90+
private bool ShouldSkipException(string exceptionType) =>
91+
Array.IndexOf(this.SkippingExceptionNames, exceptionType) >= 0;
7092
}
7193
}

src/Xunit.SkippableFact/Xunit.SkippableFact.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
<PackageTags>xunit testing skipping</PackageTags>
1717
</PropertyGroup>
1818
<ItemGroup>
19-
<PackageReference Include="Validation" Version="2.4.15" PrivateAssets="compile;contentfiles;analyzers;build" />
19+
<PackageReference Include="Validation" Version="2.4.18" PrivateAssets="compile;contentfiles;analyzers;build" />
2020
<PackageReference Include="xunit.extensibility.execution" Version="2.1.0" Condition=" '$(TargetFramework)' == 'net45' " />
21-
<PackageReference Include="xunit.extensibility.execution" Version="2.2.0" Condition=" '$(TargetFramework)' != 'net45' " />
22-
<PackageReference Include="StyleCop.Analyzers" Version="1.0.0" PrivateAssets="all" />
21+
<PackageReference Include="xunit.extensibility.execution" Version="2.4.0" Condition=" '$(TargetFramework)' != 'net45' " />
22+
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" PrivateAssets="all" />
2323
<PackageReference Update="NETStandard.Library" PrivateAssets="all" />
2424
</ItemGroup>
2525
<Target Name="SetNuSpecProperties" BeforeTargets="GenerateNuspec" DependsOnTargets="GetBuildVersion">

0 commit comments

Comments
 (0)