Last active
February 16, 2017 21:19
-
-
Save jasonrobot/1d164146b278cfb0d5cc18ab9e287642 to your computer and use it in GitHub Desktop.
The magic of assertions
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
if (3 != 3) | |
{ | |
fail("equality check failed on 3"); | |
} | |
/* | |
* That's fine, except that we're usually comparing dynamic data retrieved from the app. | |
* Sometimes we aren't even comparing against static data | |
*/ | |
for (String result : results) | |
{ | |
if ( page.verifyData(result) == false) | |
{ | |
fail("result did not match expected value"); | |
} | |
} | |
/* | |
* In this example, we have no knowledge of what was actually being compared. | |
* We only know whether it was successful or not. This might lead you to think about printing out the data | |
* before comparing it, but that leads to more complex code (less readable, more boilerplate), and more | |
* noise in the logs, with little benefit | |
*/ | |
/* | |
* The proper way is to use assertions. Javadoc: http://testng.org/javadocs/org/testng/Assert.html | |
*/ | |
assertEquals(3, 4, "equality check failed on 3"); | |
/* | |
* This is functionally equivalent to the first validation statement in this example file. It will even print the | |
* same message. But, if the condition fails, it will also print the data that it failed on. | |
* | |
* java.lang.AssertionError: equality check failed on 3 expected [4] but found [3] | |
* Expected :4 | |
* Actual :3 | |
*/ | |
/* | |
* All assertion methods take at least two parameters: The actual value, and the expected value (in that order) | |
* | |
* They also all are overloaded to take a 3rd argument, a string with a message for when the assertion fails. | |
*/ | |
/* | |
* There are assertions for just about anything you can imagine, and assertEquals is overloaded for most types | |
*/ | |
String[] foo = {"foo", "bar"}; | |
String[] bar = {"bar", "foo"}; | |
//fails | |
assertEquals(foo, bar); | |
/* java.lang.AssertionError: Lists differ at element [0]: bar != foo expected [bar] but found [foo] | |
* Expected :bar | |
* Actual :foo | |
*/ | |
//passes | |
assertEqualsNoOrder(foo, bar); | |
/* | |
* Using this assetion, we can rewrite the second example to be: | |
*/ | |
assertEquals(page.getData(), results); | |
Object nothing = null; | |
assertNotNull(nothing); | |
/* java.lang.AssertionError: expected object to not be null | |
*/ | |
Integer three = new Integer(3); | |
Integer tres = three; | |
Integer anotherThree = new Integer(3); | |
//passes | |
assertSame(tres, three); | |
//fails | |
assertSame(anotherThree, three); | |
/* java.lang.AssertionError: expected [3] but found [3] | |
* Expected :3 | |
* Actual :3 | |
*/ | |
/* | |
* ok, so that error message sucks, but you can provide your own. | |
*/ | |
assertSame(anotherThree, three, "Objects were not the same! Values were:"); | |
/* | |
* There are plenty more assertions too. Go read the javadocs! | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment