How to RowTest with MSTest?

2019-01-04 12:01发布

I know that MSTest doesn't support RowTest and similar tests.

What do MSTests users do? How is it possible to live without RowTest support?

I've seen DataDriven test features but sounds like too much overhead, is there any 3rd party patch or tool which allow me to do RowTest similar tests in MSTest?

6条回答
虎瘦雄心在
2楼-- · 2019-01-04 12:01

I have solved this issue by generating test class code with different number of generated test methods. You just have to download 2 files and include them into your project.
Then subclass a class with required number of rows in your test code and implement 2 abstract methods:

[TestClass]
public class Ha_ha_ha_Test: MsTestRows.Rows.TestRows_42<string>
{
    public override void TestMethod(string dataRow, int rowNumber)
    {
        Console.WriteLine(dataRow);
        Assert.IsFalse(dataRow.Contains("3"));
    }

    public override string GetNextDataRow(int rowNumber)
    {
        return "data" + rowNumber;
    }
}

More details:

https://github.com/dzhariy/mstest-rows

查看更多
三岁会撩人
3楼-- · 2019-01-04 12:02

We have added in support for DataRow in VS2012 Update1. See this blog for a breif introduction

Edit: Want to highlight that this feature is currently limited to Windows store apps.

查看更多
Evening l夕情丶
4楼-- · 2019-01-04 12:09

On my team that is locked into using the MS Test framework, we developed a technique that relies only on Anonymous Types to hold an array of test data, and LINQ to loop through and test each row. It requires no additional classes or frameworks, and tends to be fairly easy to read and understand. It's also much easier to implement than the data-driven tests using external files or a connected database.

For example, say you have an extension method like this:

public static class Extensions
{
    /// <summary>
    /// Get the Qtr with optional offset to add or subtract quarters
    /// </summary>
    public static int GetQuarterNumber(this DateTime parmDate, int offset = 0)
    {
        return (int)Math.Ceiling(parmDate.AddMonths(offset * 3).Month / 3m);
    }
}

You could use and array of Anonymous Types combined to LINQ to write a tests like this:

[TestMethod]
public void MonthReturnsProperQuarterWithOffset()
{
    // Arrange
    var values = new[] {
        new { inputDate = new DateTime(2013, 1, 1), offset = 1, expectedQuarter = 2},
        new { inputDate = new DateTime(2013, 1, 1), offset = -1, expectedQuarter = 4},
        new { inputDate = new DateTime(2013, 4, 1), offset = 1, expectedQuarter = 3},
        new { inputDate = new DateTime(2013, 4, 1), offset = -1, expectedQuarter = 1},
        new { inputDate = new DateTime(2013, 7, 1), offset = 1, expectedQuarter = 4},
        new { inputDate = new DateTime(2013, 7, 1), offset = -1, expectedQuarter = 2},
        new { inputDate = new DateTime(2013, 10, 1), offset = 1, expectedQuarter = 1},
        new { inputDate = new DateTime(2013, 10, 1), offset = -1, expectedQuarter = 3}
        // Could add as many rows as you want, or extract to a private method that
        // builds the array of data
    }; 
    values.ToList().ForEach(val => 
    { 
        // Act 
        int actualQuarter = val.inputDate.GetQuarterNumber(val.offset); 
        // Assert 
        Assert.AreEqual(val.expectedQuarter, actualQuarter, 
            "Failed for inputDate={0}, offset={1} and expectedQuarter={2}.", val.inputDate, val.offset, val.expectedQuarter); 
        }); 
    }
}

When using this technique it's helpful to use a formatted message that includes the input data in the Assert to help you identify which row causes the test to fail.

I've blogged about this solution with more background and detail at AgileCoder.net.

查看更多
来,给爷笑一个
5楼-- · 2019-01-04 12:21

I know this is a late answer but hopefully it helps others out.

I looked everywhere for an elegant solution and ended up writing one myself. We use it in over 20 projects with thousands of unit tests and hundreds of thousands of iterations. Never once missed a beat.

https://github.com/Thwaitesy/MSTestHacks

1) Install the NuGet package.

2) Inherit your test class from TestBase

public class UnitTest1 : TestBase
{ }

3) Create a Property, Field or Method, that returns IEnumerable

public class UnitTest1 : TestBase
{
    private IEnumerable<int> Stuff
    {
        get
        {
            //This could do anything, get a dynamic list from anywhere....
            return new List<int> { 1, 2, 3 };
        }
    }
}

4) Add the MSTest DataSource attribute to your test method, pointing back to the IEnumerable name above. This needs to be fully qualified.

[DataSource("Namespace.UnitTest1.Stuff")]
public void TestMethod1()
{
    var number = this.TestContext.GetRuntimeDataSourceObject<int>();

    Assert.IsNotNull(number);
}

End Result: 3 iterations just like the normal DataSource :)

using Microsoft.VisualStudio.TestTools.UnitTesting;
using MSTestHacks;

namespace Namespace
{
    public class UnitTest1 : TestBase
    {
        private IEnumerable<int> Stuff
        {
            get
            {
                //This could do anything, get a dynamic list from anywhere....
                return new List<int> { 1, 2, 3 };
            }
        }

        [DataSource("Namespace.UnitTest1.Stuff")]
        public void TestMethod1()
        {
            var number = this.TestContext.GetRuntimeDataSourceObject<int>();

            Assert.IsNotNull(number);
        }
    }
}
查看更多
走好不送
6楼-- · 2019-01-04 12:23
[TestMethod]
Test1Row1
{
    Test1(1,4,5);
}

[TestMethod]
Test1Row2
{
    Test1(1,7,8);
}

private Test1(int i, int j, int k)
{
   //all code and assertions in here
}
查看更多
相关推荐>>
7楼-- · 2019-01-04 12:27

Similar to DaTest(not updated since 2008) solution using PostSharp is described in blog http://blog.drorhelper.com/2011/09/enabling-parameterized-tests-in-mstest.html

查看更多
登录 后发表回答