Skip to content

Instantly share code, notes, and snippets.

@herecydev
Created March 26, 2015 13:13
Show Gist options
  • Save herecydev/39a652d881353e4cb6c6 to your computer and use it in GitHub Desktop.
Save herecydev/39a652d881353e4cb6c6 to your computer and use it in GitHub Desktop.
Xunit bug when data is an array
public class Tests
{
//Why is this not discovered?
[MyArrayTest]
public void ThisDoesntWork(byte[] bytes)
{
Xunit.Assert.True(true);
}
[MyStringTest]
public void ThisDoesWork(string bytes)
{
Xunit.Assert.True(true);
}
}
[Xunit.Sdk.XunitTestCaseDiscoverer("XunitTests.MyDiscoverer", "XunitTests")]
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = false)]
public class MyArrayTestAttribute : Xunit.FactAttribute { }
[Xunit.Sdk.XunitTestCaseDiscoverer("XunitTests.MyDiscoverer", "XunitTests")]
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = false)]
public class MyStringTestAttribute : Xunit.FactAttribute { }
public class MyDiscoverer : Xunit.Sdk.IXunitTestCaseDiscoverer
{
private readonly Xunit.Abstractions.IMessageSink _sink;
public MyDiscoverer(Xunit.Abstractions.IMessageSink sink)
{
_sink = sink;
}
public System.Collections.Generic.IEnumerable<Xunit.Sdk.IXunitTestCase> Discover(
Xunit.Abstractions.ITestFrameworkDiscoveryOptions discoveryOptions,
Xunit.Abstractions.ITestMethod testMethod,
Xunit.Abstractions.IAttributeInfo factAttribute)
{
object[] data = null;
var isAnArrayTest = testMethod.Method.GetCustomAttributes(typeof(MyArrayTestAttribute)).Any();
//simulate finding data
if(isAnArrayTest)
data = new object[] { new byte[] { 1, 2, 3 } };
else
data = new object[] { "hello world" };
return new[] { new Xunit.Sdk.XunitTestCase(_sink, Xunit.Sdk.TestMethodDisplay.Method, testMethod, data) };
}
}
@smudge202
Copy link

Updating the Discover method to try/catch any exceptions:

        public IEnumerable<Xunit.Sdk.IXunitTestCase> Discover(
            Xunit.Abstractions.ITestFrameworkDiscoveryOptions discoveryOptions,
            Xunit.Abstractions.ITestMethod testMethod,
            Xunit.Abstractions.IAttributeInfo factAttribute)
        {
            try
            {
                object[] data = null;

                var isAnArrayTest = testMethod.Method.GetCustomAttributes(typeof(MyArrayTestAttribute)).Any();

                //simulate finding data
                if (isAnArrayTest)
                    data = new object[] { new byte[] { 1, 2, 3 } };
                else
                    data = new object[] { "hello world" };

                return new[] { new Xunit.Sdk.XunitTestCase(_sink, Xunit.Sdk.TestMethodDisplay.Method, testMethod, data) };
            }
            catch
            {
                return new[] { new Xunit.Sdk.ExecutionErrorTestCase(_sink, Xunit.Sdk.TestMethodDisplay.Method, testMethod, "This message is never seen") };
            }
        }

Made no difference. Log:

------ Discover test started ------
[xUnit.net 00:00:00.2551595] Discovery starting: XunitTests (name display = ClassAndMethod)
[xUnit.net 00:00:00.6395139] Discovery finished: XunitTests (2 tests)
========== Discover test finished: 1 found (0:00:01.6567277) ==========
------ Run test started ------
[xUnit.net 00:00:00.0074042] Execution starting: XunitTests (method display = ClassAndMethod, parallel test collections = True, max threads = 8)
[xUnit.net 00:00:00.3170787] Execution finished: XunitTests
========== Run test finished: 1 run (0:00:02.754368) ==========

@smudge202
Copy link

To confirm, just changed the byte arrays on lines 5 and 43 to int arrays to confirm it isn't a byte serialization issue:

------ Discover test started ------
[xUnit.net 00:00:00.2631720] Discovery starting: XunitTests (name display = ClassAndMethod)
[xUnit.net 00:00:00.6644896] Discovery finished: XunitTests (2 tests)
========== Discover test finished: 1 found (0:00:01.6350956) ==========
------ Run test started ------
[xUnit.net 00:00:00.0076001] Execution starting: XunitTests (method display = ClassAndMethod, parallel test collections = True, max threads = 8)
[xUnit.net 00:00:00.3281403] Execution finished: XunitTests
========== Run test finished: 1 run (0:00:03.3356458) ==========

@herecydev
Copy link
Author

Escalated this to the xunit github repo as an issue #376

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment