HIT-软件构造知识点概要二-软件测试与测试优先的编程

Testing and Test-First Programming

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

Objective of this lecture

Understand the value of testing, and know the process of test-first

programming 认可“测试”的价值,搞清楚“测试优先”的哲理

Be able to design a test suite for a method by partitioning its input

and output space, finding its boundaries, and choosing good test

cases 学会用等价划分和边界值分析方法为模块设计测试用例

Be able to judge a test suite by measuring its code coverage 可用工具

度量一组测试用例对代码的“覆盖度”

Understand and know when to use blackbox vs. whitebox testing,

unit tests vs. integration tests, and automated regression testing 各种

各样的测试,都有些初步了解

Outline

Software Testing

Test Case 测试用例

Test-First Programming / Test-

Driven Development (TDD) 测试优先的编程/测试驱动开发

Unit Testing 单元测试

Automated Unit Testing with JUnit使用JUnit进行自动化单元测试

Black-box Testing 黑盒测试

– Choosing Test Cases by Partitioning等价类划分

– Include Boundaries in the Partition边界值分析

White-box Testing * 白盒测试

Coverage of Testing 覆盖度

Integration Testing * 集成测试

Automated Testing and

Regression Testing * 回归测试

Documenting Your Testing

Strategy 在程序中文档化测试策略

1.Software Testing

·软件测试是提高软件质量的重要手段,用以确认是否达到用户需求(可用级别)

·测试就是为了寻找错误,但是再好的测试也无法证明系统里不存在错误

·好的测试呢个发现错误,不冗余,拥有最佳特性,不能太复杂也不能太简单

·Unit testing 单元测试: refers to tests that verify the

functionality of a specific section of code, usually at

the function level.(函数水平)

·Integration testing 集成测试: the combined execution of two or more

classes, packages, components, subsystems that have been created by

multiple programmers or programming teams.

·System testing 系统测试: to test a completely integrated system to

verify that the system meets its requirements, which executes the

software in its final configuration.

·静态测试与动态测试

静态测试:不实际执行程序,例如使用工具/文本编辑器检查源代码结构或编译器(预编译器),检查语法和数据流作为静态程序分析。

动态测试:实际上用给定的集合执行已编程的代码的测试用例。

·测试与调试

测试:发现是否存在错误

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

·白盒测试与黑盒测试

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

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

2.Test Case

·测试用例:输入+执行条件+期望结果

3.Test-First Programming

·Write the tests before you write the code.

·过程:先写spec-再写符合spec的测试用例-写代码、执行测试、有问题再改、再执行测试用例,直到通过它

·Spec描述了函数的输入和输出行为。

-它给出了参数的类型和任何额外的约束对它们(例如sqrt()的形参必须是非负的)。

-它还给出了返回值的类型和返回值的方式与输入有关。

-在代码中,规范由方法签名和上面的注释描述了它的功能。

·先写测试会节省大量的调试时间

4.Unit Testing

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

5.Automated Unit Testing with JUnit

·一个流行的单元测试框架:JUnit

·JUnit单元测试是作为注释前面的方法编写的

@Test

一个单元测试方法通常包含一个或多个模块,然后使用断言检查结果assertEquals、assertTrue和assertFalse等方法。

举例来说,我们选择的测试Math.max()可能是这样的,当为JUnit实现时:

·assertXXX in JUnit:

assertArrayEquals("failure - byte arrays not same", expected, actual);

assertEquals("failure - strings are not equal", "text", "text");

assertFalse("failure - should be false", false);

assertNotNull("should not be null", new Object());

assertNotSame("should not be same Object", new Object(), new Object());

assertNull("should be null", null);

assertSame("should be same", aNumber, aNumber);

assertTrue("failure - should be true", true);

assertThat("albumen", both(containsString("a")).and(containsString("b")));

assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));

assertThat("good", allOf(equalTo("good"), startsWith("good")));

6.Black-box Testing

·黑盒测试:用于检查代码的功能,不关心内部实现细节,检查程序是否符合规约

-测试用例通常来源于软件的外部描述,包括规格、要求和设计参数。

-选择一组足够小的测试用例来快速运行,但又足够大验证程序。

·黑盒测试完全从函数spec导出测试用例,不考虑函数内部实现

6.1Choosing Test Cases by Partitioning

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

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

·等价类可以根据指南定义:

—如果输入条件指定了一个范围,则一个有效,两个无效定义了等价类。

—如果输入条件需要指定的值,一个有效,一个无效定义了等价类。

—如果输入条件指定了一个集合的成员,一个有效,一个无效定义了等价类。

—如果输入条件为布尔值,则一个有效类和一个无效类定义的。

6.2 Include Boundaries in the  Partition

·边界值分析(BVA)被发展为一种测试技术,导致在边界值上选择测试用例。

-一个测试用例设计技术,以补充等价划分。

- BVA不是选择等价类中的任何元素,而是导致在类的“边缘”选择测试用例。

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

·在等价类划分时,将边界作为等价类之一加入考虑

Example: max()

Let’s redo max : int × int → int.

Partition into:

– Relationship between a and b

• a < b

• a = b

• a > b

– Value of a

• a = 0

• a < 0

• a > 0

• a = minimum integer

• a = maximum integer

– Value of b

• b = 0

• b < 0

• b > 0

• b = minimum integer

• b = maximum integer

§ Now let’s pick test values that cover all these classes:

– (1, 2) covers a < b, a > 0, b > 0

– (-1, -3) covers a > b, a < 0, b < 0

– (0, 0) covers a = b, a = 0, b = 0

– (Integer.MIN_VALUE, Integer.MAX_VALUE) covers a < b, a =minint, b = maxint

– (Integer.MAX_VALUE, Integer.MIN_VALUE) covers a > b, a =maxint, b = minint

7.White-box Testing

·白盒测试要考虑内部实现细节

-根据程序执行路径设计测试用例

-白盒测试一般较早执行

8.Coverage of Testing

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

·覆盖有许多种,例如:

函数覆盖:功能

语句覆盖:每条语句

分支覆盖:对错时的分支

条件覆盖:条件对或错的分支

路径覆盖:分支的组合

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

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

9.Automated Testing and Regression Testing

·自动调用被测函数、自动判定测试结果、自动计算覆盖度

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

-一旦发现bug,要马上写一个可重现该bug的测试用例,并将其加入测试库

-Automated regression testing is a best-practice of modern software engineering

Exercise:

1.下面哪个是重新运行所有JUnit测试的好时机?

-在git添加/提交/推送之前

-重写函数后,使其更快

-当使用代码覆盖工具时

-在你认为你修复了一个错误之后

2.下面哪一个最好地定义了回归测试?

-无论何时更改代码,都应该运行一套测试。

-你的代码中的每个组件都应该有一个关联的集合练习其规范中所有关键用例的测试。

-在编写代码之前,应该先编写测试检查您对规范的理解。

-当一个新的测试暴露了一个bug,你应该在之前的所有测试上运行它代码的版本,直到你找到错误所在的版本介绍

10.Documenting Your Testing Strategy

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

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

11.Summary of this lecture

§ Test-first programming. Write tests before you write code.

§ Partitioning and boundaries for choosing test cases systematically.

§ White box testing and statement coverage for filling out a test suite.

§ Unit-testing each module, in isolation as much as possible.

§ Automated regression testing to keep bugs from coming back.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值