Software Construction 3

本文介绍了测试驱动开发的基本原则,包括测试优先的方法论、分区测试策略及其应用实例,并探讨了黑盒测试与白盒测试的区别,最后讲解了自动化测试及回归测试的重要性。

text3
一、测试优先
(1)为函数写一个规格说明。
(2)为上一步的规格说明写一些测试用例。
(3)编写实际代码。一旦你的代码通过了所有你写的测试用例,这个函数就算完成了。
二、分区测试(三个例子)
EX1:

/**
 * @param val another BigInteger
 * @return a BigInteger whose value is (this * val).
 */
public BigInteger multiply(BigInteger val)

测试:a、b如何取值

BigInteger a = ...;
BigInteger b = ...;
BigInteger ab = a.multiply(b);

方案:
这里写图片描述

一共7*7个区域

  • EX2
/**
 * @param a  an argument
 * @param b  another argument
 * @return the larger of a and b.
 */
public static int max(int a, int b)

分析区域:
这里写图片描述

*a与b的关系 a < b a = b a > b
*a的值 a = 0 a < 0 a > 0 a = 最小的整数 a = 最大的整数
*b的值 b = 0 b < 0 b > 0 b = 最小的整数 b = 最大的整数

  • EX3
/**
 * Reverses the end of a string.
 *
 *                          012345                     012345
 * For example: reverseEnd("Hello, world", 5) returns "Hellodlrow ,"
 *                               <----->                    <----->
 *
 * With start == 0, reverses the entire text.
 * With start == text.length(), reverses nothing.
 *
 * @param text    non-null String that will have its end reversed
 * @param start   the index at which the remainder of the input is reversed,
 *                requires 0 <= start <= text.length()
 * @return input text with the substring from start to the end of the string reversed
 */
public static String reverseEnd(String text, int start)

start测试分区:
A start = 0, start = 5, start = 100
B start < 0, start = 0, start > 0
C start = 0, 0 < start < text.length(), start = text.length()
D start < text.length(), start = text.length(), start > text.length()
注:选第三个,因为第四个不是输入的范围内

text测试分区
A text contains some letters; text contains no letters, but some numbers; text contains neither letters nor numbers
B text.length() = 0; text.length() > 0
C text.length() = 0; text.length()-start is odd; text.length()-start is even
D text is every possible string from length 0 to 100
注:选择B、C 。选择C是因为,例如(abc def ,2)
翻转后变成 abc fed(e的位置没有改变),这可能需要特殊的行为来处理,也可能是bug产生的原因
三、如何写测试

/**
 * Reverses the end of a string.
 *
 * For example:
 *   reverseEnd("Hello, world", 5)
 *   returns "Hellodlrow ,"
 *
 * With start == 0, reverses the entire text.
 * With start == text.length(), reverses nothing.
 *
 * @param text    non-null String that will have
 *                its end reversed
 * @param start   the index at which the
 *                remainder of the input is
 *                reversed, requires 0 <=
 *                start <= text.length()
 * @return input text with the substring from
 *               start to the end of the string
 *               reversed
 */
static String reverseEnd(String text, int start)

我们应该在测试时记录下我们的测试策略,例如我们是如何分区的,有哪些特殊值、边界值等等

/*
 * Testing strategy
 *
 * Partition the inputs as follows:
 * text.length(): 0, 1, > 1
 * start:         0, 1, 1 < start < text.length(),
 *                text.length() - 1, text.length()
 * text.length()-start: 0, 1, even > 1, odd > 1
 *
 * Include even- and odd-length reversals because
 * only odd has a middle element that doesn't move.
 *
 * Exhaustive Cartesian coverage of partitions.
 */

另外,每一个测试方法都要有一个小的注解,说明这个测试方法是代表测试策略中的哪一部分,例如:

// covers test.length() = 0,
//        start = 0 = text.length(),
//        text.length()-start = 0
@Test public void testEmpty() {
    assertEquals("", reverseEnd("", 0));
}

四、黑盒测试与白盒测试
黑:只依据函数的规格说明来选择测试用例,而不关心函数是如何实现
白:考虑实现的方法
例题:Q???
五、自动化测试、回归测试
自动化测试(Automated testing):是指自动地运行测试对象,输入对应的测试用例,并记录结果的测试
回归测试:我们称修改代码带来新的bug的现象为“回归”,而在修改后重新运行所有的测试

  • 练习
    什么情况下应该重新运行所有的 JUnit 测试?
  • 在使用 git add/commit/push之前
  • 在优化一个函数的性能后
  • 在使用覆盖率工具时
  • 在修改一个bug后
    注:全部。第三个:Rerunning tests is an essential part of using a code coverage tool, because you want to see the code lines that your tests don’t reach.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值