在 MSTest 中运行时动态创建单元测试方法

发布于 2024-08-13 03:31:04 字数 370 浏览 1 评论 0原文

MSTest 中是否有相当于 SuiteBuilder 的工具?到目前为止还没有找到。

我有一堆 xml 文件,每个文件都被视为映射到一个测试方法。由于有数百个这样的项目,并且为每个项目手动编写测试,这不是一个好主意。

因此,在 nunit 中,您可以实现 ISuiteBuilder 并让测试用例动态运行并显示为许多测试方法。

我正在寻找一种方法在 MSTest 中做同样的事情。

我查看了 DataSource 属性,但它满足每个测试方法 1 个数据源 xml 文件/csv,迫使我编写 100 个测试方法。我还想将每个 xml 文件分开,不要将它们全部合并到 1 个大文件中,在这种情况下它将变得无法维护。

有人尝试过这个或者有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2024-08-20 03:31:04

不完全符合您的要求,但您可以使用 pex 用于自动化和可参数化的白盒测试。这样,您就不需要手动完成所有这些事情。 Pex 支持 MSTest 以及 NUnit。生成的测试使用额外的文件,您不需要任何 xml 文件。

但我认为您无法轻松地使用 NUnit 中现有的 .xml 文件并使用 pex 与 MSTest 共享它们 - 如果这就是您的意图。

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.

醉生梦死 2024-08-20 03:31:04

我已经这样做了。以下是您需要执行的操作:

测试:

[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 和 test2.xml 必须存在于 MyTestData 目录中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文