开发者

Create Unit test methods dynamically during runtime in MSTest

开发者 https://www.devze.com 2022-12-12 20:42 出处:网络
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

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 suggest开发者_开发技巧ions?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消