Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Unit test methods dynamically during runtime in MSTest

Is there an equivalent of SuiteBuilder in MSTest? couldn't find one so far.

I have a bunch of xml files, each to be seen as mapped to a test method. As there are 100s of these and to manually write tests for each of these, is not a good idea.

So in nunit you could implement ISuiteBuilder and have the Test cases run dynamically and show up as those many test methods.

I am looking for a way to do the same thing in MSTest.

I've looked at DataSource attribute, but it caters to 1 datasource xml file/csv per test method, forcing me to write 100s of test methods. I also want to keep each xml file separate and don't club them all in to 1 huge file, in which case it would become unmaintainable.

Has someone tried this or has any suggestions?

like image 523
Vin Avatar asked Dec 02 '09 00:12

Vin


2 Answers

Not exactly what you asked for, but you can use pex for automated and parametrizable white box tests. In that way, you dont need to manually do all that stuff. Pex supports MSTest as well as NUnit. Generated Tests use an extra file, you don't need any xml files.

But I think you can't easily use your existing .xml files from NUnit and share them with MSTest using pex - if that is what you intended.

like image 189
mbx Avatar answered Sep 23 '22 16:09

mbx


I have done this already. Here is what you would need to do:

The Test:

[TestMethod]
[DeploymentItem("MyTestData")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
                   "|DataDirectory|\\MyTestData.xml",
                   "Test",
                    DataAccessMethod.Sequential)]
public void MyTest()
{
    string file = TestContext.DataRow[0].ToString();
    string expectedResult = TestContext.DataRow[1].ToString();
        // TODO: Test something
}

MyTestData.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Rows>
  <Test>
    <File>test1.xml</File>
    <Result>1</Result>
  </Test>
  <Test>
    <File>test2.xml</File>
    <Result>2</Result>
  </Test>
</Rows>

test1.xml and test2.xml must exist in the MyTestData directory.

like image 2
frast Avatar answered Sep 23 '22 16:09

frast