Automated rails tests in VS Code

Flavio Wuensche
Sep 3 · 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. We have currently 12,000 automated tests (and counting). That being said, developers cannot just run the whole test suite in their local machines — it’s not feasible. It is recommended though that people run at least the tests concerned by their 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 specific tests (based on their names)
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à.

Currently, I use the following shortcuts:

  • CMD+H to headless launch the integration test for the current line
  • CMD+SHIFT+H to headless launch ALL integration tests for current file
  • CMD+L to launch the integration test for the 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.

Cool, isn’t it? And you, would you like to help us build the future of healthcare?

Pour un système de santé plus humain, efficace et connecté

Flavio Wuensche

Written by

Doctolib

Doctolib

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade