Search code examples
junitphpunitjavadocnosephpdoc

How to place junit/phpunit tests in a methods javadoc/phpdoc docblock?


I remember having seen something like:

/**
 * ("world") -> "hello world"
 * ("") -> "hello "
 */
public static myFunc(in) {
  return "hello " + in;
}

So you can place trivial tests directly in the documentation block of a function. Your test framework finds these tests and executes them.

But I can't remember whether I saw this in Java, Python, PHP or JavaScript. Do you know any tools in any language who do this? Is there a special name for this thing?

I already found it in groovy.


Solution

  • In python it is called doctest:

    The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they work exactly as shown.