i use minitest framework for testing and simplecov gem for code coverage. i have a problem about simplecov. my problem is lkie this:
i wrote a model test. when i run the test using rake minitest:models, test runs and coverage shows %100. But when i run tests using bundle exec rake, the code coverage of same tests are shown missing.
i make researching on net. some people also have problem like this about simplecov. but i could not find a solution for this.
i'm waiting your ideas. Thanks in advance.
Did you enabled simplecov by doing SimpleCov.start on top of your code?
This is required as the first statement i.e. before your code, otherwise you will never get it work.
Also include the SimpleCoV Adapter.
As following the post generating-code-coverage-metrics-for-a-ruby-on-rails-project-with-simplecov, define rules with SimpleCov putting conditions:
SimpleCov.start do
# rules here
end if ENV["COVERAGE"]
Then run the coverage on demand by specifying the coverage variable:
COVERAGE=true bundle exec rake spec
Related
I am using VsCode to write and edit some rspec tests.
What I would like to be able to do is run a specific 'it' or 'describe' block in debug mode.
At this time I can run the rspec/spec file in debug mode but it executes all of the tests.
I have install 'Rails run Spec' extension which allows me to execute a specific 'it' or 'describe' block without the ability to debug.
Ideally I would like both options married together.
I have done some digging but not able to find anything that fits my scenario.
Any help would be greatly appreciated.
Joe
Add pry-byebug to your gemfile and run bundle install
# Gemfile
gem 'pry-byebug'
Then, whenever you want to inspect a test, add binding.pry inside the test.
# some_spec.rb
it "is not behaving how I want it to" do
binding.pry
expect(my_var).to eq(some_val)
end
I want to analyse the test coverage of our code , and therefore, installed the simplecov gem.
Our testing environement has 2 seperate project: REST API test (Java+Rest-Assured) and Web UI testing (Java-Selenium).
As you can see, we dont have unit testing inside the rails app, and we are testing using external projects.
I configured the simplecov gem as descriped in the tutorial and put this, in the rails script:
require 'simplecov'
SimpleCov.start 'rails'
puts "require simplecov"
When loading the app, I see the string I printed.
I ran both automation test projects, saw their printouts in the rails log, but I don't see any coverage of controllers/models, I see only small precentage of initializtion files of some gems and rails.
I searched the net, and tried putting the code phrase in boot.rb or even puma.rb and it returned the same results.
Any ideas?
EDIT
Nothing helped with all the comments, but I figured out something very interesting, in all cases, I only see the name of methods marked as tested, not the content (in controllers).
I tried to put the simplecov start phrase in both bin/rails, puma.rb, config.ru, environment.rb, all not given the desired results of code coverage.
I'm not sure simplecov can measure the whole rails app coverage... But I googled something that you can attach as a rack middleware:
https://github.com/danmayer/coverband
And it's output is compatible with simplecov. So it looks like it could be useful in your case.
As you mentioned in your question you're using puma. I suspect that, since it's multi-threaded, it spawns a few rails apps and their simplecov output overwrites each other's results. I'd try with the single threaded server like webrick - but this may make your tests slower (depending on how the tests are fired up really) or try the coverband gem.
Also - even if the server is single threaded - I'm not sure if each request would not overwrite simplecov's output.
Maybe you have to specify the paths
require 'simplecov'
SimpleCov.start do
# add_filter '/admin/'
add_group "Models", "app/models"
add_group "Controllers", "app/controllers"
add_group "Lib", "lib/"
add_group "Helpers", "app/helpers"
end
You need to to start SimpleCov before loading any of your files, so put these lines as early as possible in your ruby entrypoint:
require 'simplecov'
SimpleCov.start
You could see an example in one of my repos here:
https://github.com/tareksamni/DockUp/blob/master/spec/spec_helper.rb
I do autoload my ruby code after starting SimpleCov. You need to the same as well:
require 'simplecov'
SimpleCov.start
require './autoload'
No matter what I do, the only tests that run with the rake test command are those in spec/requests. Naturally, I would like to run everything in the spec directory.
I thought getting the gem and installing RSpec would do it, but it seems with these testing libraries that the whole "convention over configuration" thing is turned on its head. There's a hell of a lot of configuration.
I simply want to run all of my tests. How can I do that?
What does your Rakefile look like? You may need to add the following:
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new
Also, check that your spec's filenames all end in _spec.rb.
Just this will do:
bundle exec rspec spec
This must be fired from root directory of application. Here spec is the directory.
Autotest increases the speed at which tests run by running only the changed tests.
But I want to push it even further by using spork to preload the Rails environment, so that I will get even faster feedback.
Is this possible?
Autotest : https://github.com/grosser/autotest
Spork : http://github.com/timcharper/spork
ARTICLE1 mikbe has you covered! I would attempt to rephrase it here, but the post does such a great job.
If you happen to be on OSX, there are also instructions to utilize Growl for tray notifications.
ARTICLE2 Ruby Inside also has a walkthrough for Rails 3 and RSpec 2.
If you use Ruby 1.9 and want to use spork and autotest together with Test::Unit (actually MiniTest) try this:
Gemfile:
group :test do
# Newer version of test::unit:
gem 'minitest'
# spork preloads a rails instance which is forked every time the tests are
# run, removing test startup time.
gem 'spork'
# Run 'spork minitest' to start drb server (test server). Use 'testdrb' to
# run individual tests via spork.
gem 'spork-minitest'
# Run 'bundle exec autotest' to rerun relevant tests whenever a file/test is
# changed. '.autotest' makes sure the tests are run via test server (spork).
gem 'autotest-standalone'
# -pure gives us autotest without ZenTest gem.
gem 'autotest-rails-pure'
end
.autotest:
class Autotest
# run tests over drb server (spork)
def make_test_cmd files_to_test
if files_to_test.empty?
"" # no tests to run
else
"testdrb #{files_to_test.keys.join(' ')}"
end
end
end
(Note: Instructions says bin/testdrb, but I changed it to testdrb to make it work for me.)
In a terminal:
spork minitest --bootstrap
Edit test/test_helper.rband follow instructions.
After the above setup is done once, you can start the test server:
spork minitest
Finally start autotest in another terminal:
bundle exec autotest
And (hopefully) enjoy really fast autotesting with MiniTest.
I haven't tried it yet, but there's a section in chapter 3 of the RailsTutorial that tells some "hacks" to set up spork. The tutorial currently says:
... as of this writing Spork doesn’t officially support Rails 3
The chapter goes on to tell how to set it up with autotest. One thing to know is that you'll need
--drb
in your .rspec file.
I'm working on a plugin for Rails that would add limited in-memory caching to ActiveRecord's finders. The functionality itself is mature enough, but I can't for the life of me get unit tests to work with the plugin.
I now have under vendor/plugins/my_plugin/test/my_plugin_test.rb a standard subclass of ActiveSupport::TestCase with a couple of basic tests. I try running 'rake test' from the plugin directory, and I have confirmed that this task loads the ruby file with the test case, but it doesn't actually run any of the tests.
I followed the Rails plugin guide (http://guides.rubyonrails.org/plugins.html) where applicable, but it seems to be horribly outdated (it suggests things that Rails now do automatically, etc.)
The only output I get is this:
Kakadu:ingenious_record adam$ rake test
(in /Users/adam/Sites/1_PRK/vendor/plugins/ingenious_record)
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -Ilib:lib:test "/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/ingenious_record_test.rb"
The simplest test case looks like this:
require 'test_helper'
require 'active_record'
class IngeniousRecordTest < ActiveSupport::TestCase
test "example" do
assert false
end
end
This should definitely produce at least some output, and the only test in that file should produce a failed assertion.
Any ideas what I could do to get Rails to run my tests?
I test my plugins using the Engines plugin's rake tasks:
rake test:plugins:all PLUGIN=myplugin
I'm sure you can do it without Engines, but it is an option. Recent versions of Rails (I'm still on 2.2.2) have Engines support integrated.
Apparently there is a little problem with rails 2.2.3, as it does not include the 'test/unit' package/file in plugins.
In order to fix your plugin problems, just add the following to the top of your test_helper.rb file:
require 'test/unit'
Found it as a ticket at lighthouse.