question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extra test cases for DataRow methods

See original GitHub issue

Description

After upgrading MSTest to MSTest v2, there is an extra test case for each DataRow method (like a summary to all data row tests of the method). And that causes the number of total tests, passed and failed are also not expected. It is fine with MSTest v1 and vstest.cosole.exe.

Is this a known issue or expected feature? Is there any parameters can be set to change the behavior?

Steps to reproduce

    [DataTestMethod]
    [DataRow("1")]
    [DataRow("2")]
    public void TestMethodA(string a)
    {
        Assert.IsTrue(true);
    }

    [DataTestMethod]
    [DataRow("1")]
    [DataRow("2")]
    public void TestMethodB(string a)
    {
        Assert.IsTrue(a.Equals("1"));
    }

Expected behavior

Passed   TestMethodA (Data Row 0)
Passed   TestMethodA (Data Row 1)
Passed   TestMethodB (Data Row 0)
Failed   TestMethodB (Data Row 1)
Total tests: 4. Passed: 3. Failed: 1. Skipped: 0.

Actual behavior

Passed   TestMethodA
Passed   TestMethodA (Data Row 0)
Passed   TestMethodA (Data Row 1)
Failed   TestMethodB
Passed   TestMethodB (Data Row 0)
Failed   TestMethodB (Data Row 1)
Total tests: 6. Passed: 4. Failed: 2. Skipped: 0.

Environment

Run tests through vstest.console.exe on Azure DevOps MSTest 1.3.2

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:48 (23 by maintainers)

github_iconTop GitHub Comments

3reactions
enisakcommented, Feb 1, 2022

2.2.8, works totally fine for me

1reaction
Hylke-Atmoscommented, Jun 30, 2020

I’d like to argue that this is a bug and undesired behavior!

It seems that not only the results seem off. It also seems like it is incorrectly dealing with the DynamicData method used for retrieving the data. The first entry is retrieved twice (first for the initial call and then second time for actual execution of the data).

Due to this behavior we have a test that is simply failing because its data (a FileStream) is retrieved/opened twice which is (by default) not allowed by the file system.

It is not the most pretty test, but it is a simple and effective test for checking the function that is verifying the version we embed in a file.

public static IEnumerable<object[]> GetFileStreamForGetVersionFromFileStream()
{
    yield return new object[] { new FileStream(@"Assets\temp1.x", FileMode.Open), 0x01 };
    yield return new object[] { new FileStream(@"Assets\temp2.x", FileMode.Open), 0x02 };
    yield return new object[] { new FileStream(@"Assets\temp3.x", FileMode.Open), 0x03 };
}

[DataTestMethod]
[DynamicData(nameof(GetFileStreamForGetVersionFromFileStream), DynamicDataSourceType.Method)]
public void GetVersionFromFileStreamReturnsCorrectValue(FileStream fs, int expected)
{
   MethodInfo method =
       typeof(FileHandler).GetMethod("GetVersionFromFileStream", BindingFlags.NonPublic | BindingFlags.Static);
   var actual = uint.MaxValue;
   if (method != null)
   {
       actual = (uint) method.Invoke(null, new object[] {fs});
   }
   Assert.AreEqual((uint)expected, actual);
   //Cleanup
   fs.Close();
}

This test fails because temp1.x is opened twice and causing the following exception:

The process cannot access the file '...\Assets\temp1.x' because it is being used by another process.

Although it is possible to overcome this particular problem by using the FileShare.Read flag, it is undesired (and unexpected) behavior that MSTest is retrieving and locking the resources twice!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Improving MSTest unit tests with DataRows
Our first option for testing would be to write a series of tests that ... This is better, but it comes at the...
Read more >
Create Data-Driven Unit Tests - Visual Studio (Windows)
Learn how to use the Microsoft unit test framework for managed code to set up a unit test method to retrieve values from...
Read more >
Writing parameterized unit tests in C# — Run the same test ...
One of the best ways to do that is to write a single test which will support multiple test cases through parameters. This...
Read more >
Using DataTestMethod and DataRow to pass parameters to a ...
In TDD, you will need to test for various inputs based on some condition. For example, we have a class that is responsible...
Read more >
Running selective unit tests with specific DataRow Value - ...
I would try first to use the .runsettings method for defining environment variables for running tests: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found