Is there a way to not run the setup callback during doctests?

Is there a way to not run the setup callback during doctests? For example, I’d like to have a test .exs file that both runs doctests using doctest and regular tests using test against the same code module, but I would like the setup callback to only run before each test and not doctest. Is there a way to do this, or is the only way to separate them into separate modules?

Thank you!

I suppose an alternative question is: Is there a way to check inside the setup callback if one is about to run a doctest or not?

Scoping the setup block should do the trick:

describe "tests with setup" do
  setup do
  end
  
  test "test with setup", %{...} do
  end
end
2 Likes

There’s a :test_type available in the test context that will be :test or :doctest (or I think the property testing library sets this as well):

setup %{test_type: test_type} do
  unless test_type == :doctest do
    # perform setup
  end
end

Though I think separating things with describe blocks is a totally valid solution as well

2 Likes

@stefanchrobot @brettbeatty Thank you both so much! This answers both of my questions exactly. Both of these will be useful. Thanks again.

1 Like