Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option for covering tests #98

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/main/java/eu/stamp_project/testrunner/EntryPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public class EntryPoint {
/**
* Enable this boolean to keep the values after each run. The list of concerned
* values are: JVMArgs, outPrintStream, errPrintStream, workingDirectory,
* timeoutInMs,
* timeoutInMs, coverTests
*/
public static boolean persistence = true;

Expand All @@ -150,6 +150,11 @@ public class EntryPoint {
public static ParserOptions.CoverageTransformerDetail coverageDetail =
ParserOptions.CoverageTransformerDetail.SUMMARIZED;

/**
* Allows to enable test class coverage computation.
*/
public static boolean coverTests = false;

// PIT OPTIONS

/**
Expand Down Expand Up @@ -318,6 +323,7 @@ public static Coverage runCoverage(String classpath, String targetProjectClasses
EntryPoint.coverageDetail == ParserOptions.CoverageTransformerDetail.SUMMARIZED ? "" :
(ParserOptions.FLAG_coverage_detail + ConstantsHelper.WHITE_SPACE
+ EntryPoint.coverageDetail.name()),
EntryPoint.coverTests ? ParserOptions.FLAG_cover_tests : "",
});
return EntryPoint.runCoverage(javaCommand);
}
Expand Down Expand Up @@ -406,6 +412,7 @@ public static CoveragePerTestMethod runCoveragePerTestMethods(String classpath,
EntryPoint.coverageDetail == ParserOptions.CoverageTransformerDetail.SUMMARIZED ? "" :
(ParserOptions.FLAG_coverage_detail + ConstantsHelper.WHITE_SPACE
+ EntryPoint.coverageDetail.name()),
EntryPoint.coverTests ? ParserOptions.FLAG_cover_tests : "",
});
try {
EntryPoint.runGivenCommandLine(javaCommand);
Expand Down Expand Up @@ -491,6 +498,7 @@ public static CoveredTestResultPerTestMethod runCoveredTestResultPerTestMethods(
EntryPoint.coverageDetail == ParserOptions.CoverageTransformerDetail.SUMMARIZED ? "" :
(ParserOptions.FLAG_coverage_detail + ConstantsHelper.WHITE_SPACE
+ EntryPoint.coverageDetail.name()),
EntryPoint.coverTests ? ParserOptions.FLAG_cover_tests : "",
});
try {
EntryPoint.runGivenCommandLine(javaCommand);
Expand Down Expand Up @@ -637,6 +645,7 @@ private static void reset() {
EntryPoint.outPrintStream = null;
EntryPoint.errPrintStream = null;
EntryPoint.blackList.clear();
EntryPoint.coverTests = false;
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ public Coverage transformJacocoObject(ExecutionDataStore executionData, String c
final CoverageBuilder coverageBuilder = new CoverageBuilder();
final Analyzer analyzer = new Analyzer(executionData, coverageBuilder);
try {
analyzer.analyzeAll(new File(classesDirectory));
//TODO: change the interface to an array of URL
String[] paths = classesDirectory.split(File.pathSeparator);
for (String path : paths) {
analyzer.analyzeAll(new File(path));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public Coverage transformJacocoObject(ExecutionDataStore executionData, String c
final CoverageBuilder coverageBuilder = new CoverageBuilder();
final Analyzer analyzer = new Analyzer(executionData, coverageBuilder);
try {
analyzer.analyzeAll(new File(classesDirectory));
//TODO: change the interface to an array of URL
String[] paths = classesDirectory.split(File.pathSeparator);
for (String path : paths) {
analyzer.analyzeAll(new File(path));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public static ParserOptions parse(String[] args) {
case FLAG_coverage_detail:
parserOptions.coverageTransformerDetail = CoverageTransformerDetail.valueOf(args[++i]);
break;
case FLAG_cover_tests:
parserOptions.coverTests = true;
break;
case " ":
case "":
break;
Expand Down Expand Up @@ -75,6 +78,9 @@ private static void usage() {
usage.append(FLAG_coverage_detail).append(ConstantsHelper.WHITE_SPACE)
.append(FLAG_HELP_coverage_detail).append(ConstantsHelper.LINE_SEPARATOR);

usage.append(FLAG_cover_tests).append(ConstantsHelper.WHITE_SPACE)
.append(FLAG_HELP_cover_tests).append(ConstantsHelper.LINE_SEPARATOR);

System.out.println(usage.toString());
}

Expand Down Expand Up @@ -136,13 +142,23 @@ public enum CoverageTransformerDetail {
" provided in the coverage information. Valid values:" +
"'SUMMARIZED' (default), 'DETAIL' or 'METHOD_DETAIL'.";

/**
* This value whether test classes are to be included in the coverage results
*/
private boolean coverTests;

public static final String FLAG_cover_tests = "--cover-tests";

public static final String FLAG_HELP_cover_tests = "The usage of this flag results in test classes being included in the coverage results.";


private ParserOptions() {
this.pathToCompiledClassesOfTheProject = "";
this.fullQualifiedNameOfTestClassesToRun = new String[]{};
this.testMethodNamesToRun = new String[]{};
this.blackList = new ArrayList<>();
this.coverageTransformerDetail = CoverageTransformerDetail.SUMMARIZED;
this.coverTests = false;
}

public String getPathToCompiledClassesOfTheProject() {
Expand Down Expand Up @@ -174,4 +190,7 @@ public CoverageTransformer getCoverageTransformer() {

}

public boolean isCoverTests() {
return coverTests;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public JUnit4JacocoRunner(String classesDirectory, String testClassesDirectory,
public static void main(String[] args) {
final ParserOptions options = ParserOptions.parse(args);
final String[] splittedArgs0 = options.getPathToCompiledClassesOfTheProject().split(ConstantsHelper.PATH_SEPARATOR);
final String classesDirectory = splittedArgs0[0];
final String classesDirectory = options.isCoverTests() ? options.getPathToCompiledClassesOfTheProject() : splittedArgs0[0];
final String testClassesDirectory = splittedArgs0[1];
final JacocoRunner jacocoRunner =
new JUnit4JacocoRunner(classesDirectory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ public JUnit4JacocoRunnerCoveredResultPerTestMethod(String classesDirectory, Str

public JUnit4JacocoRunnerCoveredResultPerTestMethod(String classesDirectory, String testClassesDirectory, List<String> blackList, CoverageTransformer coverageTransformer) {
super(classesDirectory, testClassesDirectory, blackList, coverageTransformer);
System.out.println("I'M EVERYWHERE : " + classesDirectory);
andre15silva marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
protected CoveredTestResultPerTestMethod executeCoveredTestPerTestMethod(RuntimeData data, String classesDirectory, String[] testClassNames, String[] testMethodNames) {
System.out.println("I'M HERE : " + classesDirectory);
andre15silva marked this conversation as resolved.
Show resolved Hide resolved
final CoveredTestResultsPerJUnit4TestMethod listener = new CoveredTestResultsPerJUnit4TestMethod(data, classesDirectory, coverageTransformer);
JUnit4Runner.run(
testClassNames,
Expand All @@ -43,7 +45,7 @@ protected CoveredTestResultPerTestMethod executeCoveredTestPerTestMethod(Runtime
public static void main(String[] args) {
final ParserOptions options = ParserOptions.parse(args);
final String[] splittedArgs0 = options.getPathToCompiledClassesOfTheProject().split(ConstantsHelper.PATH_SEPARATOR);
final String classesDirectory = splittedArgs0[0];
final String classesDirectory = options.isCoverTests() ? options.getPathToCompiledClassesOfTheProject() : splittedArgs0[0];
final String testClassesDirectory = splittedArgs0[1];
new JUnit4JacocoRunnerCoveredResultPerTestMethod(
classesDirectory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected CoveragePerTestMethod executeTestPerTestMethod(RuntimeData data,
public static void main(String[] args) {
final ParserOptions options = ParserOptions.parse(args);
final String[] splittedArgs0 = options.getPathToCompiledClassesOfTheProject().split(ConstantsHelper.PATH_SEPARATOR);
final String classesDirectory = splittedArgs0[0];
final String classesDirectory = options.isCoverTests() ? options.getPathToCompiledClassesOfTheProject() : splittedArgs0[0];
final String testClassesDirectory = splittedArgs0[1];
new JUnit4JacocoRunnerPerTestMethod(
classesDirectory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public JUnit5JacocoRunner(String classesDirectory, String testClassesDirectory,
public static void main(String[] args) {
final ParserOptions options = ParserOptions.parse(args);
final String[] splittedArgs0 = options.getPathToCompiledClassesOfTheProject().split(ConstantsHelper.PATH_SEPARATOR);
final String classesDirectory = splittedArgs0[0];
final String classesDirectory = options.isCoverTests() ? options.getPathToCompiledClassesOfTheProject() : splittedArgs0[0];
final String testClassesDirectory = splittedArgs0[1];
final JacocoRunner jacocoRunner =
new JUnit5JacocoRunner(classesDirectory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected CoveredTestResultPerTestMethod executeCoveredTestPerTestMethod(Runtime
public static void main(String[] args) {
final ParserOptions options = ParserOptions.parse(args);
final String[] splittedArgs0 = options.getPathToCompiledClassesOfTheProject().split(ConstantsHelper.PATH_SEPARATOR);
final String classesDirectory = splittedArgs0[0];
final String classesDirectory = options.isCoverTests() ? options.getPathToCompiledClassesOfTheProject() : splittedArgs0[0];
final String testClassesDirectory = splittedArgs0[1];
new JUnit5JacocoRunnerCoveredResultPerTestMethod(
classesDirectory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected CoveragePerTestMethod executeTestPerTestMethod(RuntimeData data,
public static void main(String[] args) {
final ParserOptions options = ParserOptions.parse(args);
final String[] splittedArgs0 = options.getPathToCompiledClassesOfTheProject().split(ConstantsHelper.PATH_SEPARATOR);
final String classesDirectory = splittedArgs0[0];
final String classesDirectory = options.isCoverTests() ? options.getPathToCompiledClassesOfTheProject() : splittedArgs0[0];
final String testClassesDirectory = splittedArgs0[1];
new JUnit5JacocoRunnerPerTestMethod(
classesDirectory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public JacocoRunner(String classesDirectory, String testClassesDirectory, Covera
this(classesDirectory, testClassesDirectory, Collections.emptyList(), coverageTransformer);
}

/**
* @param classesDirectory the path to the directory that contains the .class file of sources
* @param testClassesDirectory the path to the directory that contains the .class file of test sources
* @param blackList the names of the test methods to NOT be run.
*/
/**
* @param classesDirectory the path to the directory that contains the .class file of sources
* @param testClassesDirectory the path to the directory that contains the .class file of test sources
* @param blackList the names of the test methods to NOT be run.
*/
public JacocoRunner(String classesDirectory, String testClassesDirectory, List<String> blackList, CoverageTransformer coverageTransformer) {
try {
this.instrumentedClassLoader = new MemoryClassLoader(
Expand All @@ -79,7 +79,10 @@ public JacocoRunner(String classesDirectory, String testClassesDirectory, List<S
this.instrumenter = new Instrumenter(this.runtime);
this.coverageTransformer = coverageTransformer;
// instrument source code
instrumentAll(classesDirectory);
String[] paths = classesDirectory.split(File.pathSeparator);
for (String path : paths) {
instrumentAll(path);
}
}


Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void setUp() throws Exception {
EntryPoint.outPrintStream = null;
EntryPoint.errPrintStream = null;
EntryPoint.jUnit5Mode = true;
EntryPoint.coverTests = false;
EntryPoint.coverageDetail = ParserOptions.CoverageTransformerDetail.SUMMARIZED;
}

Expand Down Expand Up @@ -501,4 +502,45 @@ Test the runCoveredTestResultPerTestMethods() of EntryPoint.
assertEquals(5, coverageDetailed.getDetailedCoverage().size());
}

@Test
public void testRunCoveredTestResultPerTestMethodsDetailedCoverageCoverTests() throws Exception {
EntryPoint.coverageDetail = ParserOptions.CoverageTransformerDetail.DETAIL;
EntryPoint.coverTests = true;

/*
Test the runCoveredTestResultPerTestMethods() of EntryPoint.
It should return the CoveredTestResultPerTestMethod with the instruction coverage computed by Jacoco.
*/
final String classpath = MAVEN_HOME + "org/jacoco/org.jacoco.core/0.7.9/org.jacoco.core-0.7.9.jar" + ConstantsHelper.PATH_SEPARATOR +
MAVEN_HOME + "org/ow2/asm/asm-debug-all/5.2/asm-debug-all-5.2.jar" + ConstantsHelper.PATH_SEPARATOR +
MAVEN_HOME + "commons-io/commons-io/2.5/commons-io-2.5.jar" + ConstantsHelper.PATH_SEPARATOR +
JUNIT_CP + ConstantsHelper.PATH_SEPARATOR + JUNIT5_CP;

final CoveredTestResultPerTestMethod coveredTestResultPerTestMethod = EntryPoint.runCoveredTestResultPerTestMethods(
classpath + ConstantsHelper.PATH_SEPARATOR + TEST_PROJECT_CLASSES,
TEST_PROJECT_CLASSES,
"junit5.TestSuiteExample",
new String[]{"test8", "test3"}
);

// Assert test results
assertEquals(2, coveredTestResultPerTestMethod.getRunningTests().size());
assertEquals(2, coveredTestResultPerTestMethod.getPassingTests().size());
assertEquals(0, coveredTestResultPerTestMethod.getFailingTests().size());
assertEquals(0, coveredTestResultPerTestMethod.getIgnoredTests().size());

// Assert detailed coverage
assertEquals(2, coveredTestResultPerTestMethod.getCoverageResultsMap().size());

assertTrue(coveredTestResultPerTestMethod.getCoverageOf("test3") instanceof CoverageDetailed);
CoverageDetailed coverageDetailed = (CoverageDetailed) coveredTestResultPerTestMethod.getCoverageOf("test3");
assertNotNull(coverageDetailed.getDetailedCoverage());
assertEquals(16, coverageDetailed.getDetailedCoverage().size());

assertTrue(coveredTestResultPerTestMethod.getCoverageOf("test8") instanceof CoverageDetailed);
coverageDetailed = (CoverageDetailed) coveredTestResultPerTestMethod.getCoverageOf("test8");
assertNotNull(coverageDetailed.getDetailedCoverage());
assertEquals(16, coverageDetailed.getDetailedCoverage().size());
}

}
42 changes: 42 additions & 0 deletions src/test/java/eu/stamp_project/testrunner/EntryPointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void setUp() {
EntryPoint.verbose = true;
EntryPoint.setMutationEngine(ConstantsHelper.MutationEngine.DESCARTES);
EntryPoint.coverageDetail = ParserOptions.CoverageTransformerDetail.SUMMARIZED;
EntryPoint.coverTests = false;
}

@After
Expand Down Expand Up @@ -598,4 +599,45 @@ Test the runCoveredTestResultPerTestMethods() of EntryPoint.
assertEquals(5, coverageDetailed.getDetailedCoverage().size());
}

@Test
public void testRunCoveredTestResultPerTestMethodsDetailedCoverageCoverTests() throws Exception {
EntryPoint.coverageDetail = ParserOptions.CoverageTransformerDetail.DETAIL;
EntryPoint.coverTests = true;

/*
Test the runCoveredTestResultPerTestMethods() of EntryPoint.
It should return the CoveredTestResultPerTestMethod with the instruction coverage computed by Jacoco.
*/
final String classpath = MAVEN_HOME + "org/jacoco/org.jacoco.core/0.7.9/org.jacoco.core-0.7.9.jar" + ConstantsHelper.PATH_SEPARATOR +
MAVEN_HOME + "org/ow2/asm/asm-debug-all/5.2/asm-debug-all-5.2.jar" + ConstantsHelper.PATH_SEPARATOR +
MAVEN_HOME + "commons-io/commons-io/2.5/commons-io-2.5.jar" + ConstantsHelper.PATH_SEPARATOR +
JUNIT_CP + ConstantsHelper.PATH_SEPARATOR + JUNIT5_CP;

final CoveredTestResultPerTestMethod coveredTestResultPerTestMethod = EntryPoint.runCoveredTestResultPerTestMethods(
classpath + ConstantsHelper.PATH_SEPARATOR + TEST_PROJECT_CLASSES,
TEST_PROJECT_CLASSES,
"example.TestSuiteExample",
new String[]{"test8", "test3"}
);

// Assert test results
assertEquals(2, coveredTestResultPerTestMethod.getRunningTests().size());
assertEquals(2, coveredTestResultPerTestMethod.getPassingTests().size());
assertEquals(0, coveredTestResultPerTestMethod.getFailingTests().size());
assertEquals(0, coveredTestResultPerTestMethod.getIgnoredTests().size());

// Assert detailed coverage
assertEquals(2, coveredTestResultPerTestMethod.getCoverageResultsMap().size());

assertTrue(coveredTestResultPerTestMethod.getCoverageOf("test3") instanceof CoverageDetailed);
CoverageDetailed coverageDetailed = (CoverageDetailed) coveredTestResultPerTestMethod.getCoverageOf("test3");
assertNotNull(coverageDetailed.getDetailedCoverage());
assertEquals(16, coverageDetailed.getDetailedCoverage().size());

assertTrue(coveredTestResultPerTestMethod.getCoverageOf("test8") instanceof CoverageDetailed);
coverageDetailed = (CoverageDetailed) coveredTestResultPerTestMethod.getCoverageOf("test8");
assertNotNull(coverageDetailed.getDetailedCoverage());
assertEquals(16, coverageDetailed.getDetailedCoverage().size());
}

}