Speeding up rails tests during development by keeping Rails in memory? - ruby-on-rails

When running rspec tests on my rails app using "rake" or "rake spec", it takes a long time to initialize Rails and start running tests.
Is there a way to keep Rails loaded in memory and run tests against it? I'm hoping there's something similar to using "grails interactive" like this How to speed up grails test execution but for Rails.

It's almost what Spork is all about.

See the most awesome railscast "How I test" http://railscasts.com/episodes/275-how-i-test
This uses guard to run a relevant test every time you save a file so you know almost instantly if the code you have written has caused a pass or a fail. Also if you are running on linux then you can use the libnotify and libnotify-rails gems which pop up a window for you (totally unobtrusively) indicating a pass or failure so you don't have to keep checking the console.

There is a Railscast called Spork that specifically discusses this problem at http://railscasts.com/episodes/285-spork and offers an excellent solution with a detailed walkthrough.

Related

Rspec running very, very slowly on Ruby on Rails

I am running Ruby on Rails 5.1.4 and am trying to upgrade to Ruby 2.5.1 from Ruby 2.4.4. When I run Rspec, it is running incredibly slowly. I have used the method described here by myronmarston allows me to see what is happening.
The Rspec program appears to be progressing normally, just every step is taking a long time, for example it takes over half an hour to finish the rspec.configure block in spec_helper, minutes to execute a validate statement in a model. I have tried different tests and it is not any specific test code.
If I start up the rails server, the app appears to be working normally. The console appears to work normally as well, it is just rspec. I have tried spring stop and it makes no difference to outcome.
Any ideas on what is causing this?

Profiling of Ruby Minitest tests

I have a lot of slow-running Minitest tests for Rails app (3-4 minutes on one test). I want to profile them to learn how to fix them.
I tried to use standard Ruby profiler and ruby-prof - neither worked. Standard Ruby profiler profiles... something, but it prints its results BEFORE running tests, and I get no information on them. Ruby-prof simply hangs my tests if I include RubyProf.start in the test.
The Internet is full of advices on profiling Rails apps. Hovever, I did not find any guide for profiling tests for Rails apps. Please, help me choose proper strategy for test profiling!
I was able to add the profile to my unit tests successfully by including require 'profile' at the top of my test file. It does hang at the beginning (maybe for 30 seconds) when it's setting up, but after the tests ran I got a detailed output on how much time was spent in different functions.
I ran the tests from the command line with ruby -rprofile -I"lib:test" <test filepath>.

possible to debug/pry from methadone's App class?

I started a blank project with methadone, an awesome framework for building command line apps. The only problem is that I am unable to debug from within the App class that's in bin/my_app
The App class is a file created when you run methadone. Here's how I'm trying to use pry
#!/usr/bin/env ruby
require 'optparse'
require 'methadone'
require 'my_app'
require 'pry'
class App
include Methadone::Main
include Methadone::CLILogging
main do
binding.pry # <= pry here
end
...
When I run rake features I can tell the running process is trying to do something with pry since it pauses for a few seconds. I get the following error and then rake/cucumber is aborted.
process still alive after 3 seconds (ChildProcess::TimeoutError)
I can use pry just fine from the cucumber steps, rspec, or any other place, just not from anywhere in this App class.
One very interesting thing is that if I run my command line app from the console it WILL stop where pry is. It just wont pop into pry when using cucumber.
How can I get the app to work with pry when I'm running rake features?
Update
Sorry, I should clarify that methadone comes with aruba. So my cucumber scenario would look like this
When I successfully run `my_app arg1`
However, it WILL go into debug/pry if I run it with
bundle exec bin/my_app
Use pry-remote to connect to a pry session in the Aruba-managed subprocess.
(Disclosure: I paired with #Dty to come to this solution)
Aruba runs the app in a totally separate process, so I would guess what's happening is that when aruba runs your app, pry starts up at a prompt and waits for input. Since it doesn't get any, aruba times out after three seconds (the default it will wait for an app to complete). This is why you see the "process still alive" issue.
I'm not 100% sure how you could get the standard input of your shell that's running rake features to connect to your app's standard input so you could issue pry commands, but I don't think aruba was designed to allow this.
You have a couple of options:
Tag your scenario with #announce, and use When I run interactively... followed by several When I type - these commands should go to the interactive pry console that's waiting. Kind of kludgy, but it might work
Execute a unit test of your App class. You'll need to replace the call to go! with something like go! if $0 == __FILE__ so that you can require your executable in a test and manipulate App directly.
I have not tried either of these, but the second option feels a bit better and could also be improved with support from the library, if you can figure out a good way to do this.

How can I decrease my Rails test overhead?

I'm using Test::Unit on a large app with a large number of gem dependencies (>75). I'm trying to develop using BDD, but it takes minutes for the app to load it's dependencies before it can run the tests. Is there a way to preload the dependencies and just auto-run the test on changes, or a similar solution?
I would look into Spork. It works wonders.
https://github.com/sporkrb/spork
https://github.com/sporkrb/spork-testunit
I am using RSpec and there's a great tool for it, called Spork. It basically loads your app once and then just reloads modified parts. If you combine it with Guard, you get "continuous testing". That is, you hit 'Save' in your editor and tests start executing, giving you instant feedback. This still amazes me after some months :)
Edit
As #THEM points out, there's a plugin for Spork to support TestUnit. You should look into it.
There was also an interesting article about test speed on the 37Signals blog a while back. Might be of interest even if you end up going with Spork or another solution.

How to turn off rails 3.1 verbosity in test unit

I am trying to run rails tests in the old fashion i.e. dots as output but think I am missing something. I cant find where to turn off the verbosity mode and every time I run a rake task I get a list of test descriptions which at first look nice but end up being quite boring and not very helpful.
How can I turn off rails test unit verbosity mode and get back to old ....F....E..... ?
Thanks,
Remove the turn gem from your Gemfile.

Resources