Skip to content
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

Need clean way to share data between parameterized tests #169

Closed
JVimes opened this issue Apr 28, 2017 · 4 comments
Closed

Need clean way to share data between parameterized tests #169

JVimes opened this issue Apr 28, 2017 · 4 comments

Comments

@JVimes
Copy link

JVimes commented Apr 28, 2017

MSTest v2 needs a clean way to share test-data between tests. It should support objects.

Current approaches fall short:

  • DataRow can't be shared and doesn't support objects (just compile-time values).
  • DataSource doesn't support objects, and requires a database/csv.
  • Looping through an array/collection lumps the cases into a single test, making test results less readable.

Suggest adding the equivalent of NUnit's TestCaseSource, which takes the name of a field, property, or method that returns an IEnumerable (which can handle objects):

static object[] DivideCases =
{
    new object[] { 12, 3, 4 },
    new object[] { 12, 2, 6 },
    new object[] { 12, 4, 3 } 
};

[Test, TestCaseSource("DivideCases")]
public void DivideTest(int n, int d, int q)
{
    Assert.AreEqual( q, n / d );
}
@AbhitejJohn
Copy link
Contributor

Absolutely. Thanks for filing this @JVimes . This should also help drive a structure around #141.

@JVimes
Copy link
Author

JVimes commented May 26, 2017

This is being discussed in: microsoft/testfx-docs#12

@AbhitejJohn
Copy link
Contributor

This has been addressed in 1.2.0-beta release of the Framework/Adapter pair.

@JVimes
Copy link
Author

JVimes commented Jul 24, 2017

Thanks! For others who find this, here's how to try it out:

Make sure you have MSTest v1.2.0 or later. At time of writing it's in beta, so need to install yourself using NuGet's "Include prerelease" checkbox:

image

Then, add a static, object[][] property to your test class. Fill in data. Add[DynamicData("ThePropertyName")] to your method, and make sure it has the right number/type of parameters:

static object[] Data
{
    get => new[]
    {
        new object[] {1, 2, 3},
        new object[] {4, 5, 6}
    };
}
        
[TestMethod]
[DynamicData("Data")]
public void Test1(int a, int b, int c)
{
    Assert.AreEqual(1, a % 3);
    Assert.AreEqual(2, b % 3);
    Assert.AreEqual(0, c % 3);
}

You can alternatively use a method that returns an IEnumerable<object[]>, instead of a property. I'm going off this doc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants