有没有更好的方法将动态输入内联传递给

有没有更好的方法将动态输入内联传递给

本文介绍了有没有更好的方法将动态输入内联传递给 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:12