use of org.evosuite.xsd.Generation in project evosuite by EvoSuite.
the class ProjectStaticData method isToTest.
/**
* It checks whether EvoSuite was able to improve coverage,
* test suite size, etc for 'className' in the last N
* generations. I.e., it checks whether it is worth to test
* 'className'.
*
* @param className
* @param n
* @return
*/
public boolean isToTest(String className, int n) {
if (this.project == null) {
// we don't have any previous data at all
return true;
}
CUT cut = this.project.getCut().parallelStream().filter(p -> p.getFullNameOfTargetClass().equals(className)).findFirst().orElse(null);
if (cut == null) {
// we don't have any coverage yet
return true;
}
int how_many_generations_so_far = cut.getGeneration().size();
// did EvoSuite crashed?
if (cut.getGeneration().get(how_many_generations_so_far - 1).isFailed()) {
return true;
}
// not enough data to compare
if (how_many_generations_so_far < n) {
return true;
}
// we just keep track of the best test suite generated so far,
// however if the coverage of any criterion did not increased
// in the last N generations, there is no point continue testing.
// if at some point the className is changed, then the class
// should be tested again
List<Generation> lastNGenerations = cut.getGeneration().stream().filter(g -> g.getTimeBudgetInSeconds().intValue() > 0).skip(how_many_generations_so_far - n).collect(Collectors.toList());
// the previous one (N-1)
for (int i = lastNGenerations.size() - 1; i >= 0; i--) {
Generation generation = lastNGenerations.get(i);
if (generation.isFailed()) {
// the cut, re-generate new test cases for it
return true;
}
if (generation.getSuite() != null) {
// the previous one
return true;
}
}
// on the last N generations
return false;
}
use of org.evosuite.xsd.Generation in project evosuite by EvoSuite.
the class StorageManager method isBetterThanPreviousGeneration.
/**
* Before accepting the new test suite this function verifies
* whether it is better (in terms of coverage) than a previous
* test generation. It first checks whether it is a class that
* has been modified. By default we consider that a class has
* always been changed. Only HistorySchedule will change that
* behavior. So, for all Schedules except History we accept
* the new generated test suite. For HistorySchedule, and if a
* class has not been changed it then checks if the new test
* suite improves the coverage of the previous one.
*
* @param db
* @param current
* @param suite
* @return true if the generated test suite is better (in terms of
* coverage) than a previous generated test suite, false otherwise
*/
private boolean isBetterThanPreviousGeneration(Project db, ProjectStaticData current, TestsOnDisk suite) {
if (suite.csvData == null) {
// no data available
return false;
}
// only HistorySchedule change this behavior
if (current.getClassInfo(suite.cut).hasChanged()) {
return true;
}
CUT cut = ProjectUtil.getCUT(db, suite.cut);
Generation latestSuccessfulGeneration = CUTUtil.getLatestSuccessfulGeneration(cut);
if (latestSuccessfulGeneration == null) {
return true;
}
TestSuite previousTestSuite = latestSuccessfulGeneration.getSuite();
File oldFile = getFileForTargetBestTest(cut.getFullNameOfTestSuite());
if (!oldFile.exists()) {
// this could happen if file was manually removed
return true;
}
// is the OverallCoverage higher?
double previousOverallCoverage = GenerationUtil.getOverallCoverage(latestSuccessfulGeneration);
double generatedOverallCoverage = 0.0;
// first, check if the coverage of at least one criterion is better
for (Coverage coverage : previousTestSuite.getCoverage()) {
if (!suite.csvData.hasCoverage(coverage.getCriterion())) {
continue;
}
generatedOverallCoverage += suite.csvData.getCoverage(coverage.getCriterion());
}
generatedOverallCoverage /= suite.csvData.getNumberOfCoverageValues();
double covDif = generatedOverallCoverage - previousOverallCoverage;
if (covDif > 0.01) {
// just has two decimal digits
return true;
}
// developers could be interested on that particular goal(s)
for (Coverage coverage : previousTestSuite.getCoverage()) {
if (!suite.csvData.hasCoverage(coverage.getCriterion())) {
continue;
}
String generatedCoverage = suite.csvData.getCoverageBitString(coverage.getCriterion());
String previousCoverage = coverage.getCoverageBitString();
if (generatedCoverage.length() != previousCoverage.length()) {
// accept the new suite, as we can't compare both BitStrings
return true;
}
for (int i = 0; i < generatedCoverage.length(); i++) {
if (previousCoverage.charAt(i) == '0' && generatedCoverage.charAt(i) == '1') {
return true;
}
}
}
if (covDif < 0.0) {
// was higher, therefore discard the new test suite
return false;
}
// if we got same coverage, look at size
int oldSize = previousTestSuite.getTotalNumberOfStatements().intValue();
int newSize = suite.csvData.getTotalNumberOfStatements();
if (newSize != oldSize) {
return newSize < oldSize;
}
// same number of statements, look the number of test cases
int oldNumTests = previousTestSuite.getNumberOfTests().intValue();
int newNumTests = suite.csvData.getNumberOfTests();
return newNumTests < oldNumTests;
}
use of org.evosuite.xsd.Generation in project evosuite by EvoSuite.
the class ProjectStaticDataTest method testIsToTest_SkippedClass.
@Test
public void testIsToTest_SkippedClass() {
Project project = new Project();
project.setTotalNumberOfTestableClasses(XSDUtils.convert(1));
CUT cut = new CUT();
cut.setFullNameOfTargetClass("foo.Bar");
cut.setFullNameOfTestSuite("foo.BarTest");
Generation g0 = new Generation();
g0.setId(XSDUtils.convert(0));
g0.setFailed(false);
g0.setModified(true);
g0.setTimeBudgetInSeconds(XSDUtils.convert(60));
// empty dummy test suite
g0.setSuite(new TestSuite());
// and add first generation
cut.getGeneration().add(g0);
for (int i = 1; i <= 3; i++) {
Generation g = new Generation();
g.setId(XSDUtils.convert(i));
g.setFailed(false);
g.setModified(false);
g.setTimeBudgetInSeconds(XSDUtils.convert(60));
// and no test suite
cut.getGeneration().add(g);
}
Generation g4 = new Generation();
g4.setId(XSDUtils.convert(4));
g4.setFailed(false);
g4.setModified(false);
// EvoSuite skipped this one
g4.setTimeBudgetInSeconds(XSDUtils.convert(0));
cut.getGeneration().add(g4);
project.getCut().add(cut);
ProjectStaticData data = new ProjectStaticData();
data.setProject(project);
// coverage has not improved
Assert.assertFalse(data.isToTest("foo.Bar", 3));
}
use of org.evosuite.xsd.Generation in project evosuite by EvoSuite.
the class ProjectStaticDataTest method testIsToTest_NotEnoughGenerations.
@Test
public void testIsToTest_NotEnoughGenerations() {
Project project = new Project();
project.setTotalNumberOfTestableClasses(XSDUtils.convert(1));
CUT cut = new CUT();
cut.setFullNameOfTargetClass("foo.Bar");
cut.setFullNameOfTestSuite("foo.BarTest");
Generation generation = new Generation();
cut.getGeneration().add(generation);
project.getCut().add(cut);
ProjectStaticData data = new ProjectStaticData();
data.setProject(project);
// not enough data to compare, re-test it
Assert.assertTrue(data.isToTest("foo.Bar", 3));
}
use of org.evosuite.xsd.Generation in project evosuite by EvoSuite.
the class ProjectStaticDataTest method testIsToTest_LastestGenerationFailed.
@Test
public void testIsToTest_LastestGenerationFailed() {
Project project = new Project();
project.setTotalNumberOfTestableClasses(XSDUtils.convert(1));
CUT cut = new CUT();
cut.setFullNameOfTargetClass("foo.Bar");
cut.setFullNameOfTestSuite("foo.BarTest");
Generation generation = new Generation();
generation.setId(XSDUtils.convert(0));
generation.setFailed(true);
cut.getGeneration().add(generation);
project.getCut().add(cut);
ProjectStaticData data = new ProjectStaticData();
data.setProject(project);
// if EvoSuite failed to generate a new test suite
// for CUT and because we need to get N passing
// generations to know whether the coverage improved
// or not, this CUT has to be re-tested
Assert.assertTrue(data.isToTest("foo.Bar", 3));
}
Aggregations