Search in sources :

Example 36 with TestSuiteChromosome

use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.

the class CurrentTimeSystemTest method testCurrentTimeViaCalendar1.

@Test
public void testCurrentTimeViaCalendar1() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = CurrentTimeViaCalendar1.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.REPLACE_CALLS = true;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    // assuming single fitness function
    int goals = TestGenerationStrategy.getFitnessFactories().get(0).getCoverageGoals().size();
    Assert.assertEquals("Wrong number of goals: ", 3, goals);
    Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) Test(org.junit.Test)

Example 37 with TestSuiteChromosome

use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.

the class CurrentTimeSystemTest method testCurrentTime1.

@Test
public void testCurrentTime1() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = CurrentTime.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.REPLACE_CALLS = true;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    // assuming single fitness function
    int goals = TestGenerationStrategy.getFitnessFactories().get(0).getCoverageGoals().size();
    Assert.assertEquals("Wrong number of goals: ", 3, goals);
    Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) Test(org.junit.Test)

Example 38 with TestSuiteChromosome

use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.

the class CurrentTimeSystemTest method testTimeOperation.

@Test
public void testTimeOperation() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = TimeOperation.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.REPLACE_CALLS = true;
    // , "-assertions"
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    // assuming single fitness function
    int goals = TestGenerationStrategy.getFitnessFactories().get(0).getCoverageGoals().size();
    Assert.assertEquals("Wrong number of goals: ", 3, goals);
    Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) Test(org.junit.Test)

Example 39 with TestSuiteChromosome

use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.

the class IssueStaticInitializerWithTryCatchSystemTest method runTheTest.

private void runTheTest(boolean reset) {
    Properties.RESET_STATIC_FIELDS = reset;
    EvoSuite evosuite = new EvoSuite();
    String targetClass = ClassWithStaticInitializerTryCatch.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    Assert.assertNotNull(best);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome)

Example 40 with TestSuiteChromosome

use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.

the class ReadWriteSystemPropertiesSystemTest method testReadLineSeparator.

@Test
public void testReadLineSeparator() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = ReadTimezone.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.SANDBOX = true;
    Properties.REPLACE_CALLS = true;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    System.out.println("EvolvedTestSuite:\n" + best);
    double cov = best.getCoverage();
    Assert.assertEquals("Non-optimal coverage: ", 1d, cov, 0.001);
    // now check the JUnit generation
    List<TestCase> list = best.getTests();
    int n = list.size();
    Assert.assertTrue(n > 0);
    // needed because it gets pulled down after the search
    TestCaseExecutor.initExecutor();
    try {
        Sandbox.initializeSecurityManagerForSUT();
        JUnitAnalyzer.removeTestsThatDoNotCompile(list);
    } finally {
        Sandbox.resetDefaultSecurityManager();
    }
    Assert.assertEquals(n, list.size());
    TestGenerationResult tgr = TestGenerationResultBuilder.buildSuccessResult();
    String code = tgr.getTestSuiteCode();
    Assert.assertTrue("Test code:\n" + code, code.contains("user.timezone"));
    /*
		 * This is tricky. The property 'debug' is read, but it does not exist. 
		 * Ideally, we should still have in the test case a call to be sure the variable
		 * is set to null. But that would lead to a lot of problems :( eg cases
		 * in which we end up in reading hundreds of thousands variables that do not exist
		 */
    Assert.assertTrue("Test code:\n" + code, !code.contains("debug"));
}
Also used : TestGenerationResult(org.evosuite.result.TestGenerationResult) TestCase(org.evosuite.testcase.TestCase) EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) Test(org.junit.Test)

Aggregations

TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)536 Test (org.junit.Test)430 EvoSuite (org.evosuite.EvoSuite)392 Properties (org.evosuite.Properties)78 OutputVariable (org.evosuite.statistics.OutputVariable)50 GenericSUTString (com.examples.with.different.packagename.generic.GenericSUTString)49 TestChromosome (org.evosuite.testcase.TestChromosome)47 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)44 TestCase (org.evosuite.testcase.TestCase)43 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)30 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)22 Ignore (org.junit.Ignore)19 ArrayList (java.util.ArrayList)17 DefaultLocalSearchObjective (org.evosuite.ga.localsearch.DefaultLocalSearchObjective)17 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)14 CheapPurityAnalyzer (org.evosuite.assertion.CheapPurityAnalyzer)13 LineCoverageSuiteFitness (org.evosuite.coverage.line.LineCoverageSuiteFitness)13 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)13 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)10 Method (java.lang.reflect.Method)9