如何在MSTest DataRow中将属性作为参数传递 [英] How can I pass property as parameter in MSTest DataRow

查看:144
本文介绍了如何在MSTest DataRow中将属性作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用MSTest的单元测试中有一个DataTestMethod,并且在构造函数中有一个字符串属性,例如,我有以下代码:

I have a DataTestMethod in my unit test using MSTest, and I have a string property in my constructor, let's say I have this code for example:

[DataTestMethod]
[DataRow("test")] 
[DataRow(stringproperty)] // How is this?

public void TestMethod(string test) {
    Assert.AreEqual("test", test);
}

是否可以在DataRow中将属性作为参数传递?

Is it possible to pass a property as parameter in DataRow?

谢谢.

推荐答案

是的,如果您使用DynamicDataAttribute

Yes if you use the DynamicDataAttribute

[DynamicData("TestMethodInput")]
[DataTestMethod]
public void TestMethod(string test)
{
    Assert.AreEqual("test", test);
}

public static IEnumerable<object[]> TestMethodInput
{
    get
    {
        return new[]
        {
            new object[] { stringproperty },
            new object[] { "test" }
        };
    }
}

这篇关于如何在MSTest DataRow中将属性作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆