Skip to content

Commit

Permalink
Add a workaround for xUnit bug
Browse files Browse the repository at this point in the history
We're currently experiencing a bug where conditional skips aren't working in VS.
This is caused by https://github.com/xunit/xunit/issues/1782
  • Loading branch information
rynowak committed Sep 11, 2019
1 parent 4098f55 commit 7f81022
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using Xunit.Abstractions;
using Xunit.Sdk;
Expand Down Expand Up @@ -63,5 +64,24 @@ protected override IEnumerable<IXunitTestCase> CreateTestCasesForDataRow(ITestFr
base.CreateTestCasesForSkippedDataRow(discoveryOptions, testMethod, theoryAttribute, dataRow, skipReason)
: base.CreateTestCasesForDataRow(discoveryOptions, testMethod, theoryAttribute, dataRow);
}

protected override IEnumerable<IXunitTestCase> CreateTestCasesForSkippedDataRow(
ITestFrameworkDiscoveryOptions discoveryOptions,
ITestMethod testMethod,
IAttributeInfo theoryAttribute,
object[] dataRow,
string skipReason)
{
return new[]
{
new WORKAROUND_SkippedDataRowTestCase(DiagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), discoveryOptions.MethodDisplayOptionsOrDefault(), testMethod, skipReason, dataRow),
};
}

[Obsolete]
protected override IXunitTestCase CreateTestCaseForSkippedDataRow(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo theoryAttribute, object[] dataRow, string skipReason)
{
return new WORKAROUND_SkippedDataRowTestCase(DiagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), discoveryOptions.MethodDisplayOptionsOrDefault(), testMethod, skipReason, dataRow);
}
}
}
Expand Up @@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Threading;
using System.Threading.Tasks;
using Xunit.Abstractions;
using Xunit.Sdk;

Expand Down Expand Up @@ -33,8 +35,11 @@ protected override string GetSkipReason(IAttributeInfo factAttribute)

public override void Deserialize(IXunitSerializationInfo data)
{
base.Deserialize(data);
_skipReason = data.GetValue<string>(nameof(_skipReason));

// We need to call base after reading our value, because Deserialize will call
// into GetSkipReason.
base.Deserialize(data);
}

public override void Serialize(IXunitSerializationInfo data)
Expand Down
@@ -0,0 +1,80 @@
using System;
using System.ComponentModel;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace Microsoft.AspNetCore.Testing
{
// This is a workaround for https://github.com/xunit/xunit/issues/1782 - as such, this code is a copy-paste
// from xUnit with the exception of fixing the bug.
//
// This will only work with [ConditionalTheory].
internal class WORKAROUND_SkippedDataRowTestCase : XunitTestCase
{
string skipReason;

/// <summary/>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Called by the de-serializer; should only be called by deriving classes for de-serialization purposes")]
public WORKAROUND_SkippedDataRowTestCase() { }

/// <summary>
/// Initializes a new instance of the <see cref="XunitSkippedDataRowTestCase"/> class.
/// </summary>
/// <param name="diagnosticMessageSink">The message sink used to send diagnostic messages</param>
/// <param name="defaultMethodDisplay">Default method display to use (when not customized).</param>
/// <param name="testMethod">The test method this test case belongs to.</param>
/// <param name="skipReason">The reason that this test case will be skipped</param>
/// <param name="testMethodArguments">The arguments for the test method.</param>
[Obsolete("Please call the constructor which takes TestMethodDisplayOptions")]
public WORKAROUND_SkippedDataRowTestCase(IMessageSink diagnosticMessageSink,
TestMethodDisplay defaultMethodDisplay,
ITestMethod testMethod,
string skipReason,
object[] testMethodArguments = null)
: this(diagnosticMessageSink, defaultMethodDisplay, TestMethodDisplayOptions.None, testMethod, skipReason, testMethodArguments) { }

/// <summary>
/// Initializes a new instance of the <see cref="XunitSkippedDataRowTestCase"/> class.
/// </summary>
/// <param name="diagnosticMessageSink">The message sink used to send diagnostic messages</param>
/// <param name="defaultMethodDisplay">Default method display to use (when not customized).</param>
/// <param name="defaultMethodDisplayOptions">Default method display options to use (when not customized).</param>
/// <param name="testMethod">The test method this test case belongs to.</param>
/// <param name="skipReason">The reason that this test case will be skipped</param>
/// <param name="testMethodArguments">The arguments for the test method.</param>
public WORKAROUND_SkippedDataRowTestCase(IMessageSink diagnosticMessageSink,
TestMethodDisplay defaultMethodDisplay,
TestMethodDisplayOptions defaultMethodDisplayOptions,
ITestMethod testMethod,
string skipReason,
object[] testMethodArguments = null)
: base(diagnosticMessageSink, defaultMethodDisplay, defaultMethodDisplayOptions, testMethod, testMethodArguments)
{
this.skipReason = skipReason;
}

/// <inheritdoc/>
public override void Deserialize(IXunitSerializationInfo data)
{
// SkipReason has to be read before we call base.Deserialize, this is the workaround.
this.skipReason = data.GetValue<string>("SkipReason");

base.Deserialize(data);
}

/// <inheritdoc/>
protected override string GetSkipReason(IAttributeInfo factAttribute)
{
return skipReason;
}

/// <inheritdoc/>
public override void Serialize(IXunitSerializationInfo data)
{
base.Serialize(data);

data.AddValue("SkipReason", skipReason);
}
}
}

0 comments on commit 7f81022

Please sign in to comment.