如何以编程方式为数据驱动的测试创建测试输入

如何以编程方式为数据驱动的测试创建测试输入

本文介绍了有没有更好的方法将动态输入在线传递给DataTestMethod?IE.如何以编程方式为数据驱动的测试创建测试输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多年来,我一直在寻找这种方法,我认为我终于在"MSTest V2"(这是.netcore随附的方法,并且只能在Visual Studio中正确处理的方法)中找到一种真正的方法.2017).请参阅我的答案以获取解决方案.

I've been looking for this for years and years, and I think I've finally found a real way in "MSTest V2" (meaning the one that comes with .netcore, and is only really handled correctly in Visual Studio 2017). See my answer for my solution.

这为我解决的问题是我的输入数据不容易序列化,但是我需要对其中的许多逻辑进行测试.有很多理由使这种方法更好,但这对我来说是个停滞点.我被迫进行一个巨大的单元测试,并通过for循环遍历我的输入.直到现在.

The problem this solves for me is that my input data is not easily serialized, but I have logic that needs to be tested with many of these inputs. There are lots of reasons why it's better to do it this way, but that was the show-stopper for me; I was forced to have one giant unit test with a for loop going through my inputs. Until now.

推荐答案

您现在可以使用DynamicDataAttribute:

You can now use the DynamicDataAttribute:

[DynamicData(nameof("TestMethodInput"))]
[DataTestMethod]
public void TestMethod(List<string> list)
{
    Assert.AreEqual(2, list.Count);
}

public static IEnumerable<object[]> TestMethodInput
{
    get
    {
        return new[]
        {
            new object[] { new List<string> { "one" } },
            new object[] { new List<string> { "one", "two" } },
            new object[] { new List<string> { "one", "two", "three" } }
        };
    }
}

https://dev.to/frannsoft/mstest-v2---new-old-kid-on-the-block

https:上有更多详细内容://blogs.msdn.microsoft.com/devops/2017/07/18/extending-mstest-v2/

这篇关于有没有更好的方法将动态输入在线传递给DataTestMethod?IE.如何以编程方式为数据驱动的测试创建测试输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 02:11