The following test script does too much:
@Test
public void canSortResultsTest() {
HomePage homePage = new HomePage(driver);
homePage.open();
Assert.assertTrue(homePage.isDisplayed(), “home page is not displayed!”);
ResultsPage resultsPage = homePage.search(keyword);
Assert.assertTrue(resultsPage.isDisplayed(),
“results page is not displayed!”);
resultsPage.sortResultsByAuthor();
Assert.assertTrue(resultsPage.areResultsSortedByAuthor());
resultsPage.sortResultsByTitle();
Assert.assertTrue(resultsPage.areResultsSortedByTitle());
resultsPage.sortResultsByPublicationDate();
Assert.assertTrue(resultsPage.areResultsSortedByPublicationDate());
}
It is doing 3 things:
sorting results by author and checking that results are sorted correctly
sorting results by title and checking that results are sorted correctly
sorting results by publication date and checking that results are sorted correctly
You may say that this is ok since the test verifies everything about sorting results.
Everything it does is about verifying a sort order or another.
But this is not correct.
If the test fails while verifying the sorting by author, the code about sorting by title and publication date is not executed at all. Worse, we will know that the sorting by author fails but not know anything about the other 2 sort orders.
Also, if the test fails, it is difficult to know right away what went wrong, without looking into the code. The test script’s name is too vague.
Also, the test script’s execution is longer than needed.
For these reasons, we should split our test in 3 tests:
@Test
public void canSortResultsByAuthorTest() {
HomePage homePage = new HomePage(driver);
homePage.open();
Assert.assertTrue(homePage.isDisplayed(), “home page is not displayed!”);
ResultsPage resultsPage = homePage.search(keyword);
Assert.assertTrue(resultsPage.isDisplayed(),
“results page is not displayed!”);
resultsPage.sortResultsByAuthor();
Assert.assertTrue(resultsPage.areResultsSortedByAuthor());
}
@Test
public void canSortResultsByTitleTest() {
HomePage homePage = new HomePage(driver);
homePage.open();
Assert.assertTrue(homePage.isDisplayed(), “home page is not displayed!”);
ResultsPage resultsPage = homePage.search(keyword);
Assert.assertTrue(resultsPage.isDisplayed(),
“results page is not displayed!”);
resultsPage.sortResultsByTitle();
Assert.assertTrue(resultsPage.areResultsSortedByTitle());
}
@Test
public void canSortResultsByPublicationDateTest() {
HomePage homePage = new HomePage(driver);
homePage.open();
Assert.assertTrue(homePage.isDisplayed(), “home page is not displayed!”);
ResultsPage resultsPage = homePage.search(keyword);
Assert.assertTrue(resultsPage.isDisplayed(),
“results page is not displayed!”);
resultsPage.sortResultsByPublicationDate();
Assert.assertTrue(resultsPage.areResultsSortedByPublicationDate());
}
Really, Alex, this is the solution?
We have now 3 tests instead of 1 and much more code.
That’s right, we have more tests and more code but also
each test is focused on one thing
each test has a very specific name
since the tests are independent, if the first test fails, the other 2 still run
if a test fails, it is clear from the test name where the issue is
all tests are shorter than before
all tests can be executed in parallel
All tests should be independent, short and focused on doing one thing only.