1

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?

meshsf
  • 143
  • 1
  • 14
  • 1
    Possible duplicate of [How to run a test method with multiple parameters in MSTest?](http://stackoverflow.com/questions/9021881/how-to-run-a-test-method-with-multiple-parameters-in-mstest) – ivayle Mar 01 '17 at 06:42
  • I would suggest you use `TestContext` instead? https://blogs.msdn.microsoft.com/vstsqualitytools/2006/01/09/using-testcontext-in-unit-tests/ – zaitsman Mar 01 '17 at 09:55

1 Answers1

2

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.

Tingting0929
  • 4,052
  • 1
  • 12
  • 14
  • I have setup my TFS online. So is this path correct? [DataSource("Microsoft.VisualStudio.TestTools.DataSource.TestCase", "http://abc.visualstudio.com/MyFirstProject;unittestproject", "15", DataAccessMethod.Sequential)] I tried this but getting error - The unit test adapter failed to connect to the data source or to read the data. – meshsf Mar 02 '17 at 13:13
  • I myself found the answer. This works fine .[DataSource("Microsoft.VisualStudio.TestTools.DataSource.Tes‌​tCase", "https://abc.visualstudio.com;MyFirstProject, "15", DataAccessMethod.Sequential)] – meshsf Mar 06 '17 at 08:57
  • How to pass XML file (which will have test data) as test parameter for each test case in TFS?Something like this [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "https://abc.visualstudio.com;MyFirstProject", "TestData.xml", DataAccessMethod.Sequential)] is this possible? – meshsf Mar 15 '17 at 04:51
  • Sorry, I didn't understand your requirement. Could you please post a new question and give your request a detail description. – Tingting0929 Mar 15 '17 at 09:40
  • Sure. I will do and update here once post it. So that you can have a look. Thanks. – meshsf Mar 15 '17 at 11:11
  • I have posted new question here http://stackoverflow.com/questions/42827835/how-to-read-xml-attachment-from-tfs-test-case-to-test-method. – meshsf Mar 16 '17 at 07:33