Automate new test file generation and test for existence of tests

Hi all,

After 10 years not coding anything I recently started learning Elixir and it’s super cool. During the lockdown I decided to learn Python and Elixir. And let’s put it this way. I’m not going to continue with Python ;-). Thank you for the fantastic language, documentation and amazing community!

Last week I started building my first app and every time I write a function I write a test or doctest (as I should ;-)). The only problem I have is that I’m constantly making mistakes in new <module name>_test.exs files.

Mistakes I make:

  • I forget to include doctest CoolApp.Abc so even I write doctest it doesn’t get executed…
  • I copy/paste existing <module name>_test.exs and forget to replace CoolApp.AbcTest by CoolApp.XyzTest
  • I place test files in a wrong directory (it still works but it’s a mess since it does not mirror files in lib directory)

I’m thinking that because I’m just a hobbyist and I have this problem, you pros have to have the same problem ;-).

Could you please tell me is there something that can:

  1. generate empty test files as a mirror of lib directory
  2. test existence of test files -> test that there is a single test file for each file in lib directory
  3. test that each test file has correct defmodule CoolApp.AbcTest and doctest CoolApp.Abc inside
  4. optional: test that every function has at least one assert

Thank you.

Kind regards,

Mat

1 Like

A lot of this comes down to testing practices:

  • putting the files in the right place is easier to get right if you always start out running the file you’re working on with mix test test/some_file_test.exs. This doesn’t actually change anything, but typing the name more than once will give your brain a chance to spot the error.

  • there’s no requirement that one file in lib -> one file in test. Sometimes you have lots of tests, and need to split things up more.

  • the compiler should complain loudly if you duplicate a test module and run mix test

  • if you make the first doctest you write in a file be one that fails, that will tell you you’re editing the right file :stuck_out_tongue:

  • same thing for asserts - if you don’t have an assert in a particular test, you will definitely not be able to make it fail

2 Likes

Editors sometimes have language specific tools for some of this. In Atom I use a plugin that creates a test file for the current module file. Linters can check whether there are empty tests. I know there is also an Elixir linter plugin for Atom but I haven’t tried it because mix format pretty much solves that problem for me.

For stuff like that I use Projectionist with my own projection. This allows me to create new test file for module by simply using :A command (from Alternate) and it will automatically generate ExUnit test for me with all needed calls.

4 Likes

Looks like that someone (Samuel Pordeus) read my mind and created an extension for VSCode / Codium:

https://marketplace.visualstudio.com/items?itemName=samuel-pordeus.elixir-test

It’s simple and super useful. You just fire a Code command Elixir Test: Jump and Code jumps to the test file or vice versa. If the test doesn’t exist then Code will ask you “do you want me to create it?” and creates it in the right spot in test/. Simple and fantastic.

(and if you know Code, you know that you can bind any command to a keyboard shortcut ;-))

3 Likes