When a test passes the first time it’s ever run, a developer’s reaction is “Great! Let’s move on!”. Well this can be a dangerous practice as I discovered one cold rainy day.
It was a cold rainy day (kind of common in Dublin), I was happy enough with my test results being all shiny green, when I decided to do some exploratory testing. To my surprise I discovered that an element on a web page that had always been there before, was gone, departed, vanished!
First reaction was to say, where the hell is it? I run some investigation and I saw the cause of it, no worries, it got knocked out by the last change, easy fix. The worst feeling had yet to come, in fact when I went to write a test for that scenario I saw that there was already an existing one checking for exactly that specific element existence… WHAT? The damn test had passed and was staring at me in its shiny green suit!
When we write automated tests be it a unit test, acceptance or any other type of test it is extremely important that we make it FAIL at least ONCE.
In fact, until you make a test FAIL, you will never know if the damn bastard passes because the code under test is correct or because the implementation of the test itself is wrong.
A test that never fails is worse than having no test at all because gives the false confidence that some code is tested and clean while it might be completely wrong now or any other cold rainy day in Dublin after a re-factor or a new push and we will never know because IT WILL NEVER FAIL.
If you don’t follow what I’m talking about, have a look at this example:
Take a Web app and say I want to verify that one field is visible in the UI at a certain stage.
What I do is to build automation that performs a series of actions and at the end I will verify whether I can see that field or not.
To do this I will create a method isFieldVisible() that returns true or false depending on whether the field is visible or not, so that I can assertTrue(isFieldVisible(myField));
When this test passes I am only half way there because I need to demonstrate that when the field is not visible isFieldVisible() does return false, otherwise my test might never fail
To do this I write a temporary extra step in the automation that hides the field and then run the same assertion again
assertTrue(isFieldVisible(myField));
At this point I expect the assertion to fail, if it doesn’t it means that I just wrote a very dangerous ALWAYS-GREEN test
What if I did write a very dangerous ALWAYS-GREEN test? What do I do now?
I must change the code (the test code, not the app under test code) until the test FAILS, when it fails for the first time and the original test is still green I can be sure that the test can fail and will fail in the future if after a re-factor or any other change that introduces regression, rainy day or not.
At this point you might argue that, rather than simply changing the test to make it fail and revert it to the original test, we should write the negative test and execute it as part of the automation.
It is an interesting point and the answer depends on the specific situation. In some cases a negative test can be as important as the original test and it is necessary for covering a different path in the code, but this is not always the case and we will have to make an informed call every time.
Example1 – When writing a negative test makes sense:
I want to verify that when I hit the “Customer Feedback link” my “Company Search box” can still be seen by the user.
I write the following test:
To make it fail I will add an extra temporary step:
If the original test was green then this test MUST FAIL (otherwise we have written the very dangerous ALWAYS-GREEN test)
At this point I notice that this is a valid scenario and I can write a test for it (if I don’t have it already).
The test will be
I have positive and negative scenarios covered. The negative scenario verifies that the “Hide Search Box link” functionality works as expected.
Example2 – When writing a negative test does not make sense:
I want to verify that after performing a search for a company and getting search results back, the value of the latest search is persisted in the search box.
I write the following test:
To make it fail I remove the second and third step
If the original test was green then this test MUST FAIL (otherwise we have written the very dangerous ALWAYS-GREEN test)
At this point I look a the tests and realise that there is no point in adding a negative test like the above (the one with 2 steps only) because if we don’t actually type ” Jackie Treehorn corp.” in the field, it is very unlikely that Jackie Treehorn or Jeff Lebowsky or any other cool character will suddenly appear in a search box magically so I decide that a negative test is not required
To recap:
1. When you write a test you MUST be able to make it fail to demonstrate its implementation is valid in particular if it is a cold rainy day.
2. If while you make the test fail you realise that this represents a new valid scenario to be tested then write the scenario and a separate test with the negative assertion, it might come useful on one hot sunny day.




