是否有使用 NUnit 测试复杂函数的通用方法?

发布于 2024-10-18 20:30:02 字数 136 浏览 2 评论 0原文

有没有一种通用方法可以使用 NUnit 测试具有多个参数的复杂函数?我认为测试每个条件是非常困难或不可能的。

恐怕函数中未预期的参数组合在测试中也不是预期的。

所以预期的情况不会失败,而是意外的情况发生。

谢谢

is there a common way to test complex functions with several parameters with NUnit? I think it is very hard or impossible to test every condition.

I'm afraid the combination of parameters that isn't expected in the function is also not expected in the test.

So the expected condition will not fail but the unexpected.

Thanks

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

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

发布评论

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

评论(5

安静被遗忘 2024-10-25 20:30:02

这应该不难测试。如果是,则该方法不是为可测试性而设计的,这是一种代码味道,告诉您需要重构它。

我倾向于在这些情况下编写测试如下(其他人可能有更好的建议):

  1. 当传递所有适当的参数时,它是否按预期工作?
  2. 当我认为应该抛出预期的异常时,它是否会抛出预期的异常? (ArgumentNullException 等)
  3. 对于每个参数,当我传递 null、最小值和最大值时会发生什么。 (这可能非常广泛,具体取决于参数的数量。)

如果您的方法采用大量参数,请考虑重构它以采用带有信息的对象,以便您可以将其规则封装在对象中,并将对象传递给方法。

This shouldn't be hard to test at all. If it is, the method isn't designed for testability, and that is a code smell that tells you that you need to refactor it.

I tend to write tests in these cases as follows (others may have better suggestions):

  1. Does it work as intended when all appropriate parameters are passed?
  2. Does it throw expected exceptions when I think it should? (ArgumentNullException, etc.)
  3. For each parameter, what happens when I pass null, the minimum and the maximum. (This can be very extensive, depending on the number of arguments.)

If your method takes a lot of parameters, consider refactoring it to take an object with the information on it, so that you can encapsulate the rules for it in the object, and pass the object to the method.

乱了心跳 2024-10-25 20:30:02

对于 NUnit 中的数据驱动测试,有 [TestCase] 属性。单元测试通常不会测试所有可能的场景。他们只是测试代表性的输入集,这些输入集很好地覆盖了 SUT 对各种输入的操作。只需选择一些特征输入,就可以了。

For data-driven tests in NUnit, there is [TestCase] attribute. Unit tests usually dont't test every possible scenario. They just test representative set of inputs, which have good coverage of what the SUT does on various inputs. Just pick some characteristic inputs, and you'll be fine.

青芜 2024-10-25 20:30:02

不知道这是否是您正在寻找的东西,但是有一个由 Microsoft Research 创建的自动化单元测试生成器,名为 PEX

Pex 自动生成具有高代码覆盖率的测试套件。 Pex 直接从 Visual Studio 代码编辑器中找到方法的有趣输入输出值,您可以将其保存为具有高代码覆盖率的小型测试套件。 Microsoft Pex 是一个用于测试 .NET Framework 应用程序的 Visual Studio 插件。

Don't know if this is the kind of thing you are looking for, but there is an automated unit test generator that was created by Microsoft research called PEX.

Pex automatically generates test suites with high code coverage. Right from the Visual Studio code editor, Pex finds interesting input-output values of your methods, which you can save as a small test suite with high code coverage. Microsoft Pex is a Visual Studio add-in for testing .NET Framework applications.

夜司空 2024-10-25 20:30:02

使用RowTest类似的问题可以在

C#, NUnit Assert in 找到a Loop

看看@“Sam Holder”的回复,我从中复制了代码,做了一些调整。

[TestFixture] 
    public class TestExample 
    {      

    [RowTest]      
    [Row( 1)]      
    [Row( 2)]      
    [Row( 3)]      
    [Row( 4)]      

    public void TestMethodExample(int value)      
    {           
              ...
              ...
              ...
              Assert.IsTrue("some condition ..");
    } 
 } 

Use RowTest similar question can be found at

C#, NUnit Assert in a Loop

have a look at @"Sam Holder" reply, I copied the code from it, with few tweaks.

[TestFixture] 
    public class TestExample 
    {      

    [RowTest]      
    [Row( 1)]      
    [Row( 2)]      
    [Row( 3)]      
    [Row( 4)]      

    public void TestMethodExample(int value)      
    {           
              ...
              ...
              ...
              Assert.IsTrue("some condition ..");
    } 
 } 
燃情 2024-10-25 20:30:02

我同意 Mike Hofer 的观点,这个问题表明了代码的味道。

尽管如此,NUnit 有一个组合属性,如果你没有重构/重新设计。

I agree with Mike Hofer, that the question indicates a code smell.

Nevertheless, NUnit has a Combinatorial attribute that might help you, if you're not refactoring/redesigning.

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