Doctests

A doctest in Elixir is an example in the documentation of a function that the test suite runs. The example is written the way a reader types it in iex, the module marks it with @doc, and one line in a test module turns every such example into a test. The code and its documentation cannot drift apart without a red suite.

mix new doc_demo

* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/doc_demo.ex
* creating test
* creating test/test_helper.exs
* creating test/doc_demo_test.exs

Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:

    cd doc_demo
    mix test

Run "mix help" for more commands.

An example in @doc

The documentation string of DocDemo holds two examples under an iex> prompt. The line after the prompt is the call, and the line after that is the value the call returns:

doc_demo/lib/doc_demo.ex

defmodule DocDemo do @moduledoc false @doc """ Greets the world. iex> DocDemo.hello() :world """ def hello, do: :world @doc """ Greets a name. iex> DocDemo.greeting("Ada") "Hello, Ada!" """ def greeting(name), do: "Hello, #{name}!" end

doctest

The test module calls doctest DocDemo, which reads the documentation of the module and registers one test per example. A module can have both ordinary tests and doctests; the generated test of the project calls DocDemo.hello(), and the two examples add two more claims:

# in test/doc_demo_test.exs defmodule DocDemoTest do use ExUnit.Case doctest DocDemo test "greets the world" do assert DocDemo.hello() == :world end end

The block is the generated test module with the doctest line it already has; the test file is written by mix new and is not edited here.

The test the docs write

With the project created above and the module written, mix test runs the ordinary test and the two examples. The seed and the time change; the counts do not:

cd doc_demo

mix test

Compiling 1 file (.ex)
Generated doc_demo app
Running ExUnit with seed: 449673, max_cases: 8

...
Finished in 0.04 seconds (0.00s async, 0.04s sync)
2 doctests, 1 test, 0 failures

The three dots are the two doctests and the ordinary test. A wrong example fails in the same report as any other test, and the fix is the call, the expected value or the code; the ExUnit lesson shows that report, and the typespecs lesson adds the contract a doctest does not express.

The commands on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is a doctest?

    Answer

    An example in the documentation that ExUnit runs as a test

    doctest reads the iex examples in @doc and registers them as tests.

  2. Which line in a test module registers the documentation checks as tests?

    Answer

    doctest ModuleName

    The doctest macro takes the module and registers one test per documentation check.

  3. How is the expected value written in a doctest?

    Answer

    On the line after the iex> call, as the value the call returns

    The line after the prompt is the call; the next line is the value the shell would print.

  4. What does a doctest need from the module?

    Answer

    A public function and its documentation

    doctest reads the docs of the module the test names.

  5. What does this documentation example claim? @doc """ Greets a name. iex> Greeting.hello("Ada") "Hello, Ada!" """

    Answer

    That Greeting.hello("Ada") returns "Hello, Ada!"

    The line after the call is the expected return value.

  6. A doctest fails. What does the report show?

    Answer

    The file and line of the example, the expected value and the actual one

    A doctest is an ordinary test, so the failure report names the example and both values.

  7. Why does a doctest keep code and documentation together?

    Answer

    Because the example runs, so a stale example fails the suite

    The suite runs the example, so documentation that no longer matches the code turns red.

  8. What does the line doctest DocDemo in a test module add?

    Answer

    One test per doctest example of DocDemo

    Each iex example becomes a test case.

  9. Which command runs the doctests of a Mix project?

    Answer

    mix test

    mix test runs the test modules, and doctest registers the examples with them.

  10. What happens when the expected value in a doctest is wrong?

    Answer

    The suite has a failing test

    The example is a test; a mismatch between code and docs is a failure.

Автор статьи: Arthur Isaev