TestNG TimeOut Example (Java Unit Test @test TimeOut)

Source: Internet
Author: User
Tags assert testng

"The other tutorials in this series are being translated and clicked on Category: TestNG for viewing." 】

"Translation by mingming like the month QQ 605283073"

Original: http://websystique.com/java/testing/testng-timeout-example/


This article describes the timeout for testng tests.

The TestNG timeout attribute can be implemented through @test (timeOut = 1000) annotations.

If a test method with @test annotations should be completed in a very short time but executed for a long time, we should study it.

@Test (timeOut = 1000) indicates that this test method should not be completed in 1000 milliseconds (one second). If not done within this time,

This test method also counts as a failure.


Tested class:

Package com.websystique.testng;
 
public class Calculator {public
 
    double add (double A, double b) {return
        a+b;
    }
     
    Public double subtract (double A, double b) throws interruptedexception{
        Thread.Sleep (5000);
        Return A-b
    }
     
}


In the subtract (subtraction) method there is thread.sleep (5000), which lets the thread message 5000 milliseconds.

To write a test class:

Package com.websystique.testng;
Import Org.testng.Assert;
Import Org.testng.annotations.AfterClass;
Import Org.testng.annotations.AfterMethod;
Import Org.testng.annotations.BeforeClass;
Import Org.testng.annotations.BeforeMethod;
 
Import Org.testng.annotations.Test;
 
    public class Testngtimeoutexample {Calculator Calculator;
        @BeforeClass public void Setup () {System.out.println ("setup ()");
    Calculator = new Calculator ();
        @AfterClass public void teardown () {System.out.println ("teardown ()");
    Calculator = null;
    @BeforeMethod public void Beforemethod () {System.out.println ("Beforemethod ()");
    @AfterMethod public void Aftermethod () {System.out.println ("Aftermethod ()");
        @Test public void Testadd () {System.out.println ("Testadd ()");
    Assert.assertequals (Calculator.add (3, 4), 7.0); } @Test (TimeOut = 3000)//timeout in milliseconds publicvoid Testsubtract () throws Interruptedexception {System.out.println ("testsubtract ()");
    Assert.assertequals (Calculator.subtract (5, 2), 3.0); }
 
}


Run test results by testng Eclipse plug-in or MVN clean test

Setup () Beforemethod () Testadd () Aftermethod () Beforemethod () testsubtract () Aftermethod () teardown () Failed:testsubtract Org.testng.internal.thread.ThreadTimeoutException:Method Org.testng.internal.TestNGMethod.testSubtract () didn ' t finish within the time-out 3000 at JAVA.UTIL.CONCURRENT.LOCKS.A Bstractqueuedsynchronizer$conditionobject.dosignalall (abstractqueuedsynchronizer.java:1890) at Java.util.concurrent.locks.abstractqueuedsynchronizer$conditionobject.signalall ( abstractqueuedsynchronizer.java:1959) at Java.util.concurrent.ThreadPoolExecutor.tryTerminate ( threadpoolexecutor.java:707) at Java.util.concurrent.ThreadPoolExecutor.processWorkerExit (
    threadpoolexecutor.java:1006) at Java.util.concurrent.ThreadPoolExecutor.runWorker (threadpoolexecutor.java:1163) At Java.util.concurrent.threadpoolexecutor$worker.run (threadpoolexecutor.java:615) at Java.lang.Thread.run ( thread.java:745) =============================================== DefauLt Test Tests Run:2, failures:1, skips:0 =============================================== 


You can see that this test method ran for more than 3 seconds, so the test failed.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.