Search in sources :

Example 31 with EvoSuite

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

the class InjectionSystemTest method doTest.

private void doTest(Class<?> target) {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = target.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.CRITERION = new Properties.Criterion[] { Properties.Criterion.LINE };
    Properties.JEE = true;
    Properties.P_REFLECTION_ON_PRIVATE = 0.0;
    // Takes extra time to set up the DBManager which is useless
    Properties.ASSERTIONS = false;
    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);
    Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) Properties(org.evosuite.Properties)

Example 32 with EvoSuite

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

the class ServletSystemTest method testSimpleCase_noJEE.

@Test
public void testSimpleCase_noJEE() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = SimpleHttpServlet.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.CRITERION = new Properties.Criterion[] { Properties.Criterion.LINE };
    Properties.JEE = false;
    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);
    Assert.assertTrue(best.getCoverage() < 1);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) Properties(org.evosuite.Properties) Test(org.junit.Test)

Example 33 with EvoSuite

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

the class CoverageAnalysisCharSequenceSystemTest method test.

@Test
public void test() throws IOException {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = WordUtils.class.getCanonicalName();
    String testClass = WordUtilsTest.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.CRITERION = new Properties.Criterion[] { Properties.Criterion.LINE };
    Properties.OUTPUT_VARIABLES = RuntimeVariable.Total_Goals + "," + RuntimeVariable.LineCoverage;
    Properties.STATISTICS_BACKEND = StatisticsBackend.CSV;
    Properties.COVERAGE_MATRIX = true;
    Properties.SANDBOX = false;
    Properties.VIRTUAL_FS = false;
    Properties.VIRTUAL_NET = false;
    Properties.REPLACE_CALLS = false;
    Properties.REPLACE_SYSTEM_IN = false;
    Properties.MAX_LOOP_ITERATIONS = -1;
    String[] command = new String[] { "-class", targetClass, "-Djunit=" + testClass, "-measureCoverage" };
    SearchStatistics statistics = (SearchStatistics) evosuite.parseCommandLine(command);
    Assert.assertNotNull(statistics);
    Map<String, OutputVariable<?>> outputVariables = statistics.getOutputVariables();
    assertEquals(10, (Integer) outputVariables.get(RuntimeVariable.Total_Goals.name()).getValue(), 0.0);
    assertEquals(9, (Integer) outputVariables.get(RuntimeVariable.Covered_Goals.name()).getValue(), 0.0);
    assertEquals(0.9, (Double) outputVariables.get(RuntimeVariable.LineCoverage.name()).getValue(), 0.0);
    assertEquals(1, (Integer) outputVariables.get(RuntimeVariable.Tests_Executed.name()).getValue(), 0.0);
    // the constructor of 'WordUtils' is not covered
    assertEquals("0111111111", (String) outputVariables.get(RuntimeVariable.LineCoverageBitString.name()).getValue());
    // check coverage matrix
    String coveragematrix_file = System.getProperty("user.dir") + File.separator + Properties.REPORT_DIR + File.separator + "data" + File.separator + Properties.TARGET_CLASS + File.separator + Properties.Criterion.LINE.name() + File.separator + Properties.COVERAGE_MATRIX_FILENAME;
    System.out.println("CoverageMatrix file " + coveragematrix_file);
    List<String> lines = Files.readAllLines(Paths.get(coveragematrix_file));
    // coverage of one test case
    assertEquals(1, lines.size());
    // all components except the WordUtils' constructor are covered ("1"), and the test case passes ("+")
    assertTrue(lines.get(0).equals("0 1 1 1 1 1 1 1 1 1 +"));
}
Also used : SearchStatistics(org.evosuite.statistics.SearchStatistics) EvoSuite(org.evosuite.EvoSuite) Properties(org.evosuite.Properties) OutputVariable(org.evosuite.statistics.OutputVariable) Test(org.junit.Test) WordUtilsTest(com.examples.with.different.packagename.WordUtilsTest)

Example 34 with EvoSuite

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

the class CoverageAnalysisLocaleSystemTest method test.

@Test
public void test() throws IOException {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = StringUtils.class.getCanonicalName();
    String testClass = StringUtilsEqualsIndexOfTest.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.CRITERION = new Properties.Criterion[] { Properties.Criterion.LINE };
    Properties.OUTPUT_VARIABLES = RuntimeVariable.Total_Goals + "," + RuntimeVariable.LineCoverage;
    Properties.STATISTICS_BACKEND = StatisticsBackend.CSV;
    Properties.COVERAGE_MATRIX = true;
    String[] command = new String[] { "-class", targetClass, "-Djunit=" + testClass, "-measureCoverage" };
    SearchStatistics statistics = (SearchStatistics) evosuite.parseCommandLine(command);
    Assert.assertNotNull(statistics);
    Map<String, OutputVariable<?>> outputVariables = statistics.getOutputVariables();
    assertEquals(10, (Integer) outputVariables.get(RuntimeVariable.Total_Goals.name()).getValue(), 0.0);
    assertEquals(8, (Integer) outputVariables.get(RuntimeVariable.Covered_Goals.name()).getValue(), 0.0);
    assertEquals(8.0 / 10.0, (Double) outputVariables.get(RuntimeVariable.LineCoverage.name()).getValue(), 0.0);
    assertEquals(1, (Integer) outputVariables.get(RuntimeVariable.Tests_Executed.name()).getValue(), 0.0);
    // check coverage matrix
    String coveragematrix_file = System.getProperty("user.dir") + File.separator + Properties.REPORT_DIR + File.separator + "data" + File.separator + Properties.TARGET_CLASS + File.separator + Properties.Criterion.LINE.name() + File.separator + Properties.COVERAGE_MATRIX_FILENAME;
    System.out.println("CoverageMatrix file " + coveragematrix_file);
    List<String> lines = Files.readAllLines(Paths.get(coveragematrix_file));
    // coverage of one test case
    assertEquals(1, lines.size());
    // all components have been covered ("1"), and the test case pass ("+")
    assertTrue(lines.get(0).contains("+"));
}
Also used : SearchStatistics(org.evosuite.statistics.SearchStatistics) EvoSuite(org.evosuite.EvoSuite) Properties(org.evosuite.Properties) OutputVariable(org.evosuite.statistics.OutputVariable) Test(org.junit.Test) StringUtilsEqualsIndexOfTest(com.examples.with.different.packagename.StringUtilsEqualsIndexOfTest)

Example 35 with EvoSuite

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

the class CoverageAnalysisStringInstrumentationSystemTest method testCreateBigInteger.

@Test
public void testCreateBigInteger() throws IOException {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = ClassNumberUtils.class.getCanonicalName();
    String testClass = ClassNumberUtilsTest.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.CRITERION = new Properties.Criterion[] { Properties.Criterion.LINE };
    Properties.OUTPUT_VARIABLES = RuntimeVariable.Total_Goals + "," + RuntimeVariable.LineCoverage;
    Properties.STATISTICS_BACKEND = StatisticsBackend.CSV;
    Properties.COVERAGE_MATRIX = true;
    String[] command = new String[] { "-class", targetClass, "-Djunit=" + testClass, "-measureCoverage" };
    SearchStatistics statistics = (SearchStatistics) evosuite.parseCommandLine(command);
    Assert.assertNotNull(statistics);
    Map<String, OutputVariable<?>> outputVariables = statistics.getOutputVariables();
    assertEquals(20, (Integer) outputVariables.get(RuntimeVariable.Total_Goals.name()).getValue(), 0.0);
    assertEquals(19, (Integer) outputVariables.get(RuntimeVariable.Covered_Goals.name()).getValue(), 0.0);
    assertEquals(0.95, (Double) outputVariables.get(RuntimeVariable.LineCoverage.name()).getValue(), 0.0);
    assertEquals(1, (Integer) outputVariables.get(RuntimeVariable.Tests_Executed.name()).getValue(), 0.0);
    assertEquals("01111111111111111111", (String) outputVariables.get(RuntimeVariable.LineCoverageBitString.name()).getValue());
    // check coverage matrix
    String coveragematrix_file = System.getProperty("user.dir") + File.separator + Properties.REPORT_DIR + File.separator + "data" + File.separator + Properties.TARGET_CLASS + File.separator + Properties.Criterion.LINE.name() + File.separator + Properties.COVERAGE_MATRIX_FILENAME;
    System.out.println("CoverageMatrix file " + coveragematrix_file);
    List<String> lines = Files.readAllLines(Paths.get(coveragematrix_file));
    // coverage of one test case
    assertEquals(1, lines.size());
    // all components have been covered ("1"), and the test case pass ("+")
    assertEquals("0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 +", lines.get(0));
}
Also used : SearchStatistics(org.evosuite.statistics.SearchStatistics) EvoSuite(org.evosuite.EvoSuite) Properties(org.evosuite.Properties) OutputVariable(org.evosuite.statistics.OutputVariable) ClassNumberUtilsTest(com.examples.with.different.packagename.ClassNumberUtilsTest) Test(org.junit.Test)

Aggregations

EvoSuite (org.evosuite.EvoSuite)462 Test (org.junit.Test)410 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)391 Properties (org.evosuite.Properties)86 OutputVariable (org.evosuite.statistics.OutputVariable)61 GenericSUTString (com.examples.with.different.packagename.generic.GenericSUTString)49 SearchStatistics (org.evosuite.statistics.SearchStatistics)16 Ignore (org.junit.Ignore)15 CheapPurityAnalyzer (org.evosuite.assertion.CheapPurityAnalyzer)13 CSVReader (com.opencsv.CSVReader)12 FileReader (java.io.FileReader)12 File (java.io.File)9 TestCase (org.evosuite.testcase.TestCase)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 MethodCoverageFitnessFunctionSystemTest (org.evosuite.coverage.method.MethodCoverageFitnessFunctionSystemTest)7 Before (org.junit.Before)7 TypeSeedingExampleString (com.examples.with.different.packagename.TypeSeedingExampleString)6 TestGenerationResult (org.evosuite.result.TestGenerationResult)6 CalculatorTest (com.examples.with.different.packagename.CalculatorTest)5