我试图使用Randoop (遵循Randoop手册)根据存储在JSON文件中的前后条件规范生成测试用例。
目标程序是以下(buggy) Java方法。
package com.example.math;
public class Math {
/*Expected Behavior:
Given upperBound >= 0, the method returns
1 + 2 + ... + upperBound
But This method is buggy and works only on
inputs with odd value, e.g. for upperBound == 4,
the method returns 1 + 2 + 3 + 4 + 1 instead of
1 + 2 + 3 + 4 */
public static int sum(int upperBound) {
int s = 0;
for (int i = 0; i <= upperBound; i++) {
s += i;
}
if (upperBound % 2 == 0) {// <--------- BUG!
s++; // <--------- BUG!
} // <--------- BUG!
return s;
}
}我使用以下JSON文件来指定该方法的所需行为:
[
{
"operation": {
"classname": "com.example.math.Math",
"name": "sum",
"parameterTypes": [ "int" ]
},
"identifiers": {
"parameters": [ "upperBound" ],
"returnName": "res"
},
"post": [
{
"property": {
"condition": "res == upperBound * (upperBound + 1) / 2",
"description": ""
},
"description": "",
"guard": {
"condition": "true",
"description": ""
}
}
],
"pre": [
{
"description": "upperBound must be non-negative",
"guard": {
"condition": "upperBound >= 0",
"description": "upperBound must be non-negative"
}
}
]
}
]我编译程序,并运行以下命令来应用Randoop,以便根据正确性规范生成测试用例:
java -cp my-classpath:$RANDOOP_JAR randoop.main.Main gentests --testclass=com.example.math.Math --output-limit=200 --specifications=spec.json其中spec.json是包含上述方法契约规范的JSON文件。我有两个问题:
--output-limit不改变生成的测试用例的数量?对于足够大的数字,我似乎总是只得到8个回归测试用例--其中两个检查方法getClass不返回null值(尽管这不是我的规范的一部分)。请告诉我如何生成更多的回归测试用例。我错过了命令行选项吗?spec.json内部的规范。我们能让Randoop在每一个违反提供的后置条件的输入上生成错误暴露测试用例吗?谢谢。
发布于 2019-02-12 06:32:43
--output-limit不改变生成的测试用例的数量?Randoop生成测试,然后输出其中的子集。例如,Randoop不输出包含在一起的测试,这些测试显示为一些较长测试的子序列。
这是在--output-limit中间接提到的。
其中两个检查方法
getClass不返回空值(尽管这不是我的规范的一部分)
getClass()是Math (测试中的类)中的一个方法,所以Randoop调用getClass()。在测试生成时,返回值不是null,因此Randoop对此进行了断言。
getClass()没有什么特别之处;Randoop会为其他方法创建类似的回归测试。
spec.json内部的规范。Randoop处理静态方法的后条件规范时有一个错误。这个bug是固定。
要报告错误,最好使用兰多普问题追踪器,如Randoop手册中所述。寻求帮助的选项还包括邮件列表。与堆栈溢出不同,问题跟踪器和邮件列表允许讨论和跟踪当前状态。谢谢!
https://stackoverflow.com/questions/54612864
复制相似问题