Skip to content

Commit

Permalink
Review testing of failed assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
javiertuya committed Aug 14, 2024
1 parent 0b7488e commit b1b5c07
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package giis.visualassert;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

Expand All @@ -16,7 +16,6 @@ public void testSoftNoFail() {
SoftVisualAssert va = new SoftVisualAssert();
va.assertEquals("ab cd", "ab cd");
va.assertEquals("xy vw", "xy vw");
va.assertAll();
}

/*
Expand Down Expand Up @@ -47,12 +46,12 @@ public static void doFailSoftAssert(SoftVisualAssert va, String expected, String
va.assertEquals("this is notnull", null, "msgan", "fan.html");
va.assertEquals("xy vw", "xy zz vw", "msg5");
assertEquals(5, va.getFailureCount());
boolean success = false;
try {
if ("".equals(aggregateFile))
va.assertAll();
else
va.assertAll(aggregateFile);
fail("this should fail");
} catch (AssertionError e) {
//first transforms the file name in expected mesage to include the path
expected=expected.replace("f1.html", FileUtil.getPath(reportPath, "f1.html"));
Expand All @@ -61,7 +60,9 @@ public static void doFailSoftAssert(SoftVisualAssert va, String expected, String
expected=expected.replace("diff-0.html", FileUtil.getPath(reportPath, "diff-0.html"));
expected=expected.replace("Aggregate.html", FileUtil.getPath(reportPath, "Aggregate.html"));
assertEquals(CallStack.normalize(expected), CallStack.normalize(e.getMessage()));
success = true;
}
assertTrue(success);

//assertAll resets the list
assertEquals(0, va.getFailureCount());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package giis.visualassert;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

Expand Down Expand Up @@ -38,35 +38,41 @@ public void testSingleStackItem() {
SoftVisualAssert va = new SoftVisualAssert(); //default stack 1
va.assertEquals("ab zz cd", "ab cd", "", "fstack11.html");
callDoAssert(va);
boolean success = false;
try {
va.assertAll();
fail("this should fail");
} catch (AssertionError e) {
assertEquals(CallStack.normalize(getExpectedSingleStackItem()), CallStack.normalize(e.getMessage()));
success=true;
}
assertTrue(success);
}
@Test
public void testMultipleStackItem() {
SoftVisualAssert va = new SoftVisualAssert().setCallStackLength(3);
callDoAssert(va);
boolean success = false;
try {
va.assertAll();
fail("this should fail");
} catch (AssertionError e) {
assertEquals(CallStack.normalize(getExpectedMultipleStackItem()), CallStack.normalize(e.getMessage()));
success=true;
}
assertTrue(success);
}
@Test
public void testZeroStackItem() {
SoftVisualAssert va = new SoftVisualAssert().setCallStackLength(0);
va.assertEquals("ab zz cd", "ab cd", "", "fstack11.html");
callDoAssert(va);
boolean success = false;
try {
va.assertAll();
fail("this should fail");
} catch (AssertionError e) {
assertEquals(CallStack.normalize(getExpectedZeroStackItem()), CallStack.normalize(e.getMessage()));
success=true;
}
assertTrue(success);
}

private String getExpectedSingleStackItem() {
Expand Down
15 changes: 11 additions & 4 deletions java/src/test/java/giis/visualassert/TestVisualAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.List;

Expand Down Expand Up @@ -57,17 +56,21 @@ public void testFailAllConditionsFalse() {
public static void doFailAllConditionsFalse(VisualAssert va, String assertionException, String assertMessage, String expActMessage) {
FileUtil.createDirectory(defaultFolder); // ensure folder exists
FileUtil.fileWrite(FileUtil.getPath(defaultFolder, diffFile), "");
// Always use this pattern with a boolean variable to check that assert exception is raised
// to avoid problems with exception hierarchy across platforms and frameworks.
boolean success = false;
try {
va.assertEquals(expected, actualFail, "This is the additional message", diffFile);
fail("this should fail");
} catch (AssertionError e) {
assertEquals(assertionException, e.getClass().getName()); //not a subclass of this exception
//first transforms the file name in expected message to include the path
String message=expectedMessageShort.replace(diffFile, FileUtil.getPath(JavaCs.DEFAULT_REPORT_SUBDIR, diffFile));
message+=expActMessage;
assertEquals(message, e.getMessage());
assertEquals(htmlDiffs, FileUtil.fileRead(FileUtil.getPath(defaultFolder, diffFile)));
success = true;
}
assertTrue(success);
}

@Test
Expand All @@ -80,9 +83,9 @@ public void testFailAllConditionsTrue() {
.setSoftDifferences(true)
.setBrightColors(true)
.setReportSubdir(tempReportPath);
boolean success = false;
try {
va.assertEquals(expected, actualFail);
fail("this should fail");
} catch (AssertionError e) {
//get file and path of the generated diff file (only file in the folder)
List<String> allFiles = FileUtil.getFileListInDirectory(tempReportPath);
Expand All @@ -102,7 +105,9 @@ public void testFailAllConditionsTrue() {
assertEquals(expectedMessageLong.replace("\r", ""), e.getMessage().replace("\r", ""));
assertEquals(htmlDiffs.replace("&nbsp;", " ").replace("e6ffe6", "00ff00").replace("ffe6e6", "ff4000"),
FileUtil.fileRead(FileUtil.getPath(tempReportPath, diffFileName)));
success = true;
}
assertTrue(success);
}

@Test
Expand All @@ -114,12 +119,14 @@ public void testFailWithNulls() {
+ "- Visual diffs at: target/diff-1.html", "null-expected.html");
}
private void doAssertNulls(VisualAssert va, String expected, String actual, String message, String expectedMessage, String htmlFile) {
boolean success = false;
try {
va.assertEquals(expected, actual, message);
fail("this should fail");
} catch (AssertionError e){
assertEquals(expectedMessage, e.getMessage().replace("\\", "/"));
success = true;
}
assertTrue(success);
}

@Test
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions net/TestVisualAssert/Giis.Visualassert/TestVisualAssert.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b1b5c07

Please sign in to comment.