| 1 | package fr.sii.ogham.testing.extension.junit; | |
| 2 | ||
| 3 | import org.junit.rules.TestWatcher; | |
| 4 | import org.junit.runner.Description; | |
| 5 | ||
| 6 | import fr.sii.ogham.testing.extension.common.Printer; | |
| 7 | import fr.sii.ogham.testing.extension.common.TestInformationLogger; | |
| 8 | ||
| 9 | /** | |
| 10 | * Write information about test. This is useful when there are many tests: | |
| 11 | * <ul> | |
| 12 | * <li>To quickly find the logs for the test</li> | |
| 13 | * <li>To quickly know if the test has failed or succeeded</li> | |
| 14 | * <li>To quickly identify the test failure</li> | |
| 15 | * <li>To quickly find failed tests</li> | |
| 16 | * </ul> | |
| 17 | * | |
| 18 | * @author Aurélien Baudet | |
| 19 | * | |
| 20 | */ | |
| 21 | public class LoggingTestRule extends TestWatcher { | |
| 22 | private static final String SEPARATOR = "."; | |
| 23 | ||
| 24 | private final TestInformationLogger logger; | |
| 25 | ||
| 26 | /** | |
| 27 | * Initializes with 100 characters per line, slf4j marker and "test-info" | |
| 28 | * marker. | |
| 29 | * | |
| 30 | */ | |
| 31 | public LoggingTestRule() { | |
| 32 | super(); | |
| 33 | this.logger = new TestInformationLogger(); | |
| 34 | } | |
| 35 | ||
| 36 | /** | |
| 37 | * Initializes with the provided max line length. | |
| 38 | * | |
| 39 | * Uses Slf4j logger and default marker ("test-info"). | |
| 40 | * | |
| 41 | * @param maxLength | |
| 42 | * the length of each line | |
| 43 | */ | |
| 44 | public LoggingTestRule(int maxLength) { | |
| 45 | super(); | |
| 46 | this.logger = new TestInformationLogger(maxLength); | |
| 47 | } | |
| 48 | ||
| 49 | /** | |
| 50 | * Initializes with the provided max line length and marker. | |
| 51 | * | |
| 52 | * Uses Slf4j logger. | |
| 53 | * | |
| 54 | * @param maxLength | |
| 55 | * the length of each line | |
| 56 | * @param marker | |
| 57 | * the marker for logs | |
| 58 | */ | |
| 59 | public LoggingTestRule(int maxLength, String marker) { | |
| 60 | super(); | |
| 61 | this.logger = new TestInformationLogger(maxLength, marker); | |
| 62 | } | |
| 63 | ||
| 64 | /** | |
| 65 | * | |
| 66 | * @param maxLength | |
| 67 | * the length of each line | |
| 68 | * @param marker | |
| 69 | * the marker for logs | |
| 70 | * @param logger | |
| 71 | * the logger | |
| 72 | */ | |
| 73 | public LoggingTestRule(int maxLength, String marker, Printer logger) { | |
| 74 | super(); | |
| 75 | this.logger = new TestInformationLogger(maxLength, marker, logger); | |
| 76 | } | |
| 77 | ||
| 78 | @Override | |
| 79 | protected void starting(Description description) { | |
| 80 |
4
1. starting : removed call to fr/sii/ogham/testing/extension/common/TestInformationLogger::writeStart → SURVIVED 2. starting : removed call to fr/sii/ogham/testing/extension/common/TestInformationLogger::writeStart → NO_COVERAGE 3. starting : removed call to fr/sii/ogham/testing/extension/common/TestInformationLogger::writeStart → TIMED_OUT 4. starting : removed call to fr/sii/ogham/testing/extension/common/TestInformationLogger::writeStart → KILLED |
logger.writeStart(getTestName(description)); |
| 81 | } | |
| 82 | ||
| 83 | @Override | |
| 84 | protected void succeeded(Description description) { | |
| 85 |
4
1. succeeded : removed call to fr/sii/ogham/testing/extension/common/TestInformationLogger::writeSuccess → NO_COVERAGE 2. succeeded : removed call to fr/sii/ogham/testing/extension/common/TestInformationLogger::writeSuccess → SURVIVED 3. succeeded : removed call to fr/sii/ogham/testing/extension/common/TestInformationLogger::writeSuccess → TIMED_OUT 4. succeeded : removed call to fr/sii/ogham/testing/extension/common/TestInformationLogger::writeSuccess → KILLED |
logger.writeSuccess(getTestName(description)); |
| 86 | } | |
| 87 | ||
| 88 | @Override | |
| 89 | protected void failed(Throwable e, Description description) { | |
| 90 |
2
1. failed : removed call to fr/sii/ogham/testing/extension/common/TestInformationLogger::writeFailure → NO_COVERAGE 2. failed : removed call to fr/sii/ogham/testing/extension/common/TestInformationLogger::writeFailure → KILLED |
logger.writeFailure(getTestName(description), e); |
| 91 | } | |
| 92 | ||
| 93 | private static String getTestName(Description description) { | |
| 94 |
4
1. getTestName : replaced return value with "" for fr/sii/ogham/testing/extension/junit/LoggingTestRule::getTestName → SURVIVED 2. getTestName : replaced return value with "" for fr/sii/ogham/testing/extension/junit/LoggingTestRule::getTestName → NO_COVERAGE 3. getTestName : replaced return value with "" for fr/sii/ogham/testing/extension/junit/LoggingTestRule::getTestName → TIMED_OUT 4. getTestName : replaced return value with "" for fr/sii/ogham/testing/extension/junit/LoggingTestRule::getTestName → KILLED |
return description.getTestClass().getSimpleName() + SEPARATOR + description.getMethodName(); |
| 95 | } | |
| 96 | ||
| 97 | } | |
Mutations | ||
| 80 |
1.1 2.2 3.3 4.4 |
|
| 85 |
1.1 2.2 3.3 4.4 |
|
| 90 |
1.1 2.2 |
|
| 94 |
1.1 2.2 3.3 4.4 |