Skip to main content

Command Palette

Search for a command to run...

Setup VS Code to run rails test

Updated
2 min read
P

I am a full-stack Ruby on Rails developer with experience in PostgreSQL, Docker, Heroku, and performance optimization. I love exploring new technology stacks and have recently developed an interest in mobile app development using Flutter. My skills in test-driven development and fixing security vulnerabilities have helped me deliver high-quality, reliable code. I am also currently learning about machine learning and its applications in real-world problem-solving. I bring a unique combination of technical expertise, creative problem-solving, and a passion for learning to any team. I am committed to delivering elegant and efficient software solutions and excited to contribute to an innovative and dynamic team that shares this vision.

Either you use RSpec or Mini test you want an easy way to run the test file you are working on, or you might like to run the test under cursor to quickly validate if the test are passing. Most of the time we run rails test command manually in the terminal and its time consuming. For vim users there are so many great vim plugins like vim-test.

In this article I am going to show you how you can easily setup a keybinding without using any additional VS code plugins to run tests directly from your editor.

  • Open a visual studio code.

  • Click on setting -> Keyboard shortcuts (you can also use ctrl/cmd K, ctrl/cmd S

    • Open the setting file by clicking on the file icon at top right corner

    • And add following 2 shortcuts, you can change the shortcut keys as you like, or add more if you want to run all the test cases

    •   {
            // run all tests in current test file
            "key": "shift+cmd+r",
            "command": "workbench.action.terminal.sendSequence",
            "args": { "text": "bin/rails test ${relativeFile}\u000D" }
          },
          {
            // Run test at current line
            "key": "shift+cmd+i",
            "command": "workbench.action.terminal.sendSequence",
            "args": { "text": "bin/rails test ${relativeFile}:${lineNumber}\u000D" }
          }
      
  • Open your terminal ctrl/cmd ~ and also open your test file

  • You can use these shortcuts shift + cmd/ctrl + r to run all tests under current file

  • Or use shift + cmd/ctrl + i to run specific test under cursor.

Enjoy!!

More from this blog

T

The Developers Journey

41 posts

I am a full-stack Ruby on Rails developer with experience in TDD, PostgreSQL, Docker, Heroku, performance optimization and Flutter mobile app development.