2

I have a project where before every @Test method I do a check to see if the Method's annotation data is valid. If the data isn't valid I want to skip the test method & continue the rest of my test suite.

All the data parsing and logic works fine, but from what I can tell I am using the wrong tool for the job.

My code has...

 private SoftAssert s_assert = new SoftAssert(); @BeforeMethod public void beforeMethod(Method m){ //reads code if (dataNotCorrect) s_assert.fail(); } @Test @MyCustomAnnotation(data = incorrect) public void Test1(){ //Do stuff } @Test @MyCustomAnnotation(data = correct) public void Test2(){ //Do stuff } 

In this scenario I want to start trying to do both, but when the test runs Test1() should be skipped and testng should continue on to run Test2(). Yet as soon as I catch the fail at Test1(), it ends the whole suite at Test1(). Skipping not only Test1() but also Test2().

I've tried both with a Soft assert and normal assert but neither seem to work.

3 Answers 3

2

@Benoit got me most of the way there replacing my asserts with throwing a SkipException. But my issue of wanting the current test to be skipped rather than every remaining test, still remained.

The issue turned out to be with the configfailurepolicy in Testng. It defaults to skip (skipping all remaining tests) when I wanted it to be set to continue (continues the rest of the suite).

Here is an answer I found elsewhere which I managed to apply in two different ways. Link here


1. First, make a testng.xml and run tests from there. Next to the suite name, add the tag configfailurepolicy="continue" Here is my testng.xml below

<?xml version="1.0" encoding="UTF-8"?> <suite name="Suite" configfailurepolicy="continue"> <test name="MyTests" preserve-order="true"> <classes> <class name="testclassLocation..." /> </classes> </test> </suite> 

Make sure that you run your tests from testng.xml if you do it this way.


2. Find where the .jar for testng is located. I'm using maven so it was "${user.dir}.m2\repository\org\testng\testng\6.14.3".

Then open up the .jar archive, view the file 'testng-1.0.dtd', find the line

configfailurepolicy (skip | continue) "skip" 

And change it to

configfailurepolicy (skip | continue) "continue" 

Should work fine after that.

Edit: As mentioned in the comments it is recommended to use the first solution as it allows these changes/fixes to become portable across multiple projects/devices. The second solution will only apply the fixes to your machine.

Sign up to request clarification or add additional context in comments.

2 Comments

In solution 2 you alter the default behavior of TestNG by directly modifying it. It's not very portable (Keep in my mind that maven repo is usually shared among several projects, which could require a different setting). Also you have to re-apply this mod when you upgrade TestNG. Excellent research work though!
@Benoit Yea exactly. I used the first solution for mine. I'll update my answer to say that.
1

SkipException is what you are looking for.

In beforeMethod, check your data and throw SkipException if they are not correct. This will skip the test. In this complete yet simple example, test2 is skipped:

import org.testng.SkipException; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; public class TestA { @Retention(RetentionPolicy.RUNTIME) public @interface MyCustomAnnotation { boolean dataCorrect(); } @BeforeMethod public void beforeMethod(Method m) { if (!m.getAnnotation(MyCustomAnnotation.class).dataCorrect()) { throw new SkipException("Invalid data"); } } @Test @MyCustomAnnotation(dataCorrect = true) public void test1() { System.out.println("test1"); } @Test @MyCustomAnnotation(dataCorrect = false) public void test2() { System.out.println("test2"); } } 

See also: How do I use TestNG SkipException?

You also need to alter the default config failure policy, to let the others tests run even if one is skipped. This is done at the suite level:

<?xml version="1.0" encoding="UTF-8"?> <suite name="Suite" configfailurepolicy="continue"> ... </suite> 

Thanks to @Kyle the OP for pointing this attribute.

Comments

0

Create a static Booleanflag in BeforeMethod, if data not correct then just change the flag to true and in Test of boolean is true turn flag to false first and fail test

Sample code

public static Boolean myflag=false; @BeforeMethod public void beforeMethod(Method m){ //reads code if (dataNotCorrect) myflag=true; } @Test @MyCustomAnnotation(data = incorrect) public void Test1(){ if(myflag){ myflag=false; s_assert.fail(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.