Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute JUnit TestSuite with some delay between each TestCase?

I have two JUnit4 Test classes say MyTest1, MyTest2 each having a couple of test methods. Actually these are Selenium JUnit TestCases.

In MyTest1.someMethodInsertingDate() i will insert some data into DB and it will take some time to process.

In MyTest2.validateProcessedData() I need to verify that the processed data inserted in the first test method is valid or not.

I know coupling between test methods/cases is not a good practice. But I am writing SeleniumTests to automate User Actions on UI, so I have to do this.

I am executing these two TestCases using MyTestSuite with @RunWith & @SuiteClasses.

Now How can I tell JUnit to execute MyTest2 TestCase with some delay,say 1 minute.

like image 956
K. Siva Prasad Reddy Avatar asked Feb 01 '12 05:02

K. Siva Prasad Reddy


People also ask

How do I use timeout in JUnit?

JUnit provides a handy option of Timeout. If a test case takes more time than the specified number of milliseconds, then JUnit will automatically mark it as failed. The timeout parameter is used along with @Test annotation. Let us see the @Test(timeout) in action.

When executing your tests how can you ignore some test cases in a class while executing others?

@Ignore annotation can be used in two scenarios as given below: If you want to ignore a test method, use @Ignore along with @Test annotation. If you want to ignore all the tests of class, use @Ignore annotation at the class level.

How will you set the timeout in test cases in JUnit?

To specify the timeout period of a certain test case, “timeout” attribute is mentioned on the annotation @Test . Note that the timeout time is specified in milliseconds. In the above test, execution will be timed out after 500ms with the below message.


1 Answers

You can use the simple "@Before" annotation. It should delay the execution thread each time it starts the test.

Example:

@Before
public void initialise() throws InterruptedException {
    Thread.sleep(2000);
}
like image 71
Cosmos Avatar answered Jan 01 '23 13:01

Cosmos