将相同的测试方法关联到 VSTest 运行 XUnit 测试中的多个测试用例时,不会执行测试方法

Test methods are not executed when associating same test method to multiple test cases in VSTest running XUnit tests

我们有一个 ASP.Net 应用程序和 UI 测试是用 xUnit 编写的。测试计划在 VSTS 中,在某些情况下,相同的 xUnit 测试方法与多个测试用例相关联。

Azure 构建管道使用 VSTest 执行这些测试。问题是当多个测试用例与一个方法相关联时,似乎只执行了其中一个。例如。以下屏幕截图中的测试用例与同一方法相关联,并且只执行了一个。

我们在 Vstest 中尝试了 'Test assemblies' 和 'Test Plan' 选项,但结果是一样的。

根据以下 link,在 xUnit 中不可能在同一测试会话中多次 运行 相同的测试方法。

https://developercommunity.visualstudio.com/content/problem/995269/executing-multiple-test-cases-from-testplan-which.html?childToView=995554#comment-995554

我能想到的一些解决方案是,

  1. 为所有测试用例创建虚拟测试方法并维护一对一的测试方法到测试用例的映射。其中一种方法将具有测试逻辑,而其他方法将断言为真。
  2. 创建多个测试方法,其中只有一个方法包含实现。其他方法只会调用包含实现的测试方法。

如果有更好的解决方法请指教

提前致谢!

Test methods are not executed when associating same test method to multiple test cases in VSTest running XUnit tests

据我们所知:

By default, each test class is a unique test collection. Tests within the same test class will not run in parallel against each other.

因此,我们从 Azure Devops Developer Community forum and xunit 得到的响应是 XUnit 不允许 运行 在同一会话中多次测试。

我个人认为您提出的两种解决方法是正确的。您可以使用两种方法中的一种来解决此问题。你已经走对了。

希望对您有所帮助。