s********e 发帖数: 340 | 1 有一个面试题,是让用测试驱动TDD的方式开发和测试一个小程序。程序是:
写一个程序,打印从1到100的数字,如果这个数字是3的倍数,那么就打印AAA,而不是
打印出这个数字,如果这个数字是5的倍数,就打印出BBB,而不是打印出这个数字,如
果这个数字是即是3的倍数,也是5的倍数,就打印出AAABBB.
单纯写这个程序很简单,但是要用TDD的方式写出测试驱动就比较费解。主要是不知道
TDD的测试代码该如何写才能实现单元测试。 请指教,希望能给出程序和解释。
此外,请问如果一个方法没有返回值,是void的,该如何用做单元测试呢? |
s********e 发帖数: 340 | 2 求高人指点!谢谢!
【在 s********e 的大作中提到】 : 有一个面试题,是让用测试驱动TDD的方式开发和测试一个小程序。程序是: : 写一个程序,打印从1到100的数字,如果这个数字是3的倍数,那么就打印AAA,而不是 : 打印出这个数字,如果这个数字是5的倍数,就打印出BBB,而不是打印出这个数字,如 : 果这个数字是即是3的倍数,也是5的倍数,就打印出AAABBB. : 单纯写这个程序很简单,但是要用TDD的方式写出测试驱动就比较费解。主要是不知道 : TDD的测试代码该如何写才能实现单元测试。 请指教,希望能给出程序和解释。 : 此外,请问如果一个方法没有返回值,是void的,该如何用做单元测试呢?
|
z********r 发帖数: 179 | 3 use junit test?
@test
public void testMethod() {
assertTrue(yourMethod() == null);
}
if your method returns null then it should pass this test.
TDD means you write test first and then write your code until it passes your
test |
s********e 发帖数: 340 | 4 我觉得这样做的话,不能测试出正确啊? 我的困惑就是如果这个方式是void的,没有
返回值,如何测试方法里面的运行结果是正确的。比如我说的这个打印的问题,没有返
回结果,如何用assert去比较?该如何测试方法是正确的?
谢谢!
your
【在 z********r 的大作中提到】 : use junit test? : @test : public void testMethod() { : assertTrue(yourMethod() == null); : } : if your method returns null then it should pass this test. : TDD means you write test first and then write your code until it passes your : test
|
z********r 发帖数: 179 | 5 I see what u are asking. You should have separate methods for doing output
and computation. Use Junit to test your computation method. It's generally
not easy and not necessary to test the output method.
You should have a method String convertIntToStr(int a)
and test that
asssertTrue(convertIntToStr(3).equals("AAA"));
assertTrue(convertIntToStr(5).equals("BBB"));
.....
【在 s********e 的大作中提到】 : 我觉得这样做的话,不能测试出正确啊? 我的困惑就是如果这个方式是void的,没有 : 返回值,如何测试方法里面的运行结果是正确的。比如我说的这个打印的问题,没有返 : 回结果,如何用assert去比较?该如何测试方法是正确的? : 谢谢! : : your
|
s********e 发帖数: 340 | 6 谢谢你的回复,我把我写的代码发到你的站内信里了,你能看一下我的代码,该如何测
试?谢谢指教! :)
【在 z********r 的大作中提到】 : I see what u are asking. You should have separate methods for doing output : and computation. Use Junit to test your computation method. It's generally : not easy and not necessary to test the output method. : You should have a method String convertIntToStr(int a) : and test that : asssertTrue(convertIntToStr(3).equals("AAA")); : assertTrue(convertIntToStr(5).equals("BBB")); : .....
|