简体   繁体   English

NUnit:如何为三个函数编写一种通用测试方法?

[英]NUnit: How to write one generic test method for three functions?

I'm using NUnit.我正在使用 NUnit。 Here is my requirement:这是我的要求:

Let's say there are 3 functions namely add() , sub() and mul() .假设有 3 个函数,即add()sub()mul() How to write just one generic test method in Nunit?如何在 Nunit 中只编写一种通用测试方法?

So depending on the function type the generic test method execute the tests.因此,根据函数类型,通用测试方法执行测试。

Means if I pass add() to generic test method it should perform the test for add() and so on.意味着如果我将add()传递给通用测试方法,它应该执行add()等的测试。 I do not want to write 3 separate test methods for 3 methods.我不想为 3 个方法编写 3 个单独的测试方法。

I just want to accomplish this using just one generic test method.我只想使用一种通用测试方法来完成此操作。 Is this possible?这可能吗?

It seems that you don't understand what good unit tests are about.看来你不明白什么是好的单元测试。

The intention of a unit test is to help you to quickly identify the root cause of failure, in order to fix it as quickly as possible.单元测试的目的是帮助您快速确定失败的根本原因,以便尽快修复它。

That starts with the simple fact that you have one test for each "different behavior" you intend to test.首先是一个简单的事实,即您对要测试的每种“不同行为”都有一个测试。 In other words: you definitely go for one test method for each of your 3 test methods.换句话说:你肯定去为您的每个3种的一种测试方法的测试方法。

You might even go for multiple test methods for those 3 methods;您甚至可以为这 3 种方法选择多种测试方法; because "adding to empty" could be a different thing then "adding to non-empty" for example.因为例如“添加到空”可能与“添加到非空”不同。

Probably you want to have some "integration" level test that really uses all three of your methods in one test case;可能你想要一些“集成”级别的测试,在一个测试用例中真正使用你的所有三种方法; just to make sure that the overall "plumbing" works out in the end.只是为了确保整个“管道”最终都能正常工作。 But from a unit test level, you make sure that those three methods work as expected on a per method level.但是从单元测试级别,您要确保这三种方法在每个方法级别按预期工作。 You know, when you really verified that add() works in all possible scenarios;您知道,当您真正验证add() 在所有可能的情况下都有效时; there is no more special need to test it in conjunction with those other methods!没有更多的特殊需要结合那些其他方法来测试它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM