简体   繁体   English

如何使用MSTest进行RowTest?

[英]How to RowTest with MSTest?

I know that MSTest doesn't support RowTest and similar tests. 我知道MSTest不支持RowTest和类似的测试。

What do MSTests users do? MSTests用户做什么? How is it possible to live without RowTest support? 没有RowTest支持怎么办?

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 ? 我已经看过DataDriven测试功能,但听起来开销太大,是否有任何第三方补丁或工具可以让我在MSTest进行RowTest类似的测试?

[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
}

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. 我们在20多个项目中使用它,进行了数千次单元测试和数十万次迭代。 Never once missed a beat. 从来没有错过任何一个节拍。

https://github.com/Thwaitesy/MSTestHacks https://github.com/Thwaitesy/MSTestHacks

1) Install the NuGet package. 1)安装NuGet软件包。

2) Inherit your test class from TestBase 2)从TestBase继承您的测试类

public class UnitTest1 : TestBase
{ }

3) Create a Property, Field or Method, that returns IEnumerable 3)创建返回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. 4)将MSTest DataSource属性添加到您的测试方法中,指向上面的IEnumerable名称。 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 :) 最终结果: 3次迭代,就像普通的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);
        }
    }
}

We have added in support for DataRow in VS2012 Update1. 我们在VS2012 Update1中添加了对DataRow的支持。 See this blog for a breif introduction 请参阅此博客以获取breif简介

In VS2012 Update1, this feature is currently limited to Windows store apps. 在VS2012 Update1中,此功能当前仅限于Windows应用商店应用。 In later versions, it is not this limited. 在更高版本中,它不受此限制。

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. 在我的小组被锁定使用MS Test框架的团队中,我们开发了一种技术,该技术仅依赖于匿名类型来保存测试数据数组,而LINQ可以遍历和测试每一行。 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: 您可以使用结合到LINQ的匿名类型数组来编写这样的测试:

[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 . 我已经在AgileCoder.net上博客发布了有关此解决方案的更多背景和详细信息

类似于http://blog.drorhelper.com/2011/09/enabling-parameterized-tests-in-mstest.html的使用PostSharp的DaTest(自2008年以来未更新)解决方案。

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. 您只需要下载2个文件并将它们包含到您的项目中即可。
Then subclass a class with required number of rows in your test code and implement 2 abstract methods: 然后,在测试代码中将具有所需行数的类子类化,并实现2个抽象方法:

[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 https://github.com/dzhariy/mstest-rows

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM