Test-First programming

Test-first编程的思想

让程序尽早失败,从而尽早地发现bug,以节省调试时间

Test-first编程的过程(以单元测试为例):

  1. 先写spec

  2. 再写符合spec的测试用例

  3. 写代码、执行测试、有问题再改、再执行测试用例,直到通过它

Test-first 编程的好处:

  1. 可以让你更早的发现bug

  2. 可以帮助你更加深入的理解spec

  3. 让你写代码更加自信

Test Case

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

设计Test Case的一般方法:将输入按等价类划分,每个等价类代表着对输入约束加以满足/违反的有效 /无效数据的集合,在使用一种策略来根据等价类来设计测试用例

策略:

  1. 笛卡尔积,多个划分维度上的多个取值,要组合起来,每个组合都要有一个用例

  2. 覆盖每个取值,每个维度的每个取值至少被1个测试用例覆 盖一次即可

例如:

 

还有一点值得注意,一般情况下我们都会将程序的边界情况加入等价类中。因为很多程序就是在边界情况最容易出错。

使用JUnit来编写单元测试

教程:JUnit 5 tutorial - Learn how to write unit tests

比较常用的:

 

黑盒测试和白盒测试

黑盒测试:用于检查代码的功能,不关心内部实现细节,主要用于检查程序是否符合spec。测试用例完全由spec导出,上面讲的都是黑盒测试

白盒测试:要更据程序·具体实现细节来写测试用例,例如一个程序可能跟据输入规模选择了不同的算法来实现,这是就要更具不同的规模来设计测试用例

回归测试

  1. 一旦程序被修改,重新执行之前的所有测试

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

Test Strategy

相当于测试的说明书,用于记录你是如何测试的。写给未来的你和你的同事。

例如:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Mircea has n pictures. The i-th picture is a square with a side length of si centimeters. He mounted each picture on a square piece of cardboard so that each picture has a border of w centimeters of cardboard on all sides. In total, he used c square centimeters of cardboard. Given the picture sizes and the value c, can you find the value of w? A picture of the first test case. Here c=50=52+42+32, so w=1 is the answer. Please note that the piece of cardboard goes behind each picture, not just the border. Input The first line contains a single integer t (1≤t≤1000) — the number of test cases. The first line of each test case contains two positive integers n (2≤n≤2⋅105) and c (1≤c≤1018) — the number of paintings, and the amount of used square centimeters of cardboard. The second line of each test case contains n space-separated integers si (1≤si≤104) — the sizes of the paintings. The sum of n over all test cases doesn't exceed 2⋅105. Additional constraint on the input: Such an integer w exists for each test case. Please note, that some of the input for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++). Output For each test case, output a single integer — the value of w which was used to use exactly c squared centimeters of cardboard. Example inputCopy 10 3 50 3 2 1 1 100 6 5 500 2 2 2 2 2 2 365 3 4 2 469077255466389 10000 2023 10 635472106413848880 9181 4243 7777 1859 2017 4397 14 9390 2245 7225 7 176345687772781240 9202 9407 9229 6257 7743 5738 7966 14 865563946464579627 3654 5483 1657 7571 1639 9815 122 9468 3079 2666 5498 4540 7861 5384 19 977162053008871403 9169 9520 9209 9013 9300 9843 9933 9454 9960 9167 9964 9701 9251 9404 9462 9277 9661 9164 9161 18 886531871815571953 2609 10 5098 9591 949 8485 6385 4586 1064 5412 6564 8460 2245 6552 5089 8353 3803 3764 outputCopy 1 2 4 5 7654321 126040443 79356352 124321725 113385729 110961227 Note The first test case is explained in the statement. For the second test case, the chosen w was 2, thus the only cardboard covers an area of c=(2⋅2+6)2=102=100 squared centimeters. For the third test case, the chosen w was 4, which obtains the covered area c=(2⋅4+2)2×5=102×5=100×5=500 squared centimeters. c++实现
07-22

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值