xunit icon indicating copy to clipboard operation
xunit copied to clipboard

MemberData must reference a List if it's used for multiple theories

Open carlin-q-scott opened this issue 1 year ago • 1 comments

If multiple theories use MemberData to references an IEnumerable, all but the first test will fail due to this error:

the following constructor parameters did not have matching fixture data...

The rest of the message refers to the properties on the test parameter object.

If I change the type of my source to a List, then this error goes away. The documentation explicitly states that Enumerables have ToList() called on them before any test is run. This seems to be false based on the behavior I have observed.

carlin-q-scott avatar Jul 08 '22 23:07 carlin-q-scott

Can you provide a repro project? I tried this:

using System.Collections.Generic;
using Xunit;

public class Foo
{
	public static IEnumerable<object[]> MemberDataSource()
	{
		yield return new object[] { 42 };
		yield return new object[] { 2112 };
		yield return new object[] { 0 };
	}

	[Theory]
	[MemberData(nameof(MemberDataSource))]
	public void Test1(int x)
	{
		Assert.NotEqual(0, x);
	}

	[Theory]
	[MemberData(nameof(MemberDataSource))]
	public void Test2(int x)
	{
		Assert.NotEqual(2112, x);
	}

	[Theory]
	[MemberData(nameof(MemberDataSource))]
	public void Test3(int x)
	{
		Assert.NotEqual(42, x);
	}
}

and the results were what I expected:

xUnit.net Console Runner v2.4.2-pre.28+42307e821d (64-bit Desktop .NET 4.5.2, runtime: 4.0.30319.42000)
  Discovering: Repro (app domain = on [shadow copy], method display = ClassAndMethod, method display options = None)
  Discovered:  Repro (found 3 of 580 test cases)
  Starting:    Repro (parallel test collections = on, max threads = 24)
    Foo.Test3(x: 42) [FAIL]
      Assert.NotEqual() Failure
      Expected: Not 42
      Actual:   42
      Stack Trace:
        UnitTest.cs(33,0): at Foo.Test3(Int32 x)
    Foo.Test1(x: 0) [FAIL]
      Assert.NotEqual() Failure
      Expected: Not 0
      Actual:   0
      Stack Trace:
        UnitTest.cs(19,0): at Foo.Test1(Int32 x)
    Foo.Test2(x: 2112) [FAIL]
      Assert.NotEqual() Failure
      Expected: Not 2112
      Actual:   2112
      Stack Trace:
        UnitTest.cs(26,0): at Foo.Test2(Int32 x)
  Finished:    Repro
=== TEST EXECUTION SUMMARY ===
   Repro  Total: 9, Errors: 0, Failed: 3, Skipped: 0, Time: 0.265s

bradwilson avatar Jul 25 '22 02:07 bradwilson

Closing for age. Please open a new issue with a full repro project. Thanks!

bradwilson avatar Jun 11 '23 00:06 bradwilson