2012-08-07 118 views
0

我是新來的JUnit,以下this tutorial但需要在我的測試用例的一些建議和理解如何在eclipse中使用JUnit和groovy?

我有一個文件夾中的一些個XML(3MB-6MB個),每個XML我需要測試一些標記是否含有一些價值與否,有時會將該值與特定結果相匹配。

那麼如何在循環內執行我所有的@Test函數?我是否需要通常在循環內調用@Test函數(自動調用它們)?

請在此背景下幫助我學習JUnit。由於

Junit的TestCase的

public class SystemsCheck { 

    def fileList 

    @Before 
    public void setUp() throws Exception { 

     def dir = new File("E:\\temp\\junit\\fast") 
     fileList = [] 

     def newFile="" 
     dir.eachFileRecurse (FileType.FILES) { file -> 
      fileList << file 
      } 

     fileList.each { 
      //how should i test all @Test with it.text 
      println it.text 
      }  
    } 

    @Test 
    void testOsname(){ 
     NixSystems ns=new NixSystems() 
     /*checking if tag is not empty*/ 
     //assertEquals("Result","",ns.verifyOsname()) 
    } 

    @Test 
    public void testMultiply(){ 
     NixSystems ns=new NixSystems() 
     assertEquals("Result", 50, ns.multiply(10, 5)) 
    } 

} 

class NixSystems { 

    public def verifyOsname(xml){ 
     return processXml(xml, '//client/system/osname') 
    } 

    public def multiply(int x, int y) { 
     return x * y; 
    } 


    def processXml(String xml, String xpathQuery) { 
     def xpath = XPathFactory.newInstance().newXPath() 
     def builder  = DocumentBuilderFactory.newInstance().newDocumentBuilder() 
     def inputStream = new ByteArrayInputStream(xml.bytes) 
     def records  = builder.parse(inputStream).documentElement 
     xpath.evaluate(xpathQuery, records) 
     } 
} 

回答

2

的JUnit一直致力於功能對於這種測試 - Parameterized JUnit Tests。 這基本上做你已經做了,但在簡潔,標準化的方式。

以下是Java的代碼 - 可以很容易轉換爲Groovy

@RunWith(Parameterized.class) 
public class TestCase { 
    private Type placeholder1; 
    private Type placeholder2; 
    ... 
    public TestCase(Param1 placeholder1, Param2 placeholder2, ...) { 
     this.placeholder1 = placeholder1; 
    } 

    @Parameters 
    public static Collection<Object[][]> data() { 
     //prepare data 
     //each row is one test, each object in row is placeholder1/2... for this test case 
    } 

    @Test 
    public void yourTest() {...} 
} 

在JavaDocs org.junit.runners.Parameterized中,您可以找到Fibonnaci數字測試的示例。