简体   繁体   English

单元测试背后的“意图” - 我应该瞄准什么?

[英]The “intent” behind unit tests - what should I be aiming for?

I wrote a series of tests today around a method that takes an input value and returns a different array of data depending whether the value passes some (internal) validation test, ie 我今天围绕一个方法编写了一系列测试,该方法接受输入值并返回不同的数据数组,具体取决于值是否通过某些(内部)验证测试,即

[TestMethod] 
public void IsValidForValueFour()
{
    var result = myComponent.Validator(4);
    Assert.IsTrue(result[0], "Blah");
}

The Validator() method basically does a lookup in a (hardcoded) table stored privately in myComponent . Validator()方法基本上在myComponent私有存储的(硬编码)表中进行查找。

This felt wrong. 感觉不对。 I was effectively testing the values in a private lookup table. 我正在有效地测试私有查找表中的值。 Should I care about the values passed in and out? 我应该关心传入和传出的值吗? Should I be testing the length of the output array rather than its contents? 我应该测试输出数组的长度而不是其内容吗? Or is it right to test for specific responses given some input value? 或者在给定一些输入值的情况下测试特定响应是否正确?

In short, what should the intent be behind a unit test like this? 简而言之,这样的单元测试背后的意图应该是什么?

This is valuable, as you now know that the function is returning what it should. 这很有价值,因为您现在知道函数正在返回它应该的东西。

In the future, if you change implementation, you can rest sure that it still does what it should, so long as the tests all pass. 将来,只要测试全部通过,如果您更改实现,您可以确保它仍然可以执行它应该执行的操作。


As for should you test such things? 至于你应该测试这样的东西吗? It is up to you how much testing you want to preform and the value you believe you are getting from such tests. 由您决定要进行多少测试以及您认为从这些测试中获得的价值取决于您。

A unit test should test a small piece of code, like one object or even one method and should look for ways the method/class might break. 单元测试应该测试一小段代码,比如一个对象甚至一个方法,并且应该寻找方法/类可能会破坏的方法。 IMHO, A unit test which never breaks may as well not exist. 恕我直言,一个永不休息的单元测试也可能不存在。 ;) ;)

如果你的方法的合同是它返回"Blah"输入4 ,那么是的,你应该测试它!

The the contract for MyComponent.Validator(4) is that it returns an array whose first element is true , then this test is useful. MyComponent.Validator(4)的契约是它返回一个第一个元素为true的数组,然后这个测试很有用。 However, it really depends on how complicated the logic behind that method is. 但是,它实际上取决于该方法背后的逻辑有多复杂。 One of the rules I follow is that by writing short, simple methods you can skip a lot of the testing: if something is so simple it can be visually verified, it's often not worth testing. 我遵循的规则之一是,通过编写简短的方法,您可以跳过大量的测试:如果某些事情如此简单,可以通过视觉验证,通常不值得测试。

If Validator() accepts two hundred valid integers and returns a unique result from an internal array for each one, I wouldn't bother writing two hundred tests. 如果Validator()接受200个有效整数并从每个内部数组返回一个唯一的结果,那么我就不会费心去编写200个测试。 I would write tests for the endpoint values and move on. 我会为端点值编写测试并继续。

You should be testing it by passing in many, many valid pieces of data, and even more invalid pieces of data to make sure it works like its supposed to. 你应该通过传递许多有效的数据,甚至更多的无效数据来测试它,以确保它像它应该的那样工作。 That's the entire point. 这就是重点。 Your method must do what it's supposed to do, otherwise it needs to be reworked. 你的方法必须做它应该做的事情,否则它需要重做。 That way the consumer of this information (either you or possibly another developer), can work with that data without being concerned about the internal implementation of your method. 这样,此信息的使用者(您或可能是其他开发人员)可以使用该数据,而无需担心方法的内部实现。

In fact, if you have a paragraph of comments that contains text from a novel your reading (which is silly, I admit), but your method accepts and returns what it says it should, then the consumer will never know or care how you implemented that method. 事实上,如果你有一段评论包含你的阅读小说中的文字(这是愚蠢的,我承认),但你的方法接受并返回它应该的内容,那么消费者永远不会知道或关心你如何实施那种方法。

If it is important from the requirements point of view, you should test this. 如果从需求的角度来看很重要,那么您应该对此进行测试。 If it is important that value four must return “Blah” then you have valid test – But I would also certainly extract number 4 into a meaningful variable name and pass it into validator. 如果重要的是值四必须返回“Blah”然后你有有效的测试 - 但我也肯定会将数字4提取到一个有意义的变量名称并将其传递给验证器。 So whoever read your test will know why you pass value four. 所以无论谁读过你的测试都会知道为什么你通过了四个值。 If you are testing some kind of an algorithm try to use a tool like Pex to generate input for you. 如果您正在测试某种算法,请尝试使用像Pex这样的工具为您生成输入。 This will also ensure your logic within the SUD (System Under Test) behave as expected. 这也将确保您在SUD(被测系统)中的逻辑按预期运行。

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

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