严格捕获用于单元测试的测试用例 [英] Rigor in capturing test cases for unit testing

查看:99
本文介绍了严格捕获用于单元测试的测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个用伪语言定义的简单函数.

Let's say we have a simple function defined in a pseudo language.

List<Numbers> SortNumbers(List<Numbers> unsorted, bool ascending);

我们传入一个未排序的数字列表和一个布尔值,指定升序或降序排序.作为回报,我们得到一个排序的数字列表.

We pass in an unsorted list of numbers and a boolean specifying ascending or descending sort order. In return, we get a sorted list of numbers.

根据我的经验,有些人比其他人更擅长捕捉边界条件.问题是,您怎么知道何时'完成'捕获测试用例"?

In my experience, some people are better at capturing boundary conditions than others. The question is, "How do you know when you are 'done' capturing test cases"?

我们现在就可以开始列出案例,毫无疑问,一些聪明的人会想到以前的任何案例都没有涉及的另一个"案例.

We can start listing cases now and some clever person will undoubtedly think of 'one more' case that isn't covered by any of the previous.

推荐答案

不要浪费太多时间来思考每一个的约束条件.您的测试将无法在第一时间捕获每个错误.这个想法是要进行非常好的测试,然后每次出现 错误时,都专门为该错误编写一个新的测试,这样您就永远不会再听到它了

Don't waste too much time trying to think of every boundry condition. Your tests won't be able to catch every bug first time around. The idea is to have tests that are pretty good, and then each time a bug does surface, write a new test specifically for that bug so that you never hear from it again.

我想对代码覆盖率工具做一个补充说明.在C#或Java这样的语言中,您有很多获取/设置方法和类似方法,因此您应该拍摄100%的覆盖率.这意味着您浪费了太多时间来编写琐碎的代码测试.您只希望100%覆盖复杂的业务逻辑.如果您的完整代码库覆盖率接近70-80%,则说明您做得很好.如果您的代码覆盖率工具允许多个覆盖率指标,那么最好的一种就是块覆盖率",它衡量基本块"的覆盖率.其他类型是类和方法覆盖率(不会给您太多信息)和行覆盖率(太细粒度).

Another note I want to make about code coverage tools. In a language like C# or Java where your have many get/set and similar methods, you should not be shooting for 100% coverage. That means you are wasting too much time writing tests for trivial code. You only want 100% coverage on your complex business logic. If your full codebase is closer to 70-80% coverage, you are doing a good job. If your code coverage tool allows multiple coverage metrics, the best one is 'block coverage' which measures coverage of 'basic blocks'. Other types are class and method coverage (which don't give you as much information) and line coverage (which is too fine grain).

这篇关于严格捕获用于单元测试的测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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