-
Notifications
You must be signed in to change notification settings - Fork 269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ITestDataSource.GetData() could be easier to use with custom objects #246
Comments
@Frannsoft: That is a fair ask. Tagging @harshjain2 to get his thoughts on this. |
Strange that ITestDataSource wasn't more like ITestDataSource |
@harshjain2 Is there any traction on this? Seems like a fairly obvious functional gap for unit testing. |
I have used the following snippet // see https://github.com/microsoft/testfx/blob/master/src/TestFramework/MSTest.Core/Attributes/DataRowAttribute.cs
public abstract class TestDataSourceAttribute : Attribute, ITestDataSource
{
/// <summary>Gets Or sets display name in test results for customization.</summary>
public string DisplayName { get; set; }
public abstract IEnumerable<object[]> GetData(MethodInfo methodInfo);
public virtual string GetDisplayName(MethodInfo methodInfo, object[] data)
{
if (!string.IsNullOrWhiteSpace(DisplayName))
{
return DisplayName;
}
else
{
return $"{methodInfo.Name} ({string.Join(",", data.AsEnumerable())})";
}
}
} It is not possible to replace // see https://github.com/xunit/xunit/blob/main/src/xunit.v3.core/ClassDataAttribute.cs
public class ClassDataAttribute : TestDataSourceAttribute
{
public Type Class { get; }
/// <param name="class">The class that provides the data.</param>
public ClassData(Type @class)
{
Class = @class;
}
public override IEnumerable<object[]> GetData(MethodInfo methodInfo)
{
if (Activator.CreateInstance(Class) is ITestDataSource dataSource)
{
return dataSource.GetData(methodInfo);
}
else
{
throw new Exception($"{Class.FullName} must implement IEnumerable<object[]> to be used as ClassData.");
}
}
} Usage: [DataTestMethod]
[ClassData(typeof(CarTestData))]
public void ATest(Car carUnderTest)
{
// perform test.
} |
This was fixed by #4389. @Frannsoft Please, let us know if you see further issues. |
Implementing
ITestDataSource.GetData()
currently requires anIEnumerable<object[]>
to be returned. This is useful when wanting to return sets of primitives, but a bit tedious when the consumer wants to return more complex objects if I'm using this correctly.Example:
The way I've done this for now is by implementing
ITestDataSource.GetData()
like so:Is there a better way I can do this? Or would it be possible to have a variant of
ITestDataSource
that returnsIEnumerable<object>
(or maybeIEnumerable<T>
) instead ofIEnumerable<object[]>
?I tried using
DynamicDataAttribute
to accomplish this as well and ran into a similar 'issue' ofIEnumerable<object[]>
as a required return value.Environment
Thanks for your time.
AB#1408171
The text was updated successfully, but these errors were encountered: