Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1209 from fhoeben/rerunFailures
Allow rerun of failed tests (only)
- Loading branch information
Showing
8 changed files
with
273 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package fitnesse.reporting; | ||
|
||
import fitnesse.testsystems.TestPage; | ||
import fitnesse.testsystems.TestSummary; | ||
|
||
import java.io.Closeable; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.time.LocalDateTime; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* Creates a wiki page with all pages that either failed or threw exception | ||
*/ | ||
public class RerunSuiteFormatter extends BaseFormatter implements Closeable { | ||
private static final Logger LOG = Logger.getLogger(RerunSuiteFormatter.class.getName()); | ||
|
||
private final File wikiFile; | ||
private final PrintWriter pw; | ||
private int errorCount = 0; | ||
|
||
public RerunSuiteFormatter(File targetFile) throws IOException { | ||
wikiFile = targetFile; | ||
if (!wikiFile.getParentFile().exists()) { | ||
wikiFile.getParentFile().mkdirs(); | ||
} else if (wikiFile.exists()) { | ||
wikiFile.delete(); | ||
} | ||
LOG.fine("Rerun suite will be made in: " + wikiFile.getAbsolutePath()); | ||
pw = new PrintWriter(wikiFile, "utf-8"); | ||
} | ||
|
||
@Override | ||
public void testComplete(TestPage testPage, TestSummary testSummary) { | ||
if (testSummary.getExceptions() > 0) { | ||
recordFailure(testPage); | ||
} else if (testSummary.getWrong() > 0) { | ||
recordFailure(testPage); | ||
} | ||
} | ||
|
||
@Override | ||
public int getErrorCount() { | ||
return errorCount; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
pw.close(); | ||
// no content -> remove file | ||
if (getErrorCount() == 0 && wikiFile.exists()) { | ||
wikiFile.delete(); | ||
} | ||
} | ||
|
||
protected void recordFailure(TestPage testPage) { | ||
String testPageName = testPage.getName(); | ||
if (!"SuiteSetUp".equals(testPageName) | ||
&& !"SuiteTearDown".equals(testPageName)) { | ||
errorCount++; | ||
if (errorCount == 1) { | ||
appendHeader(pw); | ||
} | ||
appendPageFailure(pw, testPage); | ||
pw.flush(); | ||
} | ||
} | ||
|
||
protected void appendHeader(PrintWriter writer) { | ||
writer.append("---\n" + | ||
"Help: Lists tests failed during last run, so they can be run again (without running all tests that passed).\n" + | ||
"Suite\n" + | ||
"---\n" + | ||
"\n"); | ||
writer.append("!note This page is automatically generated when running tests. "); | ||
writer.append("It will be overwritten by the next Suite or Test execution.\n\n"); | ||
writer.append("Tests failed (first failure was at "); | ||
writer.append(LocalDateTime.now().toString()); | ||
writer.append("):\n\n"); | ||
} | ||
|
||
protected void appendPageFailure(PrintWriter writer, TestPage testPage) { | ||
String pagePath = testPage.getFullPath(); | ||
writer.append("!see [["); | ||
writer.append(pagePath); | ||
writer.append("][."); | ||
writer.append(pagePath); | ||
writer.append("]]\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package fitnesse.reporting; | ||
|
||
import fitnesse.testrunner.WikiTestPage; | ||
import fitnesse.testsystems.ExecutionResult; | ||
import fitnesse.testsystems.TestSummary; | ||
import fitnesse.wiki.PathParser; | ||
import fitnesse.wiki.WikiPage; | ||
import fitnesse.wiki.WikiPageUtil; | ||
import fitnesse.wiki.fs.InMemoryPage; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import util.FileUtil; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class RerunSuiteFormatterTest { | ||
private final WikiPage wikiPageRoot = InMemoryPage.makeRoot("root"); | ||
private final WikiPage wikiSuitePage = WikiPageUtil.addPage(wikiPageRoot, PathParser.parse("Suite"), "suite content"); | ||
|
||
private File targetFile; | ||
private RerunSuiteFormatter formatter; | ||
|
||
@Before | ||
public void setUp() throws IOException { | ||
targetFile = File.createTempFile("root", "rerun.wiki"); | ||
formatter = new RerunSuiteFormatter(targetFile); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
if (targetFile != null && targetFile.exists()) { | ||
targetFile.delete(); | ||
} | ||
} | ||
|
||
@Test | ||
public void noErrorOrFailedNoFile() throws IOException { | ||
sendTestComplete(ExecutionResult.PASS, "PassedPage"); | ||
formatter.close(); | ||
|
||
assertEquals(0, formatter.getErrorCount()); | ||
|
||
assertFalse(targetFile.exists()); | ||
} | ||
|
||
@Test | ||
public void addRefOnError() throws IOException { | ||
sendTestComplete(ExecutionResult.ERROR, "ErrorPage"); | ||
formatter.close(); | ||
|
||
assertEquals(1, formatter.getErrorCount()); | ||
|
||
List<String> xrefs = getReferencedPages(); | ||
assertEquals(".Suite.ErrorPage", xrefs.get(0)); | ||
} | ||
|
||
@Test | ||
public void addRefOnFailure() throws IOException { | ||
sendTestComplete(ExecutionResult.FAIL, "FailedPage"); | ||
formatter.close(); | ||
|
||
assertEquals(1, formatter.getErrorCount()); | ||
|
||
List<String> xrefs = getReferencedPages(); | ||
assertEquals(".Suite.FailedPage", xrefs.get(0)); | ||
} | ||
|
||
@Test | ||
public void addRefsOnMultiple() throws IOException { | ||
sendTestComplete(ExecutionResult.FAIL, "FailedPage"); | ||
sendTestComplete(ExecutionResult.PASS, "PassedPage"); | ||
sendTestComplete(ExecutionResult.ERROR, "ErrorPage"); | ||
sendTestComplete(ExecutionResult.FAIL, "FailedPage2"); | ||
formatter.close(); | ||
|
||
assertEquals(3, formatter.getErrorCount()); | ||
|
||
List<String> xrefs = getReferencedPages(); | ||
assertEquals(".Suite.FailedPage", xrefs.get(0)); | ||
assertEquals(".Suite.ErrorPage", xrefs.get(1)); | ||
assertEquals(".Suite.FailedPage2", xrefs.get(2)); | ||
} | ||
|
||
@Test | ||
public void addRefPagenameNotWikiWord() throws IOException { | ||
sendTestComplete(ExecutionResult.FAIL, "Failedpage"); | ||
formatter.close(); | ||
|
||
assertEquals(1, formatter.getErrorCount()); | ||
|
||
List<String> xrefs = getReferencedPages(); | ||
assertEquals(".Suite.Failedpage", xrefs.get(0)); | ||
} | ||
|
||
@Test | ||
public void suiteSetUpAndSuiteTearDownAreIgnored() throws IOException { | ||
sendTestComplete(ExecutionResult.ERROR, "SuiteSetUp"); | ||
sendTestComplete(ExecutionResult.ERROR, "ErrorPage"); | ||
sendTestComplete(ExecutionResult.ERROR, "SuiteTearDown"); | ||
formatter.close(); | ||
|
||
assertEquals(1, formatter.getErrorCount()); | ||
|
||
List<String> xrefs = getReferencedPages(); | ||
assertEquals(".Suite.ErrorPage", xrefs.get(0)); | ||
} | ||
|
||
private List<String> getReferencedPages() throws IOException { | ||
String rerunPageContent = FileUtil.getFileContent(targetFile); | ||
assertTrue(rerunPageContent.startsWith("---\nHelp: ")); | ||
|
||
WikiPage root = InMemoryPage.makeRoot("RooT"); | ||
WikiPage page = WikiPageUtil.addPage(root, PathParser.parse("PageName"), rerunPageContent); | ||
return WikiPageUtil.getXrefPages(page); | ||
} | ||
|
||
private void sendTestComplete(ExecutionResult result, String pageName) { | ||
WikiPage wikiTestPage = WikiPageUtil.addPage(wikiSuitePage, PathParser.parse(pageName), "content"); | ||
formatter.testComplete(new WikiTestPage(wikiTestPage), createTestSummary(result)); | ||
} | ||
|
||
private TestSummary createTestSummary(ExecutionResult fail) { | ||
TestSummary testSummary = new TestSummary(); | ||
testSummary.add(fail); | ||
return testSummary; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters