Skip to content

Commit e1e4c2e

Browse files
committed
#2719: Class with custom Fact with throwing Skip should fail appropriately
1 parent 8b0b13c commit e1e4c2e

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

src/xunit.execution/Sdk/Frameworks/XunitTestCase.cs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.ComponentModel;
55
using System.Diagnostics;
6+
using System.Globalization;
67
using System.Linq;
78
using System.Threading;
89
using System.Threading.Tasks;
@@ -118,25 +119,32 @@ protected override void Initialize()
118119
{
119120
base.Initialize();
120121

121-
var factAttribute = TestMethod.Method.GetCustomAttributes(typeof(FactAttribute)).First();
122-
var baseDisplayName = factAttribute.GetNamedArgument<string>("DisplayName") ?? BaseDisplayName;
122+
try
123+
{
124+
var factAttribute = TestMethod.Method.GetCustomAttributes(typeof(FactAttribute)).First();
125+
var baseDisplayName = factAttribute.GetNamedArgument<string>("DisplayName") ?? BaseDisplayName;
123126

124-
DisplayName = GetDisplayName(factAttribute, baseDisplayName);
125-
SkipReason = GetSkipReason(factAttribute);
126-
Timeout = GetTimeout(factAttribute);
127+
DisplayName = GetDisplayName(factAttribute, baseDisplayName);
128+
SkipReason = GetSkipReason(factAttribute);
129+
Timeout = GetTimeout(factAttribute);
127130

128-
foreach (var traitAttribute in GetTraitAttributesData(TestMethod))
129-
{
130-
var discovererAttribute = traitAttribute.GetCustomAttributes(typeof(TraitDiscovererAttribute)).FirstOrDefault();
131-
if (discovererAttribute != null)
131+
foreach (var traitAttribute in GetTraitAttributesData(TestMethod))
132132
{
133-
var discoverer = ExtensibilityPointFactory.GetTraitDiscoverer(DiagnosticMessageSink, discovererAttribute);
134-
if (discoverer != null)
135-
foreach (var keyValuePair in discoverer.GetTraits(traitAttribute))
136-
Traits.Add(keyValuePair.Key, keyValuePair.Value);
133+
var discovererAttribute = traitAttribute.GetCustomAttributes(typeof(TraitDiscovererAttribute)).FirstOrDefault();
134+
if (discovererAttribute != null)
135+
{
136+
var discoverer = ExtensibilityPointFactory.GetTraitDiscoverer(DiagnosticMessageSink, discovererAttribute);
137+
if (discoverer != null)
138+
foreach (var keyValuePair in discoverer.GetTraits(traitAttribute))
139+
Traits.Add(keyValuePair.Key, keyValuePair.Value);
140+
}
141+
else
142+
DiagnosticMessageSink.OnMessage(new DiagnosticMessage("Trait attribute on '{0}' did not have [TraitDiscoverer]", DisplayName));
137143
}
138-
else
139-
DiagnosticMessageSink.OnMessage(new DiagnosticMessage("Trait attribute on '{0}' did not have [TraitDiscoverer]", DisplayName));
144+
}
145+
catch (Exception ex)
146+
{
147+
InitializationException = new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Exception during initialization:{0}{1}", Environment.NewLine, ex.Unwrap()));
140148
}
141149
}
142150

test/test.xunit.execution/Acceptance/Xunit2AcceptanceTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,33 @@ public class ReadOnlySkipFact : FactAttribute
592592
// Property setter here is missing, so trying to use it with the overridden skip message will fail at runtime
593593
public override string Skip => "Skipped";
594594
}
595+
596+
// https://github.com/xunit/xunit/issues/2719
597+
[Fact]
598+
public void ClassWithThrowingSkipGetterShouldReportThatAsFailure()
599+
{
600+
var msgs = Run<ITestResultMessage>(new[] { typeof(ClassWithThrowingSkip) }).OrderBy(x => x.Test.DisplayName).ToList();
601+
602+
var msg = Assert.Single(msgs);
603+
Assert.Equal("Xunit2AcceptanceTests+CustomFacts+ClassWithThrowingSkip.TestMethod", msg.Test.DisplayName);
604+
var failed = Assert.IsAssignableFrom<ITestFailed>(msg);
605+
var message = Assert.Single(failed.Messages);
606+
Assert.StartsWith("Exception during initialization:" + Environment.NewLine + "System.DivideByZeroException: Attempted to divide by zero.", message);
607+
}
608+
609+
class ClassWithThrowingSkip
610+
{
611+
[ThrowingSkipFact]
612+
public void TestMethod()
613+
{
614+
Assert.True(false);
615+
}
616+
}
617+
618+
public class ThrowingSkipFactAttribute : FactAttribute
619+
{
620+
public override string Skip { get => throw new DivideByZeroException(); set => base.Skip = value; }
621+
}
595622
}
596623

597624
public class TestOutput : AcceptanceTestV2

0 commit comments

Comments
 (0)