简体   繁体   中英

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

I would like to pass parameter values from test cases which are present in Team Foundation Server. I do the automation with the help of Microsoft Test Manager.

Below is example test method created using Unit Test Project.

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);

        }
    }
}

Now when I try to build this, I get the below error:

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().

How to achieve parameter passing in this scenario?

To read parameter values from a TestCase in TFS, you could use Data-Driven unit testing:

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);
}

Note: 541 is the TestCase id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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