partychicken
Articles12
Tags3
Categories3
软件测试与测试优先的编程

软件测试与测试优先的编程

2 软件测试与测试优先的编程

Software Testing

What is testing

测试是提高软件质量的重要手段。测试是为了找到 bug 而对程序进行执行的过程,在过程中确认软件是否达到可用级别(用户需求)。 一个测试通常只关注系统的某一侧面的质量特性。

即使是最好的测试,也无法达到100%的保证无错误。

  • Residual defect rates (残留缺陷率)

    1-10 defects/kloc : Typical industry software

    0.1-1 defects/kloc: High-quality validation. (Java libraries might achieve)

    0.01-0.1 defects/kloc: The very best, safety-critical validation. (NASA and companies like Praxis can achieve this level.)

对于大型系统来说,这个缺陷率显得很震撼。(如果是 1 百万行的代码,那么整个系统就有1000个bug)

Test characteristics

目标:破坏、证错、“负能量”

再好的测试也无法证明系统里不存在错误

好的测试应具有如下特性:

  1. 能发现错误
  2. 不冗余
  3. 最佳特性
  4. 别太复杂也别太简单

Test levels

Unit testing:单元测试

Integration testing:集成测试

System testing:系统测试

Acceptance testing:验收测试

Regression testing:回归测试

Other testing types

image-20220606135801180

一些对立分类

Static vs. Dynamic

Static testing is performed without actually executing programs. Reviews, walkthroughs, or inspections are referred to as static testing.

Dynamic testing describes the testing of the dynamic behavior of code, which actually executes programmed code with a given set of test cases.

Testing vs. Debugging

测试:发现是否存在错误

调试:识别错误根源,消除错误

White-box vs. black-box

白盒测试:对程序内部代码结构的测试

黑盒测试:对程序外部表现出来的行为的测试

Why Software Testing is Hard

  • Exhaustive testing is infeasible
  • 偶然的测试没有意义
  • 基于样本的统计数据对软件测试意义不大
  • 软件行为在离散输入空间中差异巨大
  • 无统计分布规律可循

Test Case

What is test case?

Characteristics of good test case

  • Most likely to catch the wrong 最可能发现错误
  • Not repetitive and not redundant 不重复、不冗余
  • The most effective in a group of similar test cases 最有效
  • Neither too simple nor too complicated 既不简单也不复杂

Test-First Programming

Process:

  • Write a specification for the function. 先写spec
  • Write tests that exercise the specification. 再写符合spec的测试用例
  • Write the actual code. Once your code passes the tests you wrote, you’re done. 写代码、执行测试、有问题再改、再执行测试用例,直到通过它

Unit Testing

Unit testing focuses verification effort on the smallest unit of software design—the software component or module.

针对软件的最小单元模型开展测试,隔离各个模块,容易定位错误和调试

JUnit

略

Black-box Testing

黑盒测试:用于检查代码的功能,不关心内部实现细节

Black-box testing attempts to find errors in the following types:

  • Incorrect or missing functions
  • Interface errors
  • Errors in data structures or external database access
  • Behavior or performance errors
  • Initialization and termination errors

用尽可能少的测试用例,尽快运行,并尽可能大的发现程序的错误。

Choosing Test Cases by Partitioning

基于等价类划分的测试:将被测函数的输入域划分为等价类,从等价类中导出测试用例。

针对每个输入数据需要满足的约束条件,划分等价类。

每个等价类代表着对输入约束加以满足/违反的有效/无效数据的集合。

基于的假设:相似的输入,将会展示相似的行为。故可从每个等价类中选一个代表作为测试用例即可

Include Boundaries in the Partition

大量的错误发生在输入域的“边界”而非中央。

边界值分析方法是对等价类划分方法的补充。

Two Extremes for Covering the Partition

  1. Full Cartesian product 笛卡尔积:全覆盖

    测试完备,但用例数量多,测试代价高

  2. Cover each part 覆盖每个取值:最少1次即可

    测试用例少,代价低,但测试覆盖度未必高。

White-box Testing

白盒测试要考虑内部实现细节,根据程序执行路径设计测试用例。

白盒测试一般较早执行。

image-20220607211046621

独立/基本路径测试:对程序所有执行路径进行等价类划分,找出有代表性的最简单的路径(例如循环只需执行1次),设计测试用例使每一条基本路径被至少覆盖1次。

Coverage of Testing

Code coverage

代码覆盖度:已有的测试用例有多大程度覆盖了被测程序

There are many common kinds of coverage:

  • Function coverage: has each function in program been called? 函数覆盖
  • Statement coverage: is every statement run by some test case? 语句覆盖
  • Branch coverage: for every if or while or switch-case or for statement in the program, are both the true and the false direction taken by some test case? 分支覆盖
  • Condition coverage: for every condition in if/while/for/switch-case statement, are both the true/false direction taken by some test case? 条件覆盖
  • Path coverage: is every possible combination of branches — every path through the program — taken by some test case? 路径覆盖

测试效果:路径覆盖>分支覆盖>语句覆盖

测试难度:路径覆盖>分支覆盖>语句覆盖

Automated Testing and Regression Testing

回归测试:一旦程序被修改,重新执行之前的所有测试。

Documenting Your Testing Strategy

测试策略(根据什么来选择测试用例)非常重要,需要在程序中显式记录下来

目的:在代码评审过程中,其他人可以理解你的测试,并评判你的测试是否足够充分

Author:partychicken
Link:http://partychicken.github.io/2022/05/16/2%20%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E4%B8%8E%E6%B5%8B%E8%AF%95%E4%BC%98%E5%85%88%E7%9A%84%E7%BC%96%E7%A8%8B/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可
×