MemberAutoData only uses first entry from the supplied enumerable
See original GitHub issueIn AutoFixture.xUnit2
, the MemberAutoData
attribute only causes one test to be generated regardless of the size of the supplied data. You can see this in the test AutoFixture.Xunit2.UnitTest.Scenario.MemberAutoDataSuppliesDataSpecimens
.
A similar test with MemberData
and a member supplying a similar but “full” enumerable will generate two test cases:
[Theory, MemberData(nameof(StringData))]
public void MemberDataSuppliesDataSpecimens(string s1, string s2, MyClass myClass)
{
Assert.Equal("foo", s1);
Assert.NotNull(s2);
Assert.NotNull(myClass);
}
public static IEnumerable<object[]> StringData
{
get
{
yield return new object[] { "foo", "bar", new MyClass() };
yield return new object[] { "foo", "baz", new MyClass() };
}
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:5
Top Results From Across the Web
Use AutoData and MemberData attributes in XUnit test
Problem: AutoData attribute will generate random data for me. The only way I found is to get rid of the AutoData attribute and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The problem is that the implementation uses the CompositeDataAttribute base class. This one tries to merge the data from MemberDataAttribute and AutoDataAttribute, but the later always returns one row. So in your example MemberDataAttribute returns 2 rows, AutoDataAttribute only 1. The code merges this to the smallest number found, so only 1 row is generated.
I tried my best understanding of the AutoFixture source code and generated a (hopefully) working implementation:
So basically I iterate over the results from MemberDataAttribute while adding everytime the data from AutoDataAttribute.
The CustomizeFixture and the Resolve methods are copies from AutoDataAttribute. The CustomizeAttributeComparer class is a direct copy from the source code as the original class is internal.
The protected constructor is there to provide the possibility to inherit, e.g. while testing I made my own MemberAutoNSubstituteDataAttribute.
@Ergamon thank you for the workaround, it really helped me out.
Also we created a
ClassAutoDataAttribute
as well, with the slight modification of usingClassDataAttribute
instead ofMemberDataAttribute
.