如何使用MTM将参数值从TFS中的测试用例传递到单元测试方法中的测试方法?

How to pass parameter values from test case in TFS to test method in unit test method using MTM?

我想传递 Team Foundation Server 中存在的测试用例的参数值。我在 Microsoft 测试管理器的帮助下进行自动化。

下面是使用单元测试项目创建的示例测试方法。

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]

        public void TestMethod1(int a, int b, int expectedResult)
        {

            var sut = new Class1();

            var result = sut.Add(a,b);

            Assert.AreEqual(expectedResult, result);

        }
    }
}

现在,当我尝试构建它时,出现以下错误:

UTA007: Method TestMethod1 defined in class UnitTestProject1.UnitTest1 does not have correct signature. Test method marked with the [TestMethod] attribute must be non-static, public, does not return a value and should not take any parameter. for example: public void Test.Class1.Test(). Additionally, return-type must be Task if you are running async unit tests. Example: public async Task Test.Class1.Test2().

该场景如何实现传参?

要从 TFS 中的 TestCase 读取参数值,您可以使用数据驱动单元测试:

public TestContext TestContext { get; set; }
public DataRow DataRow { get; set; }

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.TestCase", "http://serverName:8080/tfs/MyCollection;teamProjectName", "541", DataAccessMethod.Sequential)]

[TestMethod]
public void TestMethod()
{
            string parameter1 = TestContext.DataRow[0].ToString(); // read parameter by column index
            string parameter2 = TestContext.DataRow[1].ToString(); 

            var sut = new Class1();

            var result = sut.Add(a, b);

            Assert.AreEqual(parameter1, result);
}

注意:541 是 TestCase id。