I think that it all depends on what you are trying to accomplish with the test(s). Practically speaking, I use contracts and assertions more than anything else. As a general rule, a test should never throw an exception via a try/catch
block. Instead, you should handle the exception more discretely, for example, wrapping it and then handling it in another class that is independent of your test class.
Using contracts is a great way of setting up pre-conditions for your tests to make them more focused. As I mentioned already, I use these and asserts more than anything. With our framework (Selenium WebDriver) here that the office, we use '@Test' to flag that this is a test and then compliment that by calling out the 'bucket' it fits into like, @Category(SomeTestClass.class)
.
When using asserts, it's important to keep in mind which type you are using. Some stop code execution while others allow for 'fall through' (much like in a switch/case
statement). Also, when using them, we try to set the assert up so that you end with a True
condition as opposed to a false one. Much like this:
assertTrue("Product breadcrumbs not showing", productCQPage.areBreadCrumbsShowing());
In the example above, I use a boolean to verify if the bread crumbs are showing or not, and in this case, the assert will only fire if that boolean returns false. So again, I am entering with a negative but asserting a positive. Confusing, right? :)
I hope this in some way is helpful to you!