是否有用于以编程方式运行 Visual Studio 单元测试的 API?

发布于 2024-08-17 20:18:20 字数 163 浏览 1 评论 0原文

是否有用于以编程方式运行 Visual Studio 单元测试的 API?

使用 Process.Start() 运行 MSTests.exe 在当前情况下不起作用。我正在寻找类似 NUnit SimpleTestRunner 的东西。

有什么想法吗?

/埃里克

Is there an API for running Visual Studio Unit Tests programmatically?

Running MSTests.exe with Process.Start() does not work in the current scenario. What I'm looking for is something like the NUnit SimpleTestRunner.

Any ideas?

/Erik

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

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

发布评论

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

评论(4

轻拂→两袖风尘 2024-08-24 20:18:20

您是对的,mstest 框架没有公共 API。有一天,我编写了 mstest 的手动替代品,看看它有多难,而且它并不像看起来那么简单(特别是如果您想利用多个 CPU 核心),因此请小心不要走这条路。

就我个人而言,我总是以编程方式运行 mstest.exe,然后解析生成的 .trx XML 文件。是否有任何特殊原因导致您无法使用 Process.Start 来运行它?

PS 如果您传递 /noisolation 命令行参数,mstest.exe 的一些奇怪行为就可以解决 - 如果您愿意,请尝试一下:-)


更新:Erik 提到他想运行在当前线程中测试 API,以便他可以设置全球化问题的线程文化。

如果您在调试器下运行单元测试,您会注意到 mstest 创建了一堆线程,并在不同的线程中运行所有测试,因此即使您可以访问 API,这也不太可能起作用。

我建议这样做:

  1. 从您的测试“运行程序”应用程序中,设置一个环境变量
  2. Run mstest 将其指向特定测试
  3. 添加 [ClassInitialize] (或 [TestInitialize]< /code>) 读取此环境变量并设置文化的方法
  4. Profit!

You're correct in that there's no public API for the mstest framework. I wrote a manual replacement for mstest one day to see how hard it was, and it's not as simple as it looks (particularly if you want to take advantage of more than one CPU core), so beware of going down this path.

Personally I've always just run mstest.exe programatically and then parsed the resulting .trx XML file. Are there any particular reasons why you can't use Process.Start to run it?

P.S. Some of the strange behaviour of mstest.exe are solved if you pass the /noisolation command line parameter - give that a go if you feel so inclined :-)


Update: Erik mentions he wants to run the test API in the current thread so he can set the thread culture for globalization issues.

If you run a unit test under the debugger, you'll notice that mstest creates a bunch of threads, and runs all your tests in different threads, so this isn't likely to work even if you could access the API.

What I'd suggest doing is this:

  1. From your test "runner" application, set an environment variable
  2. Run mstest pointing it at the specific tests
  3. Add a [ClassInitialize] (or [TestInitialize]) method which reads this environment variable and sets the culture
  4. Profit!
趁年轻赶紧闹 2024-08-24 20:18:20

在使用反射器深入研究 MSTest.exe 并进一步深入 Visual Studio 单元测试堆栈后,我发现 MSTest 使用的 API 被密封并设为私有,因此无法从外部使用。

After taking a deep dive with reflector into MSTest.exe and further down into the Visual Studio Unit Test stack, I found that the API used by MSTest is sealed up and made private so that i cannot be used from the outside.

灰色世界里的红玫瑰 2024-08-24 20:18:20

为什么不使用 Reflector 并查看 NUnit SimpleTestRunner 如何运行测试...然后使用此技术...

Why not using Reflector and seeing how NUnit SimpleTestRunner is running the tests... And then use this technique...

长梦不多时 2024-08-24 20:18:20

您可以利用 Microsoft REST API for TFS 来运行 ms 测试。请参阅此处的文档。

我已链接到“调用 Rest API”,以便您可以了解如何为 TFS 调用 REST API 之一。

请注意,如果您的测试链接到构建,则每次构建排队时它们都应该自动运行。

以下是运行功能测试的链接

我还发现了一篇有关使用 TFS SDK API 运行测试的文章。这也是该链接: API 文章链接

You can make use of the Microsoft REST API's for TFS to run ms tests. Please refer to the documentation here.

I've linked to "Call a Rest API" so that you can see how you'd go about calling one of the REST API's for TFS.

Note that if your tests are linked to the build, they should run automatically every time a build is queued.

Here is the link to Run Functional Tests.

I've also discovered an article on using the TFS SDK API to run tests. Here is that link as well: Link to API Article

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