Skipping tests

Sometimes it can be useful to flag a test as "work-in-progress". In Serenity, you use the @Pending annotation, either for a test or for a @Step-annotated method, to indicate that the scenario is still being implemented and that the results are not available yet. These tests appear as 'Pending' (shown in blue) in the test reports.

@RunWith(SerenityRunner.class)
public class WhenEarningFrequentFlyerStatus {

    @Steps
    TravellerStatusSteps travellerSteps;

    @Test
    @Pending
    public void dropsBackToSilverIfLessThan8000PointsEarnedInAYear() {
    }

    @Test
    @Ignore
    public void earnPlatinumAfter10000Points() {
        // GIVEN
        travellerSteps.a_traveller_joins_the_frequent_flyer_program();

        // WHEN
        travellerSteps.the_traveller_flies(500000);

        // THEN
        travellerSteps.traveller_should_have_a_status_of(Platinum);
    }
}

As can be seen here, Serenity also honors the JUnit @Ignore annotation. Tests marked with @Ignore will appear as 'Ignored' (shown in grey) in the test reports.

junit ignored and pending tests new
Figure 1. A test report showing ignored (grey) and pending (blue) tests

Handling failing assumptions

Sometimes it can be useful to define a pre-condition for a test. For example, suppose a series of integration tests depend on a mainframe server being running. If the mainframe is unavailable (for example, if it only runs during office hours), you may want to ignore these tests entirely. The test might look like this:

@RunWith(SerenityRunner.class)
public class WhenUpdatingMemberAccounts {

    @Steps
    TravellerHistorySteps travellerSteps;

    @Test
    public void shouldFetchFlightHistoryFromMainframe() {
        // ASSUMPTION
        travellerSteps.assuming_the_mainframe_is_available();

        // WHEN
        travellerSteps.we_fetch_the_latest_flight_history_for_a_traveller();

        // THEN
        travellerSteps.traveller_should_see_the_latest_flights();
    }
}

The assumption is encapsulated in the assuming_the_mainframe_is_available() method:

import static org.hamcrest.Matchers.is;
import static org.junit.Assume.assumeThat;

public class TravellerHistorySteps extends ScenarioSteps {
    @Step
    public void assuming_the_mainframe_is_available() {
        assumeThat(mainframe(), is(ONLINE));                        (1)
    }

    private MainframeStatus mainframe() {
        return OFFLINE;                                             (2)
    }
1 Ensure that the mainframe is available
2 Do whatever needs to be done to check the availability of the mainframe

The assuming_the_mainframe_is_available() method uses the JUnit Assume class, which behaves in a very similar way to Hamcrest matchers. If this check fails, the test will not be executed, and the test result will be reported as 'Ignored'.