My doctest does not return correctly. What am I doing wrong

475 views Asked by At

I have the following files. If you look in app.ex the doctest says 2 + 2 is 3. I am intentionally trying to make it fail.

app.ex

defmodule App do
     @doc """
      Adds two numbers

      ## Examples

        iex> App.add(2, 2)
        3

      """
    def add(a,b) do
      a + b
    end


end

app_test.exs

defmodule AppTest do
  use ExUnit.Case

  doctest App
end

In the console I type: mix test and the result is:

Finished in 0.01 seconds
0 failures

Randomized with seed 547000
2

There are 2 answers

0
Paweł Dawczak On

The problem seems to be in formatting of your @doc. I think, the examples to be properly parsed and executed by Doctest, have to be indented with 4 spaces.

I'm pasting your code here with updated formatting for reference:

defmodule App do
  @doc """
  Adds two numbers

  ## Examples

      iex> App.add(2, 2)
      3

  """
  def add(a,b) do
    a + b
  end
end
3
Patrick Oscity On

It seems that mix is not finding any test cases at all, otherwise you would get the message 12 tests, 0 failures instead of just 0 failures

This probably happens because of the non-standard naming of your test file. In Elixir, test files must end with *_test.exs, you used *.text.exs (maybe a typo).

If you rename your test to test/app_test.exs it should work just fine.