我应该使用不应在函数中传递的数据(无效输入)进行单元测试吗? [英] Should I unit-test with data that should not be passed in a function (invalid input)?

查看:15
本文介绍了我应该使用不应在函数中传递的数据(无效输入)进行单元测试吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 TDD 用于我的编码实践.我想问一下我是否应该使用不应出现在函数中的数据进行测试,但这些数据可能会破坏您的程序.

I am trying to use TDD for my coding practice. I would like to ask should I test with a data that should not happen in a function BUT this data may possibly break your program.

这是一个简单的例子来说明我的要求:

Here is one of a easy example to illustrate to what I ask :

具有一个 INT 参数的 ROBOT 函数.在此函数中,我知道有效范围仅为 0-100.如果使用-1, 101,则函数会中断.

a ROBOT function that has a one INT parameter. In this function I know that the valid range would only be 0-100. If -1, 101 is used, the function will be break.

function ROBOT (int num){
...
...
...
return result;
}

所以我为此功能决定了一些自动化测试用例...

So I decided some automated test cases for this function...

1. function ROBOT with input argument 0
2. function ROBOT with input argument 1
3. function ROBOT with input argument 10
4. function ROBOT with input argument 100

但是我应该为这个 ROBOT 函数编写带有输入参数 -1 或 101 的测试用例,如果我会在调用函数 ROBOT 的另一个函数中保护它???

But should I write test cases with input argument -1 or 101 for this ROBOT function IF I would guard that in my other function that call function ROBOT???

5. function ROBOT with input argument -1
6. function ROBOT with input argument 101

我不知道是否有必要,因为我认为测试-1和101是多余的.如果真的有必要覆盖所有情况,我必须编写更多代码来保护-1和101.

I don't know if it is necessary cause I think it is redundancy to test -1 and 101. And If it is really necessary to cover all the cases, I have to write more code to guard -1 and 101.

那么在 TDD 的常见实践中,您是否也会在 -1 和 101 上编写测试用例???

So in Common practice of TDD, will you write test case on -1 and 101 as well???

推荐答案

总之,如果它可以坏,那么你应该测试它.还要尽早验证数据.

In short, if it can break, then you should test it. Also validate data at the earliest point possible.

答案取决于您是否控制传递给 Robot 的输入.如果 Robot 是内部类(C#);值仅从作为公共类型的 RobotClientX 流入.然后我把守卫检查放在 RobotClientX 中,为它编写测试.我不会为 Robot 编写测试,因为无效值不能在两者之间实现.例如如果我将验证放在 GUI 中,以便在源头过滤掉所有无效值,那么我不会检查 GUI 下所有类中的无效值(除非我还公开了一个绕过 GUI 的公共 API).

The answer depends on whether you control the inputs passed to Robot. If Robot is an internal class (C#) ; values only flow in from RobotClientX which is a public type. Then I'd put the guard checks in RobotClientX, write tests for it. I'd not write tests for Robot, because invalid values cannot materialize in-between. e.g. if I put my validations in the GUI such that all invalid values are filtered off at the source, then I don't check for invalid values in all classes below the GUI (Unless I've also exposed a public API which bypasses the GUI).

另一方面,如果 Robot 是公开可见的,即任何人都可以用他们喜欢的任何值调用 Robot,那么我需要测试记录它在给定特定类型输入的情况下的行为......无效是其中之一.例如如果你传递一个超出范围的值,它会抛出一个 ArgumentException.

On the other hand, if Robot is publicly visible i.e. Anyone can call Robot with any value that they please, then I need tests that document it's behavior given specific kinds of input.. invalid being one of them. e.g. if you pass an out-of-range value, it'd throw an ArgumentException.

这篇关于我应该使用不应在函数中传递的数据(无效输入)进行单元测试吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆