Skip to content

Commit

Permalink
add test runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt committed Feb 7, 2022
1 parent 95ef4c1 commit 26a10ef
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Expand Up @@ -67,7 +67,7 @@ jobs:
run:
mvn test
- name: print run tests
run: cat testResults.md
run: cat testResults.spoon
coverage:
runs-on: ubuntu-latest
env:
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -31,6 +31,7 @@ spooned-classes/
spooned/
doc/_jekyll/_site/
*.tmp
testResults.spoon

# Nodejs
node_modules/
Expand All @@ -56,4 +57,3 @@ gradle-app.setting

## Gradle Enterprise ID File ##
.mvn/.gradle-enterprise/gradle-enterprise-workspace-id
testResults.md
51 changes: 44 additions & 7 deletions src/test/java/spoon/test/TestExecutionLogger.java
Expand Up @@ -5,6 +5,10 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;
import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.engine.support.descriptor.MethodSource;
import org.junit.platform.launcher.TestExecutionListener;
Expand All @@ -18,24 +22,40 @@
public class TestExecutionLogger implements TestExecutionListener {

private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private static Map<TestIdentifier, LocalDateTime> timers = new HashMap<>();
@Override
public void executionFinished(TestIdentifier testIdentifier,
TestExecutionResult testExecutionResult) {
// here we check if the execution was a test and not something like container or otherwise (like a suite)
if (testIdentifier.getType().isTest()) {
long duration = getTestRunTime(testIdentifier);
String classname = getClassName(testIdentifier);
// now we simply print the result to sysout here could be a real logger(Our logger is off during testing?), markdown to a file
// our more advanced logging happen.
String result = "executionFinished: `" + classname + "#" + testIdentifier.getDisplayName()
+ "` with result: `" + testExecutionResult.getStatus() + "`";
String result = "executionFinished: " + classname + "#" + testIdentifier.getDisplayName()
+ " with result: " + testExecutionResult.getStatus() + " in duration: " + duration
+ " miliseconds\n";
try {
Files.writeString(Path.of("testResults.md"), result, StandardOpenOption.APPEND, StandardOpenOption.CREATE);
Files.writeString(Path.of("testResults.spoon"), result, StandardOpenOption.APPEND,
StandardOpenOption.CREATE);
} catch (IOException e) {
LOGGER.error("Error writing to file", e);
}
}
}
/**
* Returns the time in milliseconds since the test started.
* @param testIdentifier the test identifier
* @return the time in milliseconds since the test started or 0 if any error occurred.
*/
private long getTestRunTime(TestIdentifier testIdentifier) {
LocalDateTime finish = LocalDateTime.now();
LocalDateTime startTime = timers.get(testIdentifier);
timers.remove(testIdentifier);
long duration = 0;
if (startTime != null) {
duration = startTime.until(finish, ChronoUnit.MILLIS);
}
return duration;
}
/**
* Returns the classname or empty string if the testIdentifier is not a methodsource.
* @param testIdentifier the testIdentifier to get the classname from.
Expand All @@ -52,11 +72,28 @@ private String getClassName(TestIdentifier testIdentifier) {

@Override
public void executionSkipped(TestIdentifier testIdentifier, String reason) {
// here land the skipped executions either by assumptions, annotations or by some other reason
LocalDateTime finish = LocalDateTime.now();
LocalDateTime startTime = timers.get(testIdentifier);
timers.remove(testIdentifier);
long duration = 0;
if (startTime != null) {
duration = startTime.until(finish, java.time.temporal.ChronoUnit.MILLIS);
}
if (testIdentifier.getType().isTest()) {
String classname = getClassName(testIdentifier);
String result = "executionSkipped: " + classname + "#" + testIdentifier.getDisplayName() +" duration: "+ duration + "miliseconds"
+ " with reason: " + reason+"\n";
try {
Files.writeString(Path.of("testResults.spoon"), result, StandardOpenOption.APPEND, StandardOpenOption.CREATE);
} catch (IOException e) {
LOGGER.error("Error writing to file", e);
}
}
}

@Override
public void executionStarted(TestIdentifier testIdentifier) {
timers.put(testIdentifier, LocalDateTime.now());
// here land all started executions for example test methods, test classes, test containers.
}
}

0 comments on commit 26a10ef

Please sign in to comment.