I am using minitest with Jenkins to produce test reports. At the moment I am using Minitest::Reporters::JUnitReporter - https://github.com/kern/minitest-reporters. This seems to give nice concise results.
The only thing missing is an embedded screenshot, specifically from failed tests.
How can I produce a Jenkins friendly test report that includes a screenshot? I am open to use one of the other Minitest::Reporters if it helps.
Thanks
You can use the capybara-screenshot gem for this: https://github.com/mattheworiordan/capybara-screenshot
Gemfile:
group :test do
gem 'capybara', '2.6.0'
gem 'selenium-webdriver', '2.49.0'
gem 'poltergeist', '1.8.1'
gem 'capybara-screenshot', '1.0.11'
end
test_helper.rb:
# default rails test configuration
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
# configure webtests with capybara and poltergeist (headless)
require 'capybara/rails'
require 'capybara/poltergeist'
require 'capybara-screenshot/minitest'
if ENV['HEADLESS'] == 'true'
# headless driver configuration
Capybara.default_driver = :poltergeist
else
Capybara.default_driver = :selenium
end
Capybara::Screenshot.prune_strategy = :keep_last_run # keep screenshots only from the last test run
Capybara.save_and_open_page_path = File.join(Rails.root, "test/screenshots") # where to save screenshots
# default test class for unit tests
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
# default test class for webtests
class ActionDispatch::IntegrationTest
include Capybara::DSL
include Capybara::Screenshot::MiniTestPlugin
end
Related
I don't get capybara working with my modular sinatra app. I created a small test app and have the same issue. I get always a 504 Timeout Error and i think that the app isn't really loaded by Capybara regardless the Line Capybara.app = Testapp in the acceptance_helper.
Browser Firefox opens when test is running but don't go to any URL and shows always a blank site.
When i do the same test only with rspec it works (second one).
What i'm doing wrong?
My Setup:
OS: Win 7 Professional 64-bit
Ruby version: ruby 2.2.2p95 (2015-04-13 revision 50295) [x64-mingw32]
Firefox version: 40.0.3
gem versions
- sinatra (1.4.6)
- rspec (3.3.0)
- capybara (2.7.1)
- selenium-webdriver (2.53.4)
- thin (1.5.1)
- rack (1.6.4)
- rack-test (0.6.3)
App structure:
test_sinatra_capybara
|
|- testapp.rb
|- spec
| |- spec_helper.rb
| |- acceptance_helper.rb
| |- testapp_spec.rb
|- views
| |- index.slim
testapp.rb
require 'sinatra'
require 'sinatra/base'
require 'slim'
class Testapp < Sinatra::Base
get '/' do
slim :index
end
end
index.slim
#test-id
| Willkommen
spec_helper.rb
require 'rspec'
require 'rack/test'
require_relative '../testapp'
Testapp.environment = :test
module RSpecMixin
include Rack::Test::Methods
def app() Testapp end
end
RSpec.configure do |config|
config.include RSpecMixin
# Use color in STDOUT
config.color = true
# Use the specified formatter
config.formatter = :documentation
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
acceptance_helper.rb
require File.dirname(__FILE__) + '/spec_helper'
require 'capybara'
require 'capybara/rspec'
Capybara.default_driver = :selenium
Capybara.app = Testapp
RSpec.configure do |config|
config.include Capybara::DSL
config.include Capybara::RSpecMatchers
end
testapp_spec.rb
require File.dirname(__FILE__) + '/acceptance_helper'
describe 'URLs that require login' do
it "start page capybara" do
# I tried both
visit '/'
# visit 'http:localhost:4567'
page.should have_content("Willkommen")
end
it "start page rspec" do
get "/"
expect(last_response.body).to include("Willkommen")
end
end
So as i said, last test is working:
Finished in 12.91 seconds (files took 2.54 seconds to load)
2 examples, 1 failure
Failed examples:
rspec ./spec/testapp_spec.rb:4 # URLs that require login start page capybara
Error on First test:
Selenium::WebDriver::Error::WebDriverError:
unexpected response, code=504, content-type="text/html"
Running the exact code posted works fine for me, which I'm guessing means one of two things. Either the version of Firefox you're using is outdated (make sure you're using 47.0.1) or you're on a machine that has a proxy server or firewall that is interfering with requests to 127.0.0.1:<some random port>. The default Capybara config starts up a server on 127.0.0.1:<random port> to handle requests for the app under test. If those connections are blocked or proxied elsewhere it will obviously prevent things from working properly. If you need to bind to a different interface or set a fixed port to get around corporate firewall/proxy restrictions you can do that by setting Capybara.server_host and Capybara.server_port
I'm setting up a standalone RSpec/Capybara test suite integrated with SauceLabs, but the instructions in the documentation don't seem to be working for me.
Here are the relevant parts of my spec_helper.rb:
require 'capybara'
require 'capybara/rspec'
require 'sauce/capybara'
Sauce.config do |config|
config[:browsers] = [
[ "OSX 10.10", "Safari", "8" ]
]
end
Capybara.default_driver = :sauce
And here's the feature (not_found_spec.rb):
feature 'Enroll: 404', :sauce => true do
before :each do
#nonexistent_curriculum = FactoryGirl.build :curriculum
#enroll = Enroll.new
end
context 'When I visit a page that does not exist' do
scenario 'I see a Not Found message' do
#enroll.go #nonexistent_curriculum
expect(#enroll.not_found).to be_visible
end
end
end
When I then run rspec, the specs run and pass, but no metadata of any kind is recorded. All I see on SauceLabs is "Unnamed Ruby job".
What am I missing?
When you run
bundle exec rake sauce:install:spec
it creates a sauce_helper.rb which is then typically required from the end of your rails_helper.rb or spec_helper.rb depending on what *_helper.rb files you have. It looks like you copied the Sauce config part from sauce_helper into your spec_helper but you haven't shown that you have
require "sauce"
in there which is in the generated sauce_helper. Without requiring "sauce" it may be that sauce/rspec/rspec.rb is not getting required which is where all the hooks into rspec for tests with sauce: true are set up.
I'm using rspec to test my Sinatra app. The app is super simple, and so are the tests, but when I run rspec from the CLI, it just starts the app server. I've never had this happen.
Here's what my spec_helper.rb looks like:
#spec/spec_helper.rb
require File.expand_path '../../app.rb', __FILE__
ENV['RACK_ENV'] = "test"
require 'rspec'
require 'rack/test'
set :environment, :test
set :run, false
set :raise_errors, true
set :logging, false
module RSpecMixin
def app
App.new
end
end
RSpec.configure do |config|
config.color_enabled = true
config.tty = true
config.formatter = :documentation
config.include Rack::Test::Methods
config.include RSpecMixin
end
And my spec is just
require 'spec_helper'
describe "My Sinatra Application" do
it "should allow accessing the home page" do
expect(1).to eq(1)
end
end
I can't get rspec to run.
What I've tried:
I've tried the recommendations in this Sinatra testing guide. Also the rspec Sinatra test recipe. In this blog post, the set :run, false looked promising, but no dice. Then I thought, I'll just define a rake task. Putting the rspec gem in the test group and setting the RACK_ENV to test. All these things just start the app server:
$ rake spec
[2014-03-08 22:06:38] INFO WEBrick 1.3.1
[2014-03-08 22:06:38] INFO ruby 2.0.0 (2013-02-24) [x86_64-darwin11.4.2]
== Sinatra/1.4.4 has taken the stage on 4567 for development with backup from WEBrick
[2014-03-08 22:06:38] INFO WEBrick::HTTPServer#start: pid=21538 port=4567
Halp?
Try removing the new() from your def app:
module RSpecMixin
def app
App
end
end
An other way to create your app for testing is something like this:
APP = Rack::Builder.parse_file('config.ru').first
module RSpecMixin
def app
APP
end
end
Obvious this works only if you use config.ru.
I found the answer. It's embarrassing, but I was switching back and forth between the modular and classic style deciding which I liked best, and I had left an errant App.run! call at the bottom of my app.rb file.
I am trying to run Minitest with Spec syntax with rake test and get this error:
/path/to/gem/spec/script_spec.rb:3:in `<top (required)>': uninitialized constant MyGem (NameError)
My Rakefile:
require 'rake/testtask'
Rake::TestTask.new do |t|
t.test_files = FileList['spec/*_spec.rb']
end
My file structure:
gem/
--lib/
----script.rb
--spec/
----script_spec.rb
--Rakefile
My script.rb:
module MyGem
class OptionParser
def self.option?(arg)
arg =~ /^-{1,2}\w+$/
end
end
end
Using Minitest::Spec syntax in script_spec.rb:
require "minitest/autorun"
describe MyGem::OptionParser do
describe "option?" do
it "must be true for option name" do
OptionParser.option?('--nocolor').assert true
end
end
end
How do I fix it? Maybe lib folder isn't loaded? Do I miss something related to Spec syntax?
MyGem::OptionParser is not loaded in your tests. You either need to require it in your spec file or create a spec_helper where you require all files that you need in all your tests so you only need to require 'spec_helper' in your specs.
Also, since you're using the spec syntax, you will have to `require 'minitest/spec'. Your spec_helper would look something like:
# spec/spec_helper.rb
require 'minitest/spec'
require 'minitest/autorun'
require 'script'
And do this to your Rakefile so you can do require 'script' like above in your specs instead of doing require_relative '../lib/script'.
require 'rake/testtask'
Rake::TestTask.new do |t|
t.test_files = FileList['spec/*_spec.rb']
end
Lastly, for your spec to work, add require 'spec_helper' at the top of your script_spec file. You'll have to do this for all your spec files and make sure to add require for all the files you need to load in your specs to your spec_helper file.
Since you're also doing spec-style testing, you might want to change your test to this:
MyGem::OptionParser.option?('--nocolor').must_equal true
You could also have code like this in your 'spec_helper' file to automatically load all files in your lib folder:
Dir["../lib/**/*.rb"].each do |rb_file|
require rb_file
end
Hope this helps!
At the minute i have rspec tests in a folder called views within the spec folder and the tests run fine. What im looking to do is move the rspec tests to the integration folder within the test folder than gets generated at the start when you create an new app.
When i move the rspec tests and try and run them i get an error saying it cant find the spec_helper.rb so ive transferred that info into the test_helper.rb. When i try and run the test it now says it cant find the test_helper.rb. The test_helper.rb is located in the test folder.
spec_helper.rb code
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require "rspec"
require 'rspec/rails'
require "rubygems"
require "capybara/rspec"
require "capybara/dsl"
RSpec.configure do |config|
config.mock_with :rspec
config.include Capybara::DSL
config.formatter = :documentation # :progress, :html, :textmate
end
Capybara.configure do |config|
config.run_server=true
config.default_driver=:selenium
end
end
So is there a configuration somewhere that i have to change. Any thoughts or suggestions?
Thanks
Ray