phillee
phillee

Reputation: 2227

SimpleCov with Selenium/Rails

We have a suite of Selenium tests. I'd like to use SimpleCov to coverage the server-side coverage of those tests. First off, is this a common approach? I haven't been able to find anything on SimpleCov/Selenium. Maybe SimpleCov is usually used for unit/functional tests instead of integration?

Current Selenium setup requires booting up a rails server, than having a suite of Selenium tests hit it. I'd need SimpleCov to run on the rails server, then quit after the suite is done.

Any help greatly appreciated!

Upvotes: 4

Views: 1247

Answers (1)

TheDeadSerious
TheDeadSerious

Reputation: 862

simplecov author here. Whenever you launch SimpleCov, it applies the coverage analysis to the currently running process. Therefore, you would need to launch SimpleCov inside your Rails server process. I would recommend adding the SimpleCov setup as a conditional to your Rails app's config/boot.rb (at the very top), like so:

# config/boot.rb
if ENV["SELENIUM"]
  require 'simplecov'
  SimpleCov.start 'rails'
end

Before booting your Rails test server, set that environment variable. You should now receive a coverage report once the test server is shut down. Please check out the config options if you want to move it to another directory so it does not interfere with your regular (unit/functional) coverage report.

I am not sure that boot.rb is the right place though. The fact is that SimpleCov needs to be loaded before anything else in your app is required or it won't be able to track coverage for those files. You might need to experiment or have a look into the rails boot process to find that place, but as the Bundler setup is part of boot.rb (if I remember correctly...), putting the mentioned config above the Bundler.setup should be fine.

Basically, with a similar setup you can even get code coverage for your local manual browser-based testing by launching simplecov in your server process, clicking around and exiting the server, if for example you want to know what part of your application a certain action really touches.

Upvotes: 5

Related Questions