Unit Test 学习笔记

 

public class MyTest extends TestCase {
private int count = 0;
public void test1() {
count++;
assertEquals(1, count);
}
public void test2() {
count++;
assertEquals(1, count);
}
}

 Take a quick look at this test case and imagine running it with JUnit.

What do you think will happen? Pass or fail?

The answer: It passes.

I remember shaking my head in disbelief when I saw this result. I truly

couldn’t believe my eyes, and I was certain that I had been working too

much and that I wasn’t thinking straight. But debugging into the JUnit code

confirmed this observation: JUnit reinstantiates the test class before each

test method, thereby explaining why the count field is reset to zero each

time.



Test methods with JUnit have the following constraints.

■ Their names need to start with test.

■ They can’t return any values.

■ They can’t have any parameters.

This last restriction is something that has often bothered us. Why can’t

we pass parameters to our test methods? After all, passing parameters to

methods and functions is an idea that has been present in programming languages

for decades, and we can’t think of a programmer who wouldn’t be

familiar with the idea.

Passing parameters to test methods seems to be very natural and, sometimes,

extremely practical. Again, we found that you can’t do this in JUnit,

and the only way you can approximate the result is by using a convoluted

design pattern

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值