我应该使用“玻璃盒"吗?何时导致*更少*测试? [英] Should I use "glass box" testing when it leads to *fewer* tests?

查看:105
本文介绍了我应该使用“玻璃盒"吗?何时导致*更少*测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我正在针对CsvReader编写测试.这是一个简单的类,它枚举和拆分文本行.它唯一的 raison d'être忽略了引号内的逗号.不到一页.

For example, I'm writing tests against a CsvReader. It's a simple class that enumerates and splits rows of text. Its only raison d'être is ignoring commas within quotes. It's less than a page.

通过黑匣子"测试课程,我已经检查了类似的内容

By "black box" testing the class, I've checked things like

  • 如果文件不存在怎么办?
  • 如果我没有该文件的权限怎么办?
  • 如果文件具有非Windows换行符怎么办?

但是实际上,所有这些都是StreamReader的业务.我的课堂上没有任何处理这些情况的工作.因此,从本质上讲,我的测试正在捕获StreamReader引发的错误,并测试框架处理的行为.感觉像是花了很多功夫.

But in fact, all of these things are the StreamReader's business. My class works without doing anything about these cases. So in essence, my tests are catching errors thrown by StreamReader, and testing behavior handled by the framework. It feels like a lot of work for nothing.

我已经看到了相关问题

  • Should QA test from a strictly black-box perspective?
  • Rigor in capturing test cases for unit testing

我的问题是,如果我使用我知道的东西来避免这种工作,我是否错过了玻璃箱"测试的重点?

My question is, am I missing the point of "glass box" testing if I use what I know to avoid this kind of work?

推荐答案

这实际上取决于CsvReader的界面,您需要考虑类用户的期望.

This really depends on the interface of your CsvReader, you need to consider what the user of the class is expecting.

例如,如果参数之一是文件名而该文件不存在,应该怎么办?这不应取决于您是否使用流阅读器.单元测试应该测试类的可观察的外部行为,并且在某些情况下,应进行更深入的挖掘,并确保涵盖某些实现细节,例如阅读器完成后,文件将关闭.

For example, if one of the parameters is a file name and the file does not exist what should happen? This should not be dependent upon whether you use a stream reader or not. The unit tests should test for the observable external behaviour of your class and in some cases, to dig slightly deeper and additionally ensure certain implementation details are covered, e.g. the file is closed when the reader has finished.

但是,您不希望单元测试依赖于所有细节,也不希望由于实施细节而导致某些事情发生.

However you don't want the Unit Tests to be dependent upon all of the details or to assume that because of an implementation detail something will happen.

您在问题中提到的所有示例都涉及您班级的可观察到的行为(在这种情况下为特殊情况),因此应该具有与之相关的单元测试.

All of the examples you mention in your question involve observable behaviour (in this case exceptional circumstances) of your class and therefore should have unit tests related to them.

这篇关于我应该使用“玻璃盒"吗?何时导致*更少*测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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