在MSTest运行时动态创建单元测试方法 [英] Create Unit test methods dynamically during runtime in MSTest
问题描述
MSTest中是否有SuiteBuilder的等效项?到目前为止找不到一个.
Is there an equivalent of SuiteBuilder in MSTest? couldn't find one so far.
我有一堆xml文件,每个文件都被视为映射到一种测试方法.由于其中有100多个,并且要为每个手工编写测试,所以不是一个好主意.
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.
因此,在nunit中,您可以实现ISuiteBuilder并使测试用例动态运行并显示为许多测试方法.
So in nunit you could implement ISuiteBuilder and have the Test cases run dynamically and show up as those many test methods.
我正在寻找一种在MSTest中执行相同操作的方法.
I am looking for a way to do the same thing in MSTest.
我已经看过DataSource属性,但是每个测试方法只能满足1个数据源xml文件/csv的要求,这迫使我编写100多个测试方法.我还想将每个xml文件分开,不要将它们全部合并为1个大文件,在这种情况下,它将变得难以维护.
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?
推荐答案
我已经做到了.这是您需要做的:
I have done this already. Here is what you would need to do:
测试:
[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:
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和test2.xml必须存在于MyTestData目录中.
test1.xml and test2.xml must exist in the MyTestData directory.
这篇关于在MSTest运行时动态创建单元测试方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!