amnu3387

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

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)
)

Where Next?

Popular in Questions Top

Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement