use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.
the class CoverageGoalTestNameGenerationStrategy method getUpdatedResults.
/**
* Some criteria require re-execution with observers. Make sure the results are up-to-date
*
* @param requiredCriteria
* @param origResults
* @return
*/
private List<ExecutionResult> getUpdatedResults(List<Properties.Criterion> requiredCriteria, List<ExecutionResult> origResults) {
List<ExecutionObserver> newObservers = new ArrayList<ExecutionObserver>();
if (requiredCriteria.contains(Properties.Criterion.INPUT)) {
newObservers.add(new InputObserver());
}
if (requiredCriteria.contains(Properties.Criterion.OUTPUT)) {
newObservers.add(new OutputObserver());
}
if (newObservers.isEmpty()) {
return origResults;
}
for (ExecutionObserver observer : newObservers) TestCaseExecutor.getInstance().addObserver(observer);
List<ExecutionResult> newResults = new ArrayList<ExecutionResult>();
for (ExecutionResult result : origResults) {
ExecutionResult newResult = TestCaseExecutor.getInstance().runTest(result.test);
newResults.add(newResult);
}
for (ExecutionObserver observer : newObservers) TestCaseExecutor.getInstance().removeObserver(observer);
return newResults;
}
use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.
the class TestSuiteWriter method runTest.
/**
* <p>
* runTest
* </p>
*
* @param test a {@link org.evosuite.testcase.TestCase} object.
* @return a {@link org.evosuite.testcase.execution.ExecutionResult} object.
*/
protected ExecutionResult runTest(TestCase test) {
ExecutionResult result = new ExecutionResult(test, null);
try {
logger.debug("Executing test");
result = executor.execute(test);
} catch (Exception e) {
throw new Error(e);
}
return result;
}
use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.
the class Scaffolding method generateMockInitialization.
/**
* This is needed because the first time we do initialize a mock object, that can take
* some seconds (successive calls would be based on cached data), and so tests might
* timeout. So here we force the mock initialization in a @BeforeClass
*
* @param bd
* @param results
*/
private void generateMockInitialization(String testClassName, StringBuilder bd, List<ExecutionResult> results) {
if (!TestSuiteWriterUtils.doesUseMocks(results)) {
return;
}
// In order to make sure this is called *after* initializeClasses this method is now called directly from initEvoSuiteFramework
// bd.append(METHOD_SPACE);
// bd.append("@BeforeClass \n");
bd.append(METHOD_SPACE);
bd.append("private static void initMocksToAvoidTimeoutsInTheTests() throws ClassNotFoundException { \n");
Set<String> mockStatements = new LinkedHashSet<>();
for (ExecutionResult er : results) {
for (Statement st : er.test) {
if (st instanceof FunctionalMockStatement) {
FunctionalMockStatement fms = (FunctionalMockStatement) st;
String name = new GenericClass(fms.getReturnType()).getRawClass().getTypeName();
mockStatements.add("mock(Class.forName(\"" + name + "\", false, " + testClassName + ".class.getClassLoader()));");
}
}
}
mockStatements.stream().sorted().forEach(m -> {
bd.append(BLOCK_SPACE);
bd.append(m);
bd.append("\n");
});
bd.append(METHOD_SPACE);
bd.append("}\n");
}
use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.
the class ArrayLocalSearch method searchLength.
private boolean searchLength(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
boolean hasImproved = false;
ArrayStatement p = (ArrayStatement) test.getTestCase().getStatement(statement);
logger.debug("Performing local search on array length, starting with length {}", p.size());
ExecutionResult oldResult = test.getLastExecutionResult();
oldLength = p.size();
boolean done = false;
while (!done) {
done = true;
// Try +1
p.setSize(oldLength + 1);
logger.debug("Trying increment of {}", p.getCode());
if (objective.hasImproved(test)) {
done = false;
hasImproved = true;
boolean improved = true;
while (improved) {
oldLength = p.size();
oldResult = test.getLastExecutionResult();
p.setSize(oldLength + 1);
logger.debug("Trying increment of {}", p.getCode());
improved = objective.hasImproved(test);
}
p.setSize(oldLength);
test.setLastExecutionResult(oldResult);
test.setChanged(false);
} else {
if (oldLength > 0) {
// Restore original, try -1
p.setSize(oldLength);
test.setLastExecutionResult(oldResult);
test.setChanged(false);
p.setSize(oldLength - 1);
} else {
p.setSize(Properties.MAX_ARRAY);
}
logger.debug("Trying decrement of {}", p.getCode());
if (objective.hasImproved(test)) {
done = false;
hasImproved = true;
boolean improved = true;
while (improved && p.size() > 0) {
oldLength = p.size();
oldResult = test.getLastExecutionResult();
p.setSize(oldLength - 1);
logger.debug("Trying decrement of {}", p.getCode());
improved = objective.hasImproved(test);
}
p.setSize(oldLength);
test.setLastExecutionResult(oldResult);
test.setChanged(false);
} else {
p.setSize(oldLength);
test.setLastExecutionResult(oldResult);
test.setChanged(false);
}
}
}
logger.debug("Finished local array length search with result {}", p.getCode());
return hasImproved;
}
use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.
the class FloatLocalSearch method doSearch.
private int doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective, double initialDelta, double factor, NumericalPrimitiveStatement<T> p) {
int changed = 0;
oldValue = p.getValue();
ExecutionResult oldResult = test.getLastExecutionResult();
boolean done = false;
while (!done) {
done = true;
// Try +1
p.increment(initialDelta);
logger.info("Trying increment of " + p.getCode());
// logger.info(" -> " + p.getCode());
int change = objective.hasChanged(test);
if (change != 0)
changed = change;
if (change < 0) {
done = false;
// changed = true;
iterate(factor * initialDelta, factor, objective, test, p, statement);
oldValue = p.getValue();
oldResult = test.getLastExecutionResult();
} else {
// Restore original, try -1
p.setValue(oldValue);
test.setLastExecutionResult(oldResult);
test.setChanged(false);
p.increment(-initialDelta);
logger.info("Trying decrement of " + p.getCode());
// logger.info(" -> " + p.getCode());
change = objective.hasChanged(test);
if (change < 0) {
logger.info("Iterating because of improvement");
changed = change;
done = false;
iterate(-factor * initialDelta, factor, objective, test, p, statement);
oldValue = p.getValue();
oldResult = test.getLastExecutionResult();
} else {
logger.info("Not iterating because no improvement");
p.setValue(oldValue);
test.setLastExecutionResult(oldResult);
test.setChanged(false);
}
}
}
logger.debug("Finished local search with result " + p.getCode());
return changed;
}
Aggregations