Fork me on GitHub

An implementation of Python doctests for Java/Javadoc

C. Scott Ananian

JDoctest is an implementation of Python's doctest for Java, based on the ideas in doctestj and Rhino's doctests.

Unlike doctestj, JDoctest is a javadoc Taglet, rather than a Doclet. It doesn't replace the standard javadoc, it just adds a new @doc.test tag. We also use a multiline format similar to Python's doctest (based on Rhino's implementation) rather than insist on a single evaluated expression, as doctestj does.

Doctest blocks are written in Javascript. Calling from Javascript into Java is straightforward. An example from the top of the net.cscott.jdoctest.JDoctest class:

 * A "@doc.test" tag specifies an interactive javascript
 * session, in an environment where the class' package has been imported.
 * The output of the javascript session should match the output
 * provided.
 * @author C. Scott Ananian
 * @doc.test
 *   This is an example of a test which passes:
 *   js> "a".equals("a")
 *   true
 * @doc.test
 *   This is an example of a test which fails:
 *   js> 1+2
 *   5
 * @doc.test
 *   We can write tests which expect exceptions to be thrown, although
 *   it's a little clunky (implementing doctest.ELLIPSIS or
 *   doctest.IGNORE_EXCEPTION_DETAIL would make this nicer):
 *   js> try {
 *     >   java.lang.String("hi").charAt(3);
 *     > } catch (e) {
 *     >   print(e.javaException.getMessage())
 *     > }
 *   String index out of range: 3
 * @doc.test
 *   This demonstrates that the current package has been imported:
 *   js> JDoctest
 *   [JavaClass net.cscott.jdoctest.JDoctest]
 *   js> Version.PACKAGE_NAME
 *   jdoctest
 * @doc.test
 *   Note that results referencing object hashes are properly handled in
 *   the output comparison function, even when Rhino gives a different hash
 *   at runtime:
 *   js> o = new java.lang.Object()
 *   java.lang.Object@1d2068d
 *   js> o
 *   java.lang.Object@1d2068d
 *   js> new java.lang.Object()
 *   java.lang.Object@1ac2f9c
public class JDoctest implements Taglet { ... }

View the generated javadoc for this example.


JDoctest is currently at version 1.6. You can download the latest version, see recent changes at ohloh or github, or browse the source code or API documentation on-line. Jdoctest is licensed under the GNU GPL v2 or later.

Source code repository

The git repository for jdoctest can be cloned with the following command:


Building from source; installing

To build:

To install:


To invoke from the command line:

javadoc -taglet net.cscott.jdoctest.JDoctest \
        -tagletpath lib/jdoctest/jdoctest.jar:lib/jdoctest/js.jar:bin \
        -J-ea \
        <your other javadoc options go here>

I'm assuming that your source code has been compiled into bin. Note that your compiled class files must be included on the tagletpath so that we can run the doctests. Use js-14.jar instead of js.jar if your project uses a pre-1.5 JDK. Note that you need to use the -J-ea option if you want assertions to be enabled during the evaluation of the doctests. You can use -J-ea:<your package>... to only enable assertions in your code (not in the entire javadoc tool).

If you are using Apache Ant, the following rule will work:

  <javadoc failonerror="true" ...>
    <taglet name="net.cscott.jdoctest.JDoctest"
            path="lib/jdoctest/jdoctest.jar:lib/jdoctest/js.jar:bin" />
    <arg value="-J-ea" />

As before, replace bin with the appropriate path to your compiled project classes, and use js-14.jar instead of js.jar if your project uses a pre-1.5 JDK. The -J-ea argument ensures assertions are enabled in your code when doctests are evaluated.

You may want to add css rules to make the output prettier. JDoctest has hooks to allow the use of google-code-prettify which will do syntax highlighting on the client side. The build.xml file for JDoctest shows how this is hooked up. The files in src/doc-test should be copied to the top-level source directory of your project. Note that javadoc's directory structure for the generated documentation makes it tedious to link to the top-level doc-files directory from the generated documentation, but we add a <script> tag at the bottom of each javadoc page (using the -bottom option) to make it all work out.

JUnit support was added in JDoctest version 1.5; see the README file included with JDoctest for more details.

Mailing list

Sign up to receive mail when new versions of JDoctest are released.

Google Groups
Subscribe to JDoctest Announce List
Visit this group

You can also follow the project on github, ohloh or LiveJournal.

Version Archive

Jdoctest 1.6, released 31-Mar-2014.

Jdoctest 1.5, released 12-Sep-2009.

Jdoctest 1.4, released 09-Mar-2009.

Jdoctest 1.3, released 10-Feb-2009.

Jdoctest 1.2, released 07-Feb-2009.

Jdoctest 1.1, released 05-Feb-2009.

Jdoctest 1.0, released 04-Feb-2009.

C. Scott Ananian

Valid XHTML 1.0!