Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to skip @Test if @Before fails #2511

Closed
skhandekar1 opened this issue Jul 15, 2021 · 2 comments
Closed

How to skip @Test if @Before fails #2511

skhandekar1 opened this issue Jul 15, 2021 · 2 comments
Labels

Comments

@skhandekar1
Copy link

Currently though a step in @before fails my @test still runs. Is there a way to skip @test if @before fails? Also I don't see anything in the serenity listeners that will handle @before failures. What do I do in such case?

`@Override
public void beforeEachTest()
{
/*
* This section assumes Patient James exists in CP.
*/
log.info("beforeEachTest called!");
resultWriter.setTestProtocolID(TP_ID);

	//Login to CP, search and select patient James.
	cpApp.Login.Login("147258");
	cpApp.MyPatients.Search("James");
	cpApp.MyPatients.EnterFirstPatient();
	
	//Navigate to Devices tab and associate ETM device. Register IPG as Patient's device.
	cpApp.Patient.TapDevices();
	cpApp.Patient.Devices.SelectPermanent();
	cpApp.Patient.Devices.AssociateBlueETX(Equipment.ETM1);
	cpApp.LoaderScreen.waitUntilLoaderDisappears(20);
	cpApp.Patient.Devices.RegisterIPGasPatientDevice();
	
	//Navigate to Sessions tab and attempt to add a session.
	cpApp.Patient.TapSessions();
	cpApp.Patient.Sessions.AddSession();
}

@Title("TP397-002")
@Test
public void Test001()
{
    TP397_002.has(
        new AcceptanceCriteria("Toast message appeared.")
        .with(new ExpectedResult<Boolean>(true)),
    		
        new AcceptanceCriteria("Toast message  Session cannot be created.")
        .with(new ExpectedResult<String>("Error: Patient doesn't have.")),
        
        new AcceptanceCriteria("OR Session is not created (screen remains on Sessions screen).")
        .with(new ExpectedResult<Boolean>(true))
    );
    
    //Select OR Session option.
    cpApp.Patient.Sessions.ChooseORSession();
    cpApp.ToastMessage.waitFor();

}`

Listener class
`
public class ExecutionListener implements StepListener
{

ResultWriterCSV resultWriter = ResultWriterCSV.getInstance();
String stepTitle="";
Boolean reportStep = false;

@Override
public void testSuiteStarted(Class<?> storyClass) {
	// TODO Auto-generated method stub
}

@Override
public void testSuiteStarted(Story story) {
	// TODO Auto-generated method stub
}

@Override
public void testSuiteFinished() {
	System.out.println("Test Suite finished*****************");
}

@Override
public void testStarted(String description) {
	// TODO Auto-generated method stub
}

@Override
public void testStarted(String description, String id) {
	// TODO Auto-generated method stub
}

@Override
public void testFinished(TestOutcome result) {

	//AK: Can we know from TestOutcome whether the @Before method succeeded?
	
	String failureObservation = result.getTestFailureMessage();
	
		if(result.isFailure()){
			resultWriter.appendTestCaseResultToCSV(TP397_Constants.testCaseResult_FAIL);
		}else if(result.isError()){
			resultWriter.appendTestCaseResultToCSV(failureObservation, TP397_Constants.acceptanceCriteriaResult_NOT_TESTED);
		}else if(result.isSkipped()) {
			//Do nothing if test is skipped
		}else{
			resultWriter.appendTestCaseResultToCSV(TP397_Constants.testCaseResult_PASS);
		}
}

@Override
public void testRetried() {
	// TODO Auto-generated method stub
	
}

@Override
public void stepStarted(ExecutedStepDescription description) {
	// TODO Auto-generated method stub
}

@Override
public void skippedStepStarted(ExecutedStepDescription description) {
	// TODO Auto-generated method stub
	
}

@Override
public void stepFailed(StepFailure failure) {
	// TODO Auto-generated method stub
}

@Override
public void lastStepFailed(StepFailure failure) {
	// TODO Auto-generated method stub
}

@Override
public void stepIgnored() {
	// TODO Auto-generated method stub
	
}

@Override
public void stepPending() {
	// TODO Auto-generated method stub
}

@Override
public void stepPending(String message) {
	// TODO Auto-generated method stub
	
}

@Override
public void stepFinished() {
	// TODO Auto-generated method stub
}

@Override
public void testFailed(TestOutcome testOutcome, Throwable cause) {
	// TODO Auto-generated method stub
}

@Override
public void testIgnored() {
	// TODO Auto-generated method stub
	
}

@Override
public void testSkipped() {
	// TODO Auto-generated method stub
	
}

@Override
public void testPending() {
	// TODO Auto-generated method stub
}

@Override
public void testIsManual() {
	// TODO Auto-generated method stub
	
}

@Override
public void notifyScreenChange() {
	// TODO Auto-generated method stub
	
}

@Override
public void useExamplesFrom(DataTable table) {
	// TODO Auto-generated method stub
	
}

@Override
public void addNewExamplesFrom(DataTable table) {
	// TODO Auto-generated method stub
	
}

@Override
public void exampleStarted(Map<String, String> data) {
	// TODO Auto-generated method stub
	
}

@Override
public void exampleFinished() {
	// TODO Auto-generated method stub
	
}

@Override
public void assumptionViolated(String message) {
	// TODO Auto-generated method stub
	
}

@Override
public void testRunFinished() {
	System.out.println("Test run finished*****");
	resultWriter.close();
}

}
`

@wakaleo
Copy link
Member

wakaleo commented Jul 20, 2021

The listeners do not affect test outcomes, they are intended for read-only operations. If it is an excepted failure, you could catch the exception in the @before clause and throw a Compromised exception, to distinguish from failing tests and tests that are skipped for some other reason.

@wakaleo
Copy link
Member

wakaleo commented Feb 12, 2022

Closing presumed answered

@wakaleo wakaleo closed this as completed Feb 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants