junit test case generator

前端 未结 5 1538
自闭症患者
自闭症患者 2021-02-08 12:00

Is there a good tool out there to automatically generate jUnit test cases based on some primitive template? This is so that test cases can be written by engineers who do not ha

相关标签:
5条回答
  • 2021-02-08 12:32

    Here is a typical tool: http://mediakey.dk/~cc/generate-junit-tests/

    TestGen4J is a collection of open-source tools that automatically generates unit test cases. TestGen4J automatically generates JUnit test cases from your own Java class files, or source files. Its primary focus is to exercise boundary value testing of the arguments passed to the method. It uses rules, written in a user-configurable XML file, that defines boundary conditions for the data types. The test code is separated from test data with the help of JTestCase.

    The test code is generated in a hierarchical manner. A main test suite is generated which invokes test suites of individual classes. The individual class test suite is formed by collection of test methods of that class.

    The test data is also organized hierarchically, corresponding to the structure of the test code, in XML format. This XML file actually has data for all unit test cases for each method. JTestCase helps to loop through all the test cases of each method and executing one

    by one against JUnit.

    0 讨论(0)
  • 2021-02-08 12:37

    Have you looked at fit?

    Fit lets you make an html table and then uses those values in your junit tests, turning the table elements red or green depending on the results of the test. It comes packaged with JUnit. You do need to wire up the fixture to translate table emenents into java but there's support for that.

    There a number of good resources floating around.

    0 讨论(0)
  • 2021-02-08 12:40

    Another alternative could be to create a higher level domain specific language that makes sense to the engineers for them to code their tests in. Groovy is an easy way to do that (google groovy & DSL), or at the other end of the spectrum use JavaCC.

    0 讨论(0)
  • 2021-02-08 12:42

    Parasoft's JTest is a commercial tool but it is quite good for:

    • generating unit tests for an existing codebase
    • creating regression tests

    For a free solution you can try the JUnit generation functionality of the CodePro Analytix Eclipse plugin.

    0 讨论(0)
  • 2021-02-08 12:51

    Sample code to generate the test cases in Java:

    import java.util.ArrayList;
    import java.util.List;
    
    public class JunitGenerator {
    
        public static List<String> s = new ArrayList();
    
        static String clname="employee";
        static String clBigname="Employee";
    
        public static void main(String[] args) {
    
            s.add("String;name;Name");
            s.add("int;age;Age");
            s.add("Boolean;isAge;IsAge");
            s.add("Double;amount;Amount");
    
            System.out.println("import static org.junit.Assert.*;");
            System.out.println("import static org.junit.Assert.assertEquals;");
    
            System.out.println("import org.junit.Test;");
            System.out.println();
            System.out.println("public class "+clBigname+"Test{");
            System.out.println(""+clBigname+" "+clname+"=new "+clBigname+"();");
    
            for(String s1:s) {
                String[] a=s1.split(";");
                System.out.println("@Test");
                System.out.println("public void get"+a[2]+"Test(){");
                if(a[0].equalsIgnoreCase("int"))
                    System.out.println(a[0]+" "+a[1]+" =0"+";");
                if(a[0].equalsIgnoreCase("String"))
                    System.out.println(a[0]+" "+a[1]+" =null"+";");
                if(a[0].equalsIgnoreCase("Double"))
                    System.out.println(a[0]+" "+a[1]+" =0.0"+";");
                if(a[0].equalsIgnoreCase("Boolean"))
                    System.out.println(a[0]+" "+a[1]+" =false"+";");
                System.out.println(""+ clname +".set"+a[2]+"("+a[1]+");");
                if(!a[0].equalsIgnoreCase("String"))
                    System.out.println("assertNotNull("+ clname +".get"+a[2]+"());");
                if(a[0].equalsIgnoreCase("String"))
                    System.out.println("assertEquals("+a[1]+","+ clname +".get"+a[2]+"());");
                System.out.println("}");
            }
    
            System.out.println("}");
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题