amnu3387
Is it possible to retrieve all test names in a *_test.exs(s) file(s) or directory inside a test folder?
This is more of curiosity than something utterly needed but would be great if there was a not overly hacky way of accessing them?
Basically I have a bunch of entities in json files that are parsed on application startup and represent a bunch of elements in a game.
{
"a_slug_1": {....},
"a_slug_2": {....},
....
}
Each json file represents a given “collection” of elements that share a trait, but some elements appear in more than a single json file.
Now I’ve decided to add specific tests that cover the contents of each of these files, in way of interactions when used in through a game engine, so each test is specifically named with the “slug” that is the same key by which the element is accessed in the json file, and after startup parsing is accessible application wide.
I would like to have a way of with N *_test.exs files, getting the test names included in those files so that I could assert that each element in all of the json files is covered.
Let’s say I have a test file, example_test.exs with two tests, described as:
test("a_slug_1", ....) do .... end
test("a_slug_2", ....) do .... end
I would like to be able to extract from a filepath ...../test/examples_test.exs a list of
["a_slug_1", "a_slug_2"]
Is this possible? Thanks
Marked As Solved
eksperimental
Just place this after the closing “end” of your test module. Replace NameOfYourModule with the name of your module.
IO.inspect(
NameOfYourModule.__info__(:functions)
|> Enum.reduce([], fn {name, _arity}, acc ->
case Atom.to_string(name) do
<<"test " <> rest>> ->
[rest | acc]
_ ->
acc
end
end)
)







