Cover image for Launch Rails tests without ever leaving VS Code
Doctolib Engineering

Launch Rails tests without ever leaving VS Code

fwuensche profile image Flavio Wuensche Updated on ・3 min read

If you don't write tests yet, well, it's never too late.

At Doctolib, it is mandatory to write automated tests for every little feature you develop. On the other hand, we have currently 12,000 automated tests (and counting). Developers cannot just run the whole test suite in their local machines — it’s not feasible. It is recommended though that you run at least the tests concerned by your contribution.

Wait, can’t I launch specific tests?

Yep! On Rails, there are several ways you can selectively run tests:

# launch all tests
rails test

# launch all tests inside a folder
rails test test/models

# launch all tests inside a file
rails test test/models/agenda_test.rb

# launch tests from several files
rails test test/models/{agenda,organization}_test.rb

# launch an specific test (based on a line)
rails test test/models/agenda_test.rb:16

# launch a few tests based on a regexp
rails test test/models/agenda_test.rb -n /should_validate/

And you can do all of that straight from your terminal.

But I don't want to leave VS Code

I know, I know. To avoid that back and forth, I use two different VS Code features — keybindings and tasks. Keybindings are just a fancy way to refer to keyboard shortcuts. Tasks, on the other hand, can be pretty much anything you come up with, like a shell command. Putting both together, you can now launch terminal commands using custom shortcuts. Voilà.

Currrently, I use the following shortcuts:

  • CMD+H to headless launch the integration test for current line
  • CMD+SHIFT+H to headless launch ALL integration tests for current file
  • CMD+L to launch the integration test for current line
  • CMD+SHIFT+L to launch ALL integration tests for current file

Configuring your own tasks

To edit your tasks.json file you can CMD+SHIFT+P and look for Tasks: Configure Task. Below you can see the two tasks I've described above.

  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
      "label": "launch tests for current file",
      "type": "shell",
      "command": "rails test ${file}",
      "problemMatcher": [],
      "presentation": { "clear": true },
      "label": "launch test for current line",
      "type": "shell",
      "command": "rails test ${file}:${lineNumber}",
      "problemMatcher": [],
      "presentation": { "clear": true },
      "label": "launch tests for current file (headless)",
      "type": "shell",
      "command": "HEADLESS=1 rails test ${file}",
      "problemMatcher": [],
      "presentation": { "clear": true },
      "label": "launch test for current line (headless)",
      "type": "shell",
      "command": "HEADLESS=1 rails test ${file}:${lineNumber}",
      "problemMatcher": [],
      "presentation": { "clear": true },

Note we've added a couple of custom parameters, such as clear to, well, clear the terminal panel before running the new tests, and problemMatcher which is just a workaround to avoid an annoying VS Code prompt 😉

Setting custom keyboard shortcuts

This is the easy part. All you need to add to your keybindings.json file is:

  // shortcuts for launching rails tests
  { "key": "cmd+shift+l", "command": "workbench.action.tasks.runTask",  "args": "launch tests for current file" },
  { "key": "cmd+l", "command": "workbench.action.tasks.runTask",  "args": "launch test for current line" },
  { "key": "cmd+shift+h", "command": "workbench.action.tasks.runTask",  "args": "launch tests for current file (headless)" },
  { "key": "cmd+h", "command": "workbench.action.tasks.runTask", "args": "launch test for current line (headless)" },

So now you can place the cursor over a test and press CMD+L to launch it, or CMD+SHIFT+L to run the whole file. The same applies to HEADLESS mode, except that you use the letter H.

If you have any questions or suggestions, leave a comment below. And if you want to contribute to building the future of healthcare, Doctolib is hiring Ruby on Rails developers in Paris, Berlin, Nantes, and more yet to come...

Doctolib Engineering

We are hiring in all positions. If you want to build a better healthcare system, visit our career page: https://about.doctolib.fr/jobs


markdown guide

Awesome, this is going to be helpful, thanks!


Thanks Flavio, this is a real time saver! 🙌
Is there a way to relaunch the last launched test even if you are not in the test file ? 🤔