From 95f6c9561caf2c9ef57c933a6e38423b90235836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=B8is=C3=A6ther=20Rasch?= Date: Wed, 26 Sep 2018 23:21:59 +0200 Subject: [PATCH 1/5] Unit test for skipping inside of an Assert.Throws block --- src/Xunit.SkippableFact.Tests/SampleTests.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Xunit.SkippableFact.Tests/SampleTests.cs b/src/Xunit.SkippableFact.Tests/SampleTests.cs index e1db85f..a89c1c6 100644 --- a/src/Xunit.SkippableFact.Tests/SampleTests.cs +++ b/src/Xunit.SkippableFact.Tests/SampleTests.cs @@ -63,5 +63,15 @@ public void SkipTheoryMaybe(bool skip) { Skip.If(skip, "I was told to."); } + + [SkippableFact] + public void SkipInsideAssertThrows() + { + Assert.Throws(new Action(() => + { + Skip.If(true, "Skip inside Assert.Throws"); + throw new Exception(); + })); + } } } From e19ccad796b43620e7cf0401875b365f0ad224f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=B8is=C3=A6ther=20Rasch?= Date: Wed, 26 Sep 2018 23:23:13 +0200 Subject: [PATCH 2/5] Add special handling for ThrowsException and examines the thrown exception inside it --- src/Xunit.SkippableFact/Sdk/SkippableTestMessageBus.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Xunit.SkippableFact/Sdk/SkippableTestMessageBus.cs b/src/Xunit.SkippableFact/Sdk/SkippableTestMessageBus.cs index 98849a7..7cbb0b6 100644 --- a/src/Xunit.SkippableFact/Sdk/SkippableTestMessageBus.cs +++ b/src/Xunit.SkippableFact/Sdk/SkippableTestMessageBus.cs @@ -58,7 +58,8 @@ public bool QueueMessage(IMessageSinkMessage message) if (failed != null) { var outerException = failed.ExceptionTypes.FirstOrDefault(); - if (outerException != null && Array.IndexOf(this.SkippingExceptionNames, outerException) >= 0) + if ((outerException != null && Array.IndexOf(this.SkippingExceptionNames, outerException) >= 0) || + (outerException == "Xunit.Sdk.ThrowsException" && this.SkippingExceptionNames.Any(e => failed.Messages.Any(m => m.Contains($"Actual: typeof({e})"))))) { this.SkippedCount++; return this.inner.QueueMessage(new TestSkipped(failed.Test, failed.Messages[0])); From fc068d8e8eebd50fbaa913515ac464f248e2f4e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=B8is=C3=A6ther=20Rasch?= Date: Wed, 26 Sep 2018 23:31:58 +0200 Subject: [PATCH 3/5] Only match actual thrown exception with SkipException --- src/Xunit.SkippableFact/Sdk/SkippableTestMessageBus.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Xunit.SkippableFact/Sdk/SkippableTestMessageBus.cs b/src/Xunit.SkippableFact/Sdk/SkippableTestMessageBus.cs index 7cbb0b6..52cc30d 100644 --- a/src/Xunit.SkippableFact/Sdk/SkippableTestMessageBus.cs +++ b/src/Xunit.SkippableFact/Sdk/SkippableTestMessageBus.cs @@ -59,7 +59,7 @@ public bool QueueMessage(IMessageSinkMessage message) { var outerException = failed.ExceptionTypes.FirstOrDefault(); if ((outerException != null && Array.IndexOf(this.SkippingExceptionNames, outerException) >= 0) || - (outerException == "Xunit.Sdk.ThrowsException" && this.SkippingExceptionNames.Any(e => failed.Messages.Any(m => m.Contains($"Actual: typeof({e})"))))) + (outerException == "Xunit.Sdk.ThrowsException" && this.SkippingExceptionNames.Any(e => failed.Messages.Any(m => m.Contains($"Actual: typeof({typeof(SkipException).FullName})"))))) { this.SkippedCount++; return this.inner.QueueMessage(new TestSkipped(failed.Test, failed.Messages[0])); From 18d6eb8126b1e3ad32665d21ed3df9e9cca72a0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=B8is=C3=A6ther=20Rasch?= Date: Wed, 3 Oct 2018 02:57:02 +0200 Subject: [PATCH 4/5] Upgrading package references --- src/Directory.Build.props | 3 ++- .../Xunit.SkippableFact.Tests.csproj | 13 ++++++++----- src/Xunit.SkippableFact/Xunit.SkippableFact.csproj | 6 +++--- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index dde8b1a..46c7925 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -1,10 +1,11 @@ + $(MSBuildThisFileDirectory)..\obj\$(MSBuildProjectName)\ $(MSBuildThisFileDirectory)..\bin\$(MSBuildProjectName)\$(Configuration)\ - + diff --git a/src/Xunit.SkippableFact.Tests/Xunit.SkippableFact.Tests.csproj b/src/Xunit.SkippableFact.Tests/Xunit.SkippableFact.Tests.csproj index 7cec65c..8a7101a 100644 --- a/src/Xunit.SkippableFact.Tests/Xunit.SkippableFact.Tests.csproj +++ b/src/Xunit.SkippableFact.Tests/Xunit.SkippableFact.Tests.csproj @@ -12,9 +12,12 @@ - - - - + + + + runtime; build; native; contentfiles; analyzers + all + + - \ No newline at end of file + diff --git a/src/Xunit.SkippableFact/Xunit.SkippableFact.csproj b/src/Xunit.SkippableFact/Xunit.SkippableFact.csproj index e704edb..200eae4 100644 --- a/src/Xunit.SkippableFact/Xunit.SkippableFact.csproj +++ b/src/Xunit.SkippableFact/Xunit.SkippableFact.csproj @@ -16,10 +16,10 @@ xunit testing skipping - + - - + + From aae5999342e63bc57c3075227360d8f887fe730a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=B8is=C3=A6ther=20Rasch?= Date: Wed, 3 Oct 2018 02:58:05 +0200 Subject: [PATCH 5/5] Skip if propagated Exception type nested inside Xunit.Sdk.ThrowsException is skippable --- .../Sdk/SkippableTestMessageBus.cs | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Xunit.SkippableFact/Sdk/SkippableTestMessageBus.cs b/src/Xunit.SkippableFact/Sdk/SkippableTestMessageBus.cs index 52cc30d..60bf32d 100644 --- a/src/Xunit.SkippableFact/Sdk/SkippableTestMessageBus.cs +++ b/src/Xunit.SkippableFact/Sdk/SkippableTestMessageBus.cs @@ -58,15 +58,36 @@ public bool QueueMessage(IMessageSinkMessage message) if (failed != null) { var outerException = failed.ExceptionTypes.FirstOrDefault(); - if ((outerException != null && Array.IndexOf(this.SkippingExceptionNames, outerException) >= 0) || - (outerException == "Xunit.Sdk.ThrowsException" && this.SkippingExceptionNames.Any(e => failed.Messages.Any(m => m.Contains($"Actual: typeof({typeof(SkipException).FullName})"))))) + 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; } }