Chinaunix

标题: 测试如何驱动开发 [打印本页]

作者: willyomg    时间: 2011-02-28 17:02
标题: 测试如何驱动开发
需求:反转一个句子
我可能会写出以下的测试——写一个测试,然后写代码让测试通过,然后再写下一个测试。
自己看吧。
  1. public class StringReverseTest {  
  2.   # Test 1
  3.   public void testShouldSplitSentenceIntoWords(){  
  4.     StringReverser sr=new StringReverser();  
  5.     String str = "This is a sentence";  
  6.     Assert.assertEquals(4, sr.split(str).size());
  7.     Assert.assertEquals("sentence", sr.split(str).get(0));  
  8.     Assert.assertEquals("a", sr.split(str).get(1));  
  9.     Assert.assertEquals("is", sr.split(str).get(2));  
  10.     Assert.assertEquals("This", sr.split(str).get(3));  
  11.   }  
  12.   # Test 2
  13.   public void testShouldReverseSentence(){  
  14.     StringReverser sr=new StringReverser();  
  15.     String str = "Tdd is a software devolopment technology";  
  16.     Assert.assertEquals("technology devolopment software a is Tdd",sr.reverse(str));  
  17.   }  
  18.   # Test 3
  19.   public void testShouldAlwaysReverseSentences(){  
  20.     StringReverser sr=new StringReverser();  
  21.     String str = "This is Yet Another sentence";  
  22.     Assert.assertEquals("sentence Another Yet is This",sr.reverse(str));  
  23.   }  
  24. }  
复制代码
原文http://www.javaeye.com/topic/124959




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2