diff --git a/src/Microsoft.AspNetCore.Testing/xunit/ConditionalTheoryDiscoverer.cs b/src/Microsoft.AspNetCore.Testing/xunit/ConditionalTheoryDiscoverer.cs index ed5b3dd2932..d24421f5cd3 100644 --- a/src/Microsoft.AspNetCore.Testing/xunit/ConditionalTheoryDiscoverer.cs +++ b/src/Microsoft.AspNetCore.Testing/xunit/ConditionalTheoryDiscoverer.cs @@ -25,8 +25,22 @@ protected override IEnumerable CreateTestCasesForTheory(ITestFra protected override IEnumerable CreateTestCasesForDataRow(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo theoryAttribute, object[] dataRow) { var skipReason = testMethod.EvaluateSkipConditions(); - return skipReason != null - ? base.CreateTestCasesForSkippedDataRow(discoveryOptions, testMethod, theoryAttribute, dataRow, skipReason) + if (skipReason == null && dataRow?.Length > 0) + { + var obj = dataRow[0]; + if (obj != null) + { + var type = obj.GetType(); + var property = type.GetProperty("Skip"); + if (property != null && property.PropertyType.Equals(typeof(string))) + { + skipReason = property.GetValue(obj) as string; + } + } + } + + return skipReason != null ? + base.CreateTestCasesForSkippedDataRow(discoveryOptions, testMethod, theoryAttribute, dataRow, skipReason) : base.CreateTestCasesForDataRow(discoveryOptions, testMethod, theoryAttribute, dataRow); } } diff --git a/test/Microsoft.AspNetCore.Testing.Tests/ConditionalTheoryTest.cs b/test/Microsoft.AspNetCore.Testing.Tests/ConditionalTheoryTest.cs index 77a931f8360..1181f1365af 100644 --- a/test/Microsoft.AspNetCore.Testing.Tests/ConditionalTheoryTest.cs +++ b/test/Microsoft.AspNetCore.Testing.Tests/ConditionalTheoryTest.cs @@ -4,6 +4,7 @@ using System; using Microsoft.AspNetCore.Testing.xunit; using Xunit; +using Xunit.Abstractions; namespace Microsoft.AspNetCore.Testing { @@ -115,5 +116,41 @@ public void Dispose() Assert.True(TestRan, "If this assertion fails, a conditional theory wasn't discovered."); } } + + [ConditionalTheory] + [MemberData(nameof(SkippableData))] + public void WithSkipableData(Skippable skippable) + { + Assert.Null(skippable.Skip); + Assert.Equal(1, skippable.Data); + } + + public static TheoryData SkippableData => new TheoryData + { + new Skippable() { Data = 1 }, + new Skippable() { Data = 2, Skip = "This row should be skipped." } + }; + + public class Skippable : IXunitSerializable + { + public Skippable() { } + public int Data { get; set; } + public string Skip { get; set; } + + public void Serialize(IXunitSerializationInfo info) + { + info.AddValue(nameof(Data), Data, typeof(int)); + } + + public void Deserialize(IXunitSerializationInfo info) + { + Data = info.GetValue(nameof(Data)); + } + + public override string ToString() + { + return Data.ToString(); + } + } } }