还能像 Beizer 那样进行测试吗?

发布: (2025年12月4日 GMT+8 16:00)
6 min read
原文: Dev.to

Source: Dev.to

背景与最初的问题

谁没有在公开场合听到过这样的问题:“是的,但你的单元有多大?”
然后对方回答:“小,像……呃”,接着就没有了可比的点。
他自己也使用 JUnit、PHPUnit 或其他测试框架。

Kent Beck 对单元测试的定义

在《Test‑Driven Development By Example》(2003)中,Kent Beck 写道:

“在小规模(我称之为‘单元测试’,但这并不完全符合对单元测试的通用定义)进行开发的风险在于,你可能实现了自己认为的用户意图,却发现根本不是用户想要的。”

Beck 给了我们两条信息:

  1. 在专业领域中确实存在“单元测试”的定义。
  2. 他根本不在乎这个定义;所以 JUnit 只是一种“随便用用”的东西。

Boris Beizer 的定义

Boris Beizer,International Software Testing Qualification Board 的杰出成员,著有《Software Testing Techniques》(1983)和《Software System Testing and Quality Assurance》(1984),在其首部作品 1990 年的第二版中提出了三类测试的分类。

Beizer 所划分的测试类型

类型描述
单元,单元测试单元是软件中最小的可测试元素:它可以被编译、装配、链接、加载,并由测试平台或驱动程序控制。单元通常是某个程序员的工作,代码行数在几百行(或更少)以内。单元测试的目的是证明该单元 满足其功能规格,或其实现结构与技术设计不符。检测到的缺陷称为 单元缺陷(unit bug)
组件,组件测试组件是一个或多个单元的集成整体。按照递归定义,组件可以是单元、本身包含子组件的子系统,甚至是整个系统。组件测试的目的是证明该组件 满足其功能规格,或其实现结构与技术设计不符。检测到的缺陷称为 组件缺陷(component bug)
集成,集成测试集成是将组件组装成更大组件的过程。集成测试表明,即使每个组件单独满足要求,组合后的整体仍可能出现错误或不兼容。
系统,系统测试系统是一个“大的组件”。系统测试旨在揭示那些无法归因于单独组件的异常,例如组件之间的不兼容、意外交互、性能、安全、配置、启动或恢复方面的问题。

组件 vs. 集成 的区别示例

示例:一个递归调用自身的子程序 A

  • 组件测试 不会包含对子组件的调用;因此 A 的递归行为不在测试范围内。
  • 集成测试 会测试对 A 的调用及其返回,包括递归调用所涉及的堆栈等支持。这个新集成的组件因此需要额外的测试。

英文定义摘录

“We do three distinct kinds of testing on a typical software system: unit/component testing, integration testing, and system testing. The objectives of each class are different and therefore, we can expect the mix of test methods used to differ.”

Unit, Unit Testing — A unit is the smallest testable piece of software, by which I mean that it can be compiled or assembled, linked, loaded, and put under the control of a test harness or driver. A unit is usually the work of one programmer and consists of several hundred or fewer lines of source code. Unit testing is performed to show that the unit does not satisfy its functional specification and/or that its implemented structure does not match the intended design. When such faults are revealed, we speak of a unit bug.

Component, Component Testing — A component is an integrated aggregate of one or more units. A unit is a component; a component with the subroutines it calls is also a component, etc. By this recursive definition, a component can be anything from a single unit to an entire system. Component testing is performed to show that the component does not satisfy its functional specifications and/or that its implementation structure does not match the technical design. Faults detected are called component bugs.

结论

Beizer 提供了一套清晰且详尽的测试层级分类,从单元一直到完整系统。虽然他的名字在现代培训课程中不常出现,但他的工作仍是任何想深入软件质量的人宝贵的参考。阅读 Beizer,等于为自己构建了一个坚实的框架,帮助避免“微观测试”陷阱,并确保每个集成层级都得到恰当的验证。

Back to Blog

相关文章

阅读更多 »