java junit 测试用例生成器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4102078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
junit test case generator
提问by bob
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 have a lot of Java or jUnit background. As background information, this is for black box testing. If there is some other alternative to run regression tests than using jUnit, I would also appreciate hearing about it.
有没有一个很好的工具可以根据一些原始模板自动生成 jUnit 测试用例?这是为了让没有很多 Java 或 jUnit 背景的工程师可以编写测试用例。作为背景信息,这是用于黑盒测试。如果除了使用 jUnit 之外,还有其他一些运行回归测试的替代方法,我也很感激听到它。
Thx
谢谢
采纳答案by vickirk
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.
另一种选择可能是创建一种对工程师有意义的更高级别的领域特定语言,以便他们编写测试代码。 Groovy 是一种简单的方法(google groovy 和 DSL),或者在频谱的另一端使用JavaCC。
回答by Csaba_H
Parasoft's JTestis a commercial tool but it is quite good for:
Parasoft 的JTest是一种商业工具,但它非常适合:
- generating unit tests for an existing codebase
- creating regression tests
- 为现有代码库生成单元测试
- 创建回归测试
For a free solution you can try the JUnit generation functionality of the CodePro AnalytixEclipse plugin.
对于免费的解决方案,您可以尝试CodePro AnalytixEclipse 插件的 JUnit 生成功能。
回答by Paul Rubel
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.
Fit 允许您制作一个 html 表格,然后在您的 junit 测试中使用这些值,根据测试结果将表格元素变为红色或绿色。它与 JUnit 一起打包。您确实需要连接夹具以将表元素转换为 Java,但对此有支持。
There a number of good resourcesfloating around.
有许多好的资源漂浮在周围。
回答by Prashanth
Sample code to generate the test cases in Java:
在 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("}");
}
}
回答by peter.murray.rust
Here is a typical tool: http://mediakey.dk/~cc/generate-junit-tests/
这是一个典型的工具: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.
TestGen4J 是一组开源工具,可自动生成单元测试用例。TestGen4J 自动从您自己的 Java 类文件或源文件生成 JUnit 测试用例。它的主要重点是对传递给方法的参数进行边界值测试。它使用在用户可配置的 XML 文件中编写的规则来定义数据类型的边界条件。在 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
测试数据也是分层组织的,对应于测试代码的结构,采用 XML 格式。这个 XML 文件实际上包含每个方法的所有单元测试用例的数据。JTestCase 有助于遍历每个方法的所有测试用例并执行一个
by one against JUnit.
以一比一对抗 JUnit。