I taught my friend to use the testing as the tool for understanding and knowing how to use the packages that I built.
Instead of asking me how it works I just send him to the Unit Tests so he can see how I tested it and by so he could understand how to use it.
I like the idea of treat tests as documentation directly into ExDoc generated documentation.
Some of these packages you will find things like https://github.com/straw-hat-team/straw_hat_review/blob/master/test/straw_hat_review/aspects_test.exs
defmodule StrawHat.Review.AspectsTest do
describe "find_aspect/1" do
test "with valid id should find the aspect" do
aspect = insert(:aspect)
assert {:ok, _aspect} = Aspects.find_aspect(aspect.id)
end
test "with invalid id shouldn't find the aspect" do
assert {:error, _reason} = Ecto.UUID.generate() |> Aspects.find_aspect()
end
end
end
Notice that I am using the function name with the arity as some sort of pointer to the function so the users can know the scope of my test is around that function.
I could remove however that from the message when the test is not related to a specific function and at that moment probably becomes part of the module scope from the documentation perspective.
If add some metadata to the testing and combine it with a description that represents some kind of use case I would like to see it on my ExDoc as well.
For example,
defmodule StrawHat.Review.AspectsTest do
# whatever other way, just for showing the idea
@doc function: StrawHat.Review.find_aspect/1
describe "find_aspect/1" do
test "with valid id should find the aspect" do
aspect = insert(:aspect)
assert {:ok, _aspect} = Aspects.find_aspect(aspect.id)
end
test "with invalid id shouldn't find the aspect" do
assert {:error, _reason} = Ecto.UUID.generate() |> Aspects.find_aspect()
end
end
end
# whatever other way, just for showing the idea
@doc module: StrawHat.Review
test "..." do end
This way the documentation could pull out the test with the description and use that for generate a new section on the documentation.
Maybe some collapsed blocks where you can see the description and expand the block so see the whole unit test.
Anyway, I hope you get the idea of what I am suggesting, I would love to take advantage of the testing for generating documentation.
cc: @josevalim