Autotesting rails app

37
Autotesting Rails Covering Rails MVP with autotests

Transcript of Autotesting rails app

Page 1: Autotesting rails app

Autotesting Rails

Covering Rails MVP with autotests

Page 2: Autotesting rails app

Contents

● Why?

● Rspec, shoulda-matchers, FactoryGirl

● Cucumber

● Simplecov

● What next?

Page 3: Autotesting rails app

WHY?

Page 4: Autotesting rails app

Problem

● MVP is ready

● First customers

● Not stable

FAIL

Page 5: Autotesting rails app

Solution

● Manual testing = expensive (time + $)

● Autotesting?

● Yes, we can!

Page 6: Autotesting rails app

TDD - Rspec, shoulda-matchers, FactoryGirl

Page 7: Autotesting rails app

● All instructions

https://github.com/rspec/rspec-rails

Rspec - testing framework

Page 8: Autotesting rails app

Rspec - update Gemfile

group :development, :test do

gem 'rspec-rails', '~> 3.0'

end

Page 9: Autotesting rails app

Rspec - install and generate files

$ bundle install --without production

$ rails generate rspec:install

$ bundle exec rspec

Page 10: Autotesting rails app

Shoulda-matchers - testing one liners

● All instructions

https://github.com/thoughtbot/shoulda-

matchers

Page 11: Autotesting rails app

Shoulda - update Gemfile

group :test do

gem 'shoulda-matchers', require: false

end

Run

$ bundle install --without production

Page 12: Autotesting rails app

Shoulda - update rails_helper

● in spec/rails_helper.rb

● after

require 'rspec/rails'

● add

require 'shoulda/matchers'

Page 13: Autotesting rails app

FactoryGirl - setting up test objects

● All instructions

https://github.com/thoughtbot/factory_girl

Page 14: Autotesting rails app

FactoryGirl - update Gemfile

group :test do

gem 'factory_girl'

end

Run

$ bundle install --without production

Page 15: Autotesting rails app

FactoryGirl - update Gemfile

group :test do

gem 'factory_girl'

end

Run

$ bundle install --without production

Page 16: Autotesting rails app

Generate test files for existing model

Run (replace Post with your existing model)

$ rails generate model Post -s

See generated spec and factory files

Page 17: Autotesting rails app

BDD - Cucumber

Page 18: Autotesting rails app

● All instructions specific to Rails

https://github.com/cucumber/cucumber-rails

● And overall http://cukes.info/

Cucumber - testing framework

Page 19: Autotesting rails app

Cucumber - update Gemfile

group :test do

gem 'cucumber-rails', :require => false

gem 'database_cleaner'

end

Page 20: Autotesting rails app

Cucumber - update env.rb

Add to the end of features/support/env.rb

World FactoryGirl::Syntax::Methods

Page 21: Autotesting rails app

Cucumber - install

$ bundle install --without production

$ rails generate cucumber:install

$ bundle exec cucumber

Page 22: Autotesting rails app

Capybara-webkit - testing javascript

● All instructionso https://github.com/thoughtbot/capybara-webkit

o https://github.com/leonid-shevtsov/headless

Page 23: Autotesting rails app

Capybara-webkit - update Gemfile

group :test do

gem 'capybara-webkit', '~> 1.3.0'

gem 'headless'

end

Run

$ sudo apt-get install xvfb

$ bundle install --without production

Page 24: Autotesting rails app

Cucumber - update env.rb

Add to features/support/env.rbCapybara.app = Rack::ShowExceptions.new(Rails.application)

# use webkit driver for javascript testing

Capybara.javascript_driver = :webkit

if Capybara.javascript_driver == :webkit

require 'headless'

headless = Headless.new

headless.start

end

Page 25: Autotesting rails app

Code coverage with tests

Page 26: Autotesting rails app

● All instructions

https://github.com/colszowka/simplecov

Simplecov - code coverage

Page 27: Autotesting rails app

Simplecov - update Gemfile

group :test do

gem 'simplecov', :require => false

end

Run

$ bundle install --without production

Page 28: Autotesting rails app

Simplecov - update rails_helper

Add at the very top of spec/rails_helper.rb

require 'simplecov'

SimpleCov.command_name 'RSpec'

Page 29: Autotesting rails app

Simplecov - update env.rb

Add at the very top of features/support/env.rb

require 'simplecov'

SimpleCov.command_name 'Cucumber'

Page 30: Autotesting rails app

Add file .simplecov to rails app root

SimpleCov.start 'rails' do

end

Page 31: Autotesting rails app

Update .gitignore

Append to the file

/coverage

Page 32: Autotesting rails app

Code coverage

Run

$ bundle exec rspec

$ bundle exec cucumber

Open in the browser

coverage/index.html

Page 33: Autotesting rails app

Code coverage report

Page 34: Autotesting rails app

What next?

Page 35: Autotesting rails app

● https://www.google.com.ua/search?q=rspec

+tutorial

● https://www.google.com.ua/search?q=cucum

ber+tutorial

Get down to work and write tests

Page 36: Autotesting rails app

● $$$ Jira + Stash + Bamboo

● Free (even private) Bitbucket + Jenkins*

● Free Github + Jenkins*

* you need servers

Setup continuous integration

Page 37: Autotesting rails app

Anton Naumenko

www.syndicode.co