Class TimedTest
- java.lang.Object
-
- junit.framework.Assert
-
- junit.extensions.TestDecorator
-
- com.clarkware.junitperf.TimedTest
-
- All Implemented Interfaces:
junit.framework.Test
public class TimedTest extends junit.extensions.TestDecoratorTheTimedTestis a test decorator that runs a test and measures the elapsed time of the test.A
TimedTestis constructed with a specified maximum elapsed time. By default, aTimedTestwill wait for the completion of its decorated test and then fail if the maximum elapsed time was exceeded. Alternatively, aTimedTestcan be constructed to immediately signal a failure when the maximum elapsed time of its decorated test is exceeded. In other words, theTimedTestwill not wait for its decorated test to run to completion if the maximum elapsed time is exceeded.For example, to decorate the
ExampleTestas aTimedTestthat waits for theExampleTesttest case to run to completion and then fails if the maximum elapsed time of 2 seconds is exceeded, use:
or, to time a single test method, use:Test timedTest = new TimedTest(new TestSuite(ExampleTest.class), 2000);
Test timedTest = new TimedTest(new ExampleTest("testSomething"), 2000);Alternatively, to decorate the
ExampleTest.testSomething()test as aTimedTestthat fails immediately when the maximum elapsed time of 2 seconds is exceeded, use:Test timedTest = new TimedTest(new ExampleTest("testSomething"), 2000, false);- Author:
- Mike Clark, Clarkware Consulting, Inc., Ervin Varga
-
-
Constructor Summary
Constructors Constructor Description TimedTest(junit.framework.Test test, long maxElapsedTime)Constructs aTimedTestto decorate the specified test with the specified maximum elapsed time.TimedTest(junit.framework.Test test, long maxElapsedTime, boolean waitForCompletion)Constructs aTimedTestto decorate the specified test with the specified maximum elapsed time.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcountTestCases()Returns the number of tests in this timed test.protected longgetElapsedTime(long beginTime)booleanoutOfTime()Determines whether the maximum elapsed time of the test was exceeded.protected voidprintElapsedTime(long elapsedTime)voidrun(junit.framework.TestResult result)Runs the test.protected voidrunUntilTestCompletion(junit.framework.TestResult result)Runs the test until test completion and then signals a failure if the maximum elapsed time was exceeded.protected voidrunUntilTimeExpires(junit.framework.TestResult result)Runs the test and immediately signals a failure when the maximum elapsed time is exceeded.voidsetQuiet()Disables the output of the test's elapsed time.java.lang.StringtoString()Returns the test description.-
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame, failSame, format
-
-
-
-
Constructor Detail
-
TimedTest
public TimedTest(junit.framework.Test test, long maxElapsedTime)Constructs aTimedTestto decorate the specified test with the specified maximum elapsed time.The
TimedTestwill wait for the completion of its decorated test and then fail if the maximum elapsed time was exceeded.- Parameters:
test- Test to decorate.maxElapsedTime- Maximum elapsed time (ms).
-
TimedTest
public TimedTest(junit.framework.Test test, long maxElapsedTime, boolean waitForCompletion)Constructs aTimedTestto decorate the specified test with the specified maximum elapsed time.- Parameters:
test- Test to decorate.maxElapsedTime- Maximum elapsed time (ms).waitForCompletion-true(default) to indicate that theTimedTestshould wait for its decorated test to run to completion and then fail if the maximum elapsed time was exceeded;falseto indicate that theTimedTestshould immediately signal a failure when the maximum elapsed time is exceeded.
-
-
Method Detail
-
setQuiet
public void setQuiet()
Disables the output of the test's elapsed time.
-
countTestCases
public int countTestCases()
Returns the number of tests in this timed test.- Specified by:
countTestCasesin interfacejunit.framework.Test- Overrides:
countTestCasesin classjunit.extensions.TestDecorator- Returns:
- Number of tests.
-
outOfTime
public boolean outOfTime()
Determines whether the maximum elapsed time of the test was exceeded.- Returns:
trueif the max elapsed time was exceeded;falseotherwise.
-
run
public void run(junit.framework.TestResult result)
Runs the test.- Specified by:
runin interfacejunit.framework.Test- Overrides:
runin classjunit.extensions.TestDecorator- Parameters:
result- Test result.
-
runUntilTestCompletion
protected void runUntilTestCompletion(junit.framework.TestResult result)
Runs the test until test completion and then signals a failure if the maximum elapsed time was exceeded.- Parameters:
result- Test result.
-
runUntilTimeExpires
protected void runUntilTimeExpires(junit.framework.TestResult result)
Runs the test and immediately signals a failure when the maximum elapsed time is exceeded.- Parameters:
result- Test result.
-
getElapsedTime
protected long getElapsedTime(long beginTime)
-
printElapsedTime
protected void printElapsedTime(long elapsedTime)
-
toString
public java.lang.String toString()
Returns the test description.- Overrides:
toStringin classjunit.extensions.TestDecorator- Returns:
- Description.
-
-