SlideShare a Scribd company logo
1 of 12
Introduction to
testing in Rails
Ben Collins
What is testing?
“Software testing is an investigation conducted to provide
stakeholders with information about the quality of the
product or service under test”
What?!?
It’s writing other bits of code to execute and run over our
codebase to check it works as we want, and provide
information to us when it doesn’t work.
Why test?
Many, many reasons….
● Can check that our big, complex programs work the way we want
● Can check software still works every time we make changes
● Can check software still works every time someone else makes changes
● Helps us remember and formalize what “correct behaviour” is
● i.e. permanently document conditions and responses we want
● the list goes on….
First “test”
Create a quick ruby file with this code:
require 'pry'
def add_one(number)
number + 1
end
binding.pry
What happens when we call each of the following in Pry:
add_one(3), add_one(4.5), add_one(“one”), add_one(), add_one(nil)
First “test” cont’d...
Change the add_one method code to:
def add_one(number = “No number given”)
if number.is_a?(Numeric)
puts “Calculation was successful”
number + 1
else
raise ArgumentError, "Numbers only please"
end
end
Now try: add_one(3), add_one(“one”), add_one(), add_one(nil)
Testing in Rails
Can create powerful, automated test suites easily* in Rails.
Ruby/Rails have the Minitest and Rspec frameworks.
These are testing frameworks that interact with our codebase and can run a full
test suite automatically, from a single command line prompt.
By default, every Rails application has three environments: development, test,
and production. For testing, you work inside the test environment, so
development and production data is not compromised.
* “easily” only really applies to developers who know what they’re doing.
Test Driven Development
Test Driven Development (TDD) is a software development
process in which the developer writes tests for a specific
feature or behavior first (which obviously then fail initially),
before writing code to pass the tests.
The Michael Hartl Rails Tutorial teaches test driven
development from the beginning.
Minitest - ships with Rails
http://guides.rubyonrails.org/testing.html
Rails creates a test folder for you as soon as you create a Rails project.
Use “rake test” in command line to run entire test suite.
Use “rake test test/models/[TEST NAME HERE].rb” to run a specific test, in
this case a test on our model.
Example of a Minitest
e.g. testing our todo model:
require 'test_helper'
class TodoTest < ActiveSupport::TestCase
test "todo should not save without item content" do
todo = Todo.new
assert_not todo.save, "Saved the todo without item content"
end
end
https://github.com/benlcollins/testing_experiments/blob/master/todo_testing_app/test/models/todo_test.rb
The Rails package at: https://github.com/rspec/rspec-rails
Add the gem to your Gemfile:
group :development, :test do
gem 'rspec-rails', '~> 3.0'
end
Then “bundle install”
Then initialize the spec directory “rails generate rspec:install”
To run tests “bundle exec rspec”
or “bundle exec rspec spec/models/[TEST NAME HERE].rb”
RSpec - popular testing framework
Example of RSpec
Example of an RSpec test (same model test as above):
require 'rails_helper'
RSpec.describe Todo, :type => :model do
it "doesn't save new todo with blank item content" do
new_todo = Todo.new
expect(new_todo).not_to be_valid
end
end
https://github.com/benlcollins/testing_experiments/blob/rspec_example/todo_testing_app/spec/models/todo_spec.rb
Further resources
Good intro: http://www.codenewbie.org/blogs/testing-your-code
Rails Minitest docs: http://guides.rubyonrails.org/testing.html
RSpec docs: http://rspec.info/
RSpec best practises: http://betterspecs.org/
How we test Rails apps: https://robots.thoughtbot.com/how-we-
test-rails-applications

More Related Content

What's hot

Nexthink Library - replacing a ruby on rails application with Scala and Spray
Nexthink Library - replacing a ruby on rails application with Scala and SprayNexthink Library - replacing a ruby on rails application with Scala and Spray
Nexthink Library - replacing a ruby on rails application with Scala and Spray
Matthew Farwell
 
Zero to Testing in JavaScript
Zero to Testing in JavaScriptZero to Testing in JavaScript
Zero to Testing in JavaScript
pamselle
 
Behavior Driven Development with Cucumber
Behavior Driven Development with CucumberBehavior Driven Development with Cucumber
Behavior Driven Development with Cucumber
Brandon Keepers
 

What's hot (20)

RSpec: What, How and Why
RSpec: What, How and WhyRSpec: What, How and Why
RSpec: What, How and Why
 
Djangocon 2014 angular + django
Djangocon 2014 angular + djangoDjangocon 2014 angular + django
Djangocon 2014 angular + django
 
TDD with phpspec2
TDD with phpspec2TDD with phpspec2
TDD with phpspec2
 
Nexthink Library - replacing a ruby on rails application with Scala and Spray
Nexthink Library - replacing a ruby on rails application with Scala and SprayNexthink Library - replacing a ruby on rails application with Scala and Spray
Nexthink Library - replacing a ruby on rails application with Scala and Spray
 
Zero to Testing in JavaScript
Zero to Testing in JavaScriptZero to Testing in JavaScript
Zero to Testing in JavaScript
 
Integration and Acceptance Testing
Integration and Acceptance TestingIntegration and Acceptance Testing
Integration and Acceptance Testing
 
Building an API with Django and Django REST Framework
Building an API with Django and Django REST FrameworkBuilding an API with Django and Django REST Framework
Building an API with Django and Django REST Framework
 
MidwestJS Zero to Testing
MidwestJS Zero to TestingMidwestJS Zero to Testing
MidwestJS Zero to Testing
 
2011-02-03 LA RubyConf Rails3 TDD Workshop
2011-02-03 LA RubyConf Rails3 TDD Workshop2011-02-03 LA RubyConf Rails3 TDD Workshop
2011-02-03 LA RubyConf Rails3 TDD Workshop
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJS
 
Build a bot workshop async primer - php[tek]
Build a bot workshop  async primer - php[tek]Build a bot workshop  async primer - php[tek]
Build a bot workshop async primer - php[tek]
 
greach 2014 marco vermeulen bdd using cucumber jvm and groovy
greach 2014 marco vermeulen bdd using cucumber jvm and groovygreach 2014 marco vermeulen bdd using cucumber jvm and groovy
greach 2014 marco vermeulen bdd using cucumber jvm and groovy
 
Factory Girl
Factory GirlFactory Girl
Factory Girl
 
Cucumber
CucumberCucumber
Cucumber
 
Spring rest-doc-2015-11
Spring rest-doc-2015-11Spring rest-doc-2015-11
Spring rest-doc-2015-11
 
Selenide alternative in Python - Introducing Selene [SeleniumCamp 2016]
Selenide alternative in Python - Introducing Selene [SeleniumCamp 2016]Selenide alternative in Python - Introducing Selene [SeleniumCamp 2016]
Selenide alternative in Python - Introducing Selene [SeleniumCamp 2016]
 
Writing automation tests with python selenium behave pageobjects
Writing automation tests with python selenium behave pageobjectsWriting automation tests with python selenium behave pageobjects
Writing automation tests with python selenium behave pageobjects
 
Django rest framework tips and tricks
Django rest framework   tips and tricksDjango rest framework   tips and tricks
Django rest framework tips and tricks
 
Test automation with Cucumber-JVM
Test automation with Cucumber-JVMTest automation with Cucumber-JVM
Test automation with Cucumber-JVM
 
Behavior Driven Development with Cucumber
Behavior Driven Development with CucumberBehavior Driven Development with Cucumber
Behavior Driven Development with Cucumber
 

Similar to Introduction to testing in Rails

Similar to Introduction to testing in Rails (20)

TDD - Agile
TDD - Agile TDD - Agile
TDD - Agile
 
Python and test
Python and testPython and test
Python and test
 
Unit Testing in R with Testthat - HRUG
Unit Testing in R with Testthat - HRUGUnit Testing in R with Testthat - HRUG
Unit Testing in R with Testthat - HRUG
 
Testing practicies not only in scala
Testing practicies not only in scalaTesting practicies not only in scala
Testing practicies not only in scala
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
Continuous Delivery - Automate & Build Better Software with Travis CI
Continuous Delivery - Automate & Build Better Software with Travis CIContinuous Delivery - Automate & Build Better Software with Travis CI
Continuous Delivery - Automate & Build Better Software with Travis CI
 
Introduction to Behavior Driven Development
Introduction to Behavior Driven Development Introduction to Behavior Driven Development
Introduction to Behavior Driven Development
 
SELJE_Database_Unit_Testing.pdf
SELJE_Database_Unit_Testing.pdfSELJE_Database_Unit_Testing.pdf
SELJE_Database_Unit_Testing.pdf
 
Drupal 7 ci and testing
Drupal 7 ci and testingDrupal 7 ci and testing
Drupal 7 ci and testing
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
Tdd
TddTdd
Tdd
 
Nagios Conference 2011 - Nathan Vonnahme - Integrating Nagios With Test Drive...
Nagios Conference 2011 - Nathan Vonnahme - Integrating Nagios With Test Drive...Nagios Conference 2011 - Nathan Vonnahme - Integrating Nagios With Test Drive...
Nagios Conference 2011 - Nathan Vonnahme - Integrating Nagios With Test Drive...
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
 
We continue checking Microsoft projects: analysis of PowerShell
We continue checking Microsoft projects: analysis of PowerShellWe continue checking Microsoft projects: analysis of PowerShell
We continue checking Microsoft projects: analysis of PowerShell
 
10071756.ppt
10071756.ppt10071756.ppt
10071756.ppt
 
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex ScenariosUnit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 

Recently uploaded

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Recently uploaded (20)

%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 

Introduction to testing in Rails

  • 1. Introduction to testing in Rails Ben Collins
  • 2. What is testing? “Software testing is an investigation conducted to provide stakeholders with information about the quality of the product or service under test” What?!? It’s writing other bits of code to execute and run over our codebase to check it works as we want, and provide information to us when it doesn’t work.
  • 3. Why test? Many, many reasons…. ● Can check that our big, complex programs work the way we want ● Can check software still works every time we make changes ● Can check software still works every time someone else makes changes ● Helps us remember and formalize what “correct behaviour” is ● i.e. permanently document conditions and responses we want ● the list goes on….
  • 4. First “test” Create a quick ruby file with this code: require 'pry' def add_one(number) number + 1 end binding.pry What happens when we call each of the following in Pry: add_one(3), add_one(4.5), add_one(“one”), add_one(), add_one(nil)
  • 5. First “test” cont’d... Change the add_one method code to: def add_one(number = “No number given”) if number.is_a?(Numeric) puts “Calculation was successful” number + 1 else raise ArgumentError, "Numbers only please" end end Now try: add_one(3), add_one(“one”), add_one(), add_one(nil)
  • 6. Testing in Rails Can create powerful, automated test suites easily* in Rails. Ruby/Rails have the Minitest and Rspec frameworks. These are testing frameworks that interact with our codebase and can run a full test suite automatically, from a single command line prompt. By default, every Rails application has three environments: development, test, and production. For testing, you work inside the test environment, so development and production data is not compromised. * “easily” only really applies to developers who know what they’re doing.
  • 7. Test Driven Development Test Driven Development (TDD) is a software development process in which the developer writes tests for a specific feature or behavior first (which obviously then fail initially), before writing code to pass the tests. The Michael Hartl Rails Tutorial teaches test driven development from the beginning.
  • 8. Minitest - ships with Rails http://guides.rubyonrails.org/testing.html Rails creates a test folder for you as soon as you create a Rails project. Use “rake test” in command line to run entire test suite. Use “rake test test/models/[TEST NAME HERE].rb” to run a specific test, in this case a test on our model.
  • 9. Example of a Minitest e.g. testing our todo model: require 'test_helper' class TodoTest < ActiveSupport::TestCase test "todo should not save without item content" do todo = Todo.new assert_not todo.save, "Saved the todo without item content" end end https://github.com/benlcollins/testing_experiments/blob/master/todo_testing_app/test/models/todo_test.rb
  • 10. The Rails package at: https://github.com/rspec/rspec-rails Add the gem to your Gemfile: group :development, :test do gem 'rspec-rails', '~> 3.0' end Then “bundle install” Then initialize the spec directory “rails generate rspec:install” To run tests “bundle exec rspec” or “bundle exec rspec spec/models/[TEST NAME HERE].rb” RSpec - popular testing framework
  • 11. Example of RSpec Example of an RSpec test (same model test as above): require 'rails_helper' RSpec.describe Todo, :type => :model do it "doesn't save new todo with blank item content" do new_todo = Todo.new expect(new_todo).not_to be_valid end end https://github.com/benlcollins/testing_experiments/blob/rspec_example/todo_testing_app/spec/models/todo_spec.rb
  • 12. Further resources Good intro: http://www.codenewbie.org/blogs/testing-your-code Rails Minitest docs: http://guides.rubyonrails.org/testing.html RSpec docs: http://rspec.info/ RSpec best practises: http://betterspecs.org/ How we test Rails apps: https://robots.thoughtbot.com/how-we- test-rails-applications