简体   繁体   English

如何使用groovy在eclipse中使用JUnit?

[英]How to use JUnit in eclipse with groovy?

I am new to JUnit, following this tutorial but need some suggestions and understanding on my testcase 我是JUnit的新手 ,遵循本教程但需要对我的测试用例提出一些建议和理解

I have some xmls (3MB-6MB each) in a folder, for each xml I need to test some tags whether they contain some value or not and sometimes match that value to a specific result. 我在一个文件夹中有一些xmls(每个3MB-6MB),对于每个xml,我需要测试一些标签是否包含某些值,有时将该值与特定结果匹配。

So how can I execute all my @Test functions inside a loop, for each test? 那么,对于每个测试,如何在循环内执行所有@Test函数? Do I need to normally call the @Test Functions (coz they are called automatically) inside loop? 我是否需要在循环内正常调用@Test函数(因为它们被自动调用)?

Please help me undertand JUnit with this context. 请用这个上下文帮助我和JUnit。 Thanks 谢谢

Junit testcase Junit测试用例

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 )
      }
}

JUnit has dedicated feature for this kind of tests - Parameterized JUnit Tests . JUnit具有这种测试的专用功能 - 参数化JUnit测试 This basically do what you've already done but in concise, standarized manner. 这基本上做了你已经完成的事情,但是以简洁,标准化的方式。

Below is code for Java - can be easily converted to Groovy . 下面是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() {...}
}

In JavaDocs of org.junit.runners.Parameterized you can find example of Fibonnaci numbers test. org.junit.runners.Parameterized的 JavaDocs中,您可以找到Fibonnaci数字测试的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM