Visual Studio为什么不运行具有方法调用的测试?

huangapple go评论65阅读模式
英文:

Why Visual Studio doesn't run tests that have a method call?

问题

我建立了一个 NUnit 测试的测试用例,每次运行测试时,有一些测试被跳过。没有错误,没有消息...只是没有运行,尽管测试显示为已完成。我有 625 个测试用例,但只运行了 81。

测试资源管理器

我的测试用例包含 5 个字符串和 1 个布尔值,这些字符串是由一个方法生成的。

测试用例之前

随机字符串方法

经过一些调查,我尝试插入先前生成的字符串而不是调用方法(下图),它奏效了。

测试用例之后

所以我在想,为什么我不能调用方法来创建字符串运行测试呢?

我尝试更新 Visual Studio、NUnit...都不起作用,而方法创建字符串的测试用例正常运行。

英文:

I've built a test case for a NUnit test and every time i ran the tests, some were skipped. No error, no message... just didn't run, even though the test appear as complete. I have 625 cases but it only runs 81.

Test Explorer

My test case consistis in 5 string and 1 boolean, and the strings are generated by a method.

Test Case Before

Random String Method

After some investigations, i tried inserting the string previously generated instead of calling the methods(below) and it worked.

Test Case Aafter

So i was wandering, why i can't run the tests calling the method to create the string?

I tried updating Visual Studio, NUnit... Neither worked while the method to create the string was on the test case.

答案1

得分: 1

有关VS运行测试和NUnit期望运行测试的不匹配,这主要在NUnit3的VS适配器中处理...因为这就是适配器的作用。 Visual Studio为什么不运行具有方法调用的测试?

其中一部分"适配"的过程是NUnit测试会被发现两次...一次是在VS发现阶段,一次是在执行测试时。就像这样...

VS发现 => NUnit发现

VS执行 => NUnit发现 => NUnit执行

大多数情况下,这是正常工作的。但是,当测试用例的发现涉及到随机生成数据时,它会发生两次,导致测试用例发生变化。

NUnit设计用于测试的可重复性,适配器会利用它。因此,如果您使用类似于RandomAttribute来生成值,那么在发现和执行阶段将生成相同的值。这是因为适配器在VS发现阶段保存了随机种子,并在执行阶段重复使用它。

但是,如果用户在他们自己的代码中生成随机数据,情况就不同了。该代码不知道NUnit对测试可重复性的要求。解决这个问题的方法是避免使用Random类,并使用NUnit的内部随机工具。

Randomizer在NUnit内部使用,但也可供您自己使用。您可以在以下两种方式之一中使用它:

  1. 只需使用它,而不是继承自Random的Randomizer。一切都会正常工作,但您将在两次发现过程中获得可重复性。

  2. 尝试使用Randomizer.GetString(),它会为您生成字符串。这是我会使用的方式。

在任何情况下,都不应该创建自己的Randomizer实例。相反,使用由NUnit创建的实例,在执行阶段以可重复的方式重新创建。它可以通过TestContext.CurrentContext.Randomizer获得。

英文:

There is a mismatch between how VS runs tests and how NUnit expects to run them. Mostly this is taken care of in the NUnit3 VS adapter... 'cause that's what adapters do. Visual Studio为什么不运行具有方法调用的测试?

Part of the "adapting" that happens is that NUnit tests are discovered twice... once when VS discovery runs and once when the test is executed. Like this...

VS Discovery => NUnit Discovery

VS Execution => NUnit Discovery => NUnit Execution

Mostly, this works fine. But when test case discovery involves random generation of data, it happens twice, causing the test cases to change.

NUnit is designed for repeatability of tests and the adapter makes use of it. So if you use something like the RandomAttribute to generate values, the same values will be generated for both discovery and execution. That's because the adapter saves the random seed in the VS discovery phase and re-uses it in the execution phase.

But if the user generates random data in their own code, that's another matter. That code doesn't know about NUnit's requirements for repeatability. The way around this is to avoid use of the Random class and to use NUnit's internal random facilities.

The class Randomizer is used by NUnit internally but is also available for your own use. You could make use of it in your case in one of two ways:

  1. Just use it instead of Random, from which it inherits. Everything will work the same but you'll get repeatability across the two discovery passes.

  2. Experiment with Randomizer.GetString(), which will generate strings for you. That's what I would use.

In either case, you should NOT create your own instance of Randomizer. Instead, use the instance created by NUnit, which will be re-created in a repeatable way during the execution phase. It's available as TestContext.CurrentContext.Randomizer.

huangapple
  • 本文由 发表于 2023年5月11日 03:51:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定