JTREG Tool: Java Testing Guide
JTREG Tool: Java Testing Guide
§ Balchandra Vaidya
1Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13
The following is intended to outline our general
product direction. It is intended for information
purposes only, and may not be incorporated into
any contract. It is not a commitment to deliver
any material, code, or functionality, and should
not be relied upon in making purchasing
decisions. The development, release, and timing
of any features or functionality described for
Oracle’s products remains at the sole discretion
of Oracle.
[Link] file
[Link] file
●
It can also be used for writing functional tests.
§ The jtreg tool can be used for developing and running tests for java APIs,
compilers, VM and GUI components.
§ The jtreg tool use JavaTest harness which is a set of tools designed to
execute test programs. [Link]
●
[Link]
●
[Link]
[Link] file
●
an applet (applet test)
●
a shell script (shell test)
●
if the test fails, it should throw an exception.
●
if it succeeds, it should return normally.
●
Not recommended to catch general exceptions such as Throwable,
Exception, or Error.
●
This tag can be used to run tests with non-default options.
§ Manual tests use /manual option with @run tag
●
@run applet/manual
●
@run main/manual
●
@run shell/manual
§ Negative test is marked with /fail option
●
@run main/fail
●
@compile/fail
§ Applet test requires an HTML file which should contain an applet tag with
any necessary parameters.
●
Extension should be .html
●
use Bourne shell syntax
●
It can be controlled by /timeout option e.g. @run main/timeout=100
[Link] file
§ Verbose options
●
amount of output written to the console while running tests.
●
-verbose:summary, -verbose:fail,error, ..
§ Test selection options
●
-a | -automatic can be used to run automatic tests
●
-m | -manual can be used to run manual tests
●
-k:<keywordExpr> can be used to run only tests with <keywordExpr>
which is a simple boolean expression containing keywords
●
-bug:<bugid> can be used to run only tests with <bugid>
●
All tests run by default
§ Test Mode
●
-othervm : Run every test in its own JVM; maximum isolation between
tests; maximum overhead
●
-samevm : Run each test in the same JVM as the JavaTest harness;
minimum overhead; minimum isolation between tests; one bad test can
cause problems for all subsequent tests
●
-agentvm : Run tests using a pool of reusable JVMs; reuse vm if it can
be reset to a clean state after a test; cannot be enforced in a test
●
Test mode can also be specified in a test source file e.g. @run
main/agentvm, @run main/othervm/timeout=300
§ Tests can be run concurrently using -conc:N | -concurrency:N option
●
This option can not be used with -samevm option
●
Not suitable for GUI tests e.g. a test require mouse interaction
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
14
[Link] Execution (3/4)
§ JDK options
●
-vmoption can be used to pass all applicable JVM options
●
Most common JVM options, e.g. -server, -d64, -client etc. are
supported directly
§ Test JDK
●
-testjdk can be used to pass JDK version for testing
●
JDK version used for running 'jtreg' harness is tested by default
§ Excluding testcases
●
[Link] can contain known failing tests.
●
-exclude:[Link] excludes tests present in the files from
executing.
[Link] file
/* @test
/* @test
@summary Example1 - a simple testcase
@run main Example1 § It means invoke main
*/ method of Example1 class
import [Link].*; § The line is optional for
import [Link].*; default execution.
/* @test
@summary Example1 - a simple testcase
§ Throw an exception to
@run main Example1
indicate test failure.
*/
§ Test should exit normally to
import [Link].*;
indicate PASS.
import [Link].*;
●
An empty file [Link] should exist at the root directory of the test.
●
Creates JTwork and JTreport directory, if the directory is not present.
[Link] file
#-----testresult-----
description=file\:/home/jtest/jtregtests/examples/[Link]
elapsed=928 0\:00\:00.928
end=Tue Nov 20 16\:53\:27 GMT 2012
environment=regtest
execStatus=Passed. Execution successful
hostname=testmachine Log file has an extension .jtr
javatestOS=Linux 2.6.32-100.28.5.el6.x86_64 (amd64)
javatestVersion=4.4 and can be found in sub-directories
jtregVersion=jtreg 4.1 fcs b04
script=[Link] of JTwork directory
sections=script_messages build compile main
start=Tue Nov 20 16\:53\:26 GMT 2012
test=examples/[Link]
[Link]=jtest
work=/home/jtest/jtregtests/JTwork/examples
#-----environment-----
#-----testresult-----
Test result summary
description=file\:/home/jtest/jtregtests/examples/[Link]
elapsed=928 0\:00\:00.928
end=Tue Nov 20 16\:53\:27 GMT 2012
environment=regtest
execStatus=Passed. Execution successful
hostname=testmachine
javatestOS=Linux 2.6.32-100.28.5.el6.x86_64 (amd64)
javatestVersion=4.4
jtregVersion=jtreg 4.1 fcs b04
script=[Link]
sections=script_messages build compile main
start=Tue Nov 20 16\:53\:26 GMT 2012
test=examples/[Link]
[Link]=jtest
work=/home/jtest/jtregtests/JTwork/examples
#section:build
----------messages:(3/93)----------
command: build Example1
reason: Named class compiled on demand
elapsed time (seconds): 0.635
result: Passed. Build successful Detail test results broken
#section:compile
down into different phases
----------messages:(3/143)----------
command: compile /home/jtest/jtregtests/examples/[Link]
of test execution
reason: .class file out of date or does not exist
elapsed time (seconds): 0.631
----------[Link]:(0/0)----------
----------[Link]:(0/0)----------
result: Passed. Compilation successful
#section:main
----------messages:(3/102)----------
command: main Example1
reason: User specified action: run main Example1
elapsed time (seconds): 0.22
----------[Link]:(0/0)----------
----------[Link]:(1/15)----------
STATUS:Passed.
result: Passed. Execution successful
[Link] file
§ Specifying VM Options
§ Applet testcase
§ Manual testcase
§ Shell test
§ Defining timeout
§ Compiler test
§ Negative test
§ OS/Platform checks
/* @test
* @summary Example 2 - passing VM parameters
* @run main/othervm -Xms100m -Xmx100m -XX:+PrintGC -XX:
+UseParallelGC Example2
*/
public class Example2 {
§ VM options can be
public static void main(String[] args) throws InterruptedException {
….... specified with
for (GarbageCollectorMXBean collector : collectors) { main/othervm
String x = [Link](); § Equivalent to java <vm
if ([Link]("MarkSweep")) { options> Example2
long count = [Link]();
if (count > 0) {
throw new RuntimeException("Failed.”);
}
}
}
}
}
<h1>Example3</h1>
<html>
<!-- § Run the applet specified at
@test <APPLET> tag in
@summary - Example4 - a manual test
[Link], but tester
@run applet/manual=yesno [Link]
need to interact and choose
whether test is pass or fail.
-->
<head> § NOTE: OpenJDK test
<title> Example4 - a manual test</title> workspace provides
</head> convenient helper classes to
<body>
display test instruction.
# @test
# @summary Example5 - shell tests
# @run shell [Link]
exit 0
/*
* @test
* @summary Example6 - interrupting loop/hang
* @run main/timeout=1 Example6
*/
if ( ! [Link](y) ) {
throw new RuntimeException("Failed. x is not equal y");
if ([Link]() != [Link]()) {
throw new RuntimeException("Failed. Hashcode comparison failed.");
}
}
}
Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
34
1. Examples (7/9) : Compiler test
/*
§ Invoke compiler and compiles
* @test
[Link]
* @summary Example8 - compile test
§ Test pass if compilation
* @compile [Link] succeeds.
*/ § Equivalent to “javac
[Link]”
public class Example8 {
/*
* @test
* @summary Example9 - test pass if compilation fails
* @compile/fail [Link]
*/
§ Test pass if compilation fails
§ The option /fail can be used
public class Example9 { with main, applet and shell
void test(Float f, Integer i) { tests as well, but such use is
boolean b = f == i; discouraged
} ●
@run main/fail <test>
} ●
@run applet/fail <test>
●
@run shell/fail <test>
[Link] file
[Link] file
●
Write a testcase using TestNG
●
Add “[Link] = dir1 dir2” in [Link] or [Link] file, where, dir1
and dir2 represents a package root directory relative test root ([Link])
directory
●
[Link] file, if present, should be present at package root directory
●
While an individual testcase may include jtreg information tags such as @test or
@summary, it must not include jtreg action tags such as @run or @compile
§ Type B:
●
Write a testcase using TestNG
●
Add jtreg tags (e.g. @test) to the test source
●
Use “@run testng ..” tag instead of “@run main ..” tag
OR
[Link]
[Link] = accessible
package [Link]
/* @test
@summary Example61 - a simple TestNG testcase that can be run under jtreg
import [Link];
●
Add “[Link] = dir1 dir2” to the [Link] file
●
If path starts with '/', the path is evaluated from the directory
containing [Link] file, else the path is evaluated from the
directory containing [Link] file
●
Use @library tag <path>, it is same as in other jtreg tests
●
If path starts with '/', the path is evaluated from the directory
containing [Link] file, else the path is evaluated from the
directory relative to the testcase
[Link] file
§ Guidelines
●
[Link]
§ Subscribe to jtreg-use@[Link]
●
Ask general questions about jtreg use
●
Send enhancement proposals
§ Subscribe to quality-discuss@[Link]
●
Discuss quality issues
●
Propose test cases, send change sets, send feedback etc.
[Link] file
●
If testcase is not applicable to a OS/platform, make the testcase PASS.
§ Code review
●
Recommend two peer reviews
●
Send email to OpenJDK component mailing list for feedback
[Link] file
●
$ hg clone [Link] localbuild
●
$ cd localbuild
●
$ sh get_source.sh
§ Test Root
●
${localbuild}/jdk/test
●
${localbuild}/langtools/test
●
${localbuild}/hotspot/test
[Link] file
§ [Link]
§ [Link]
§ [Link]
§ jtreg-dev@[Link]