SlideShare a Scribd company logo
Best practices for writing
                 automated tests




     Felipe Lima
felipe@gogobot.com
      @felipecsl
• Assumption 1: You are already
 convinced that automated tests are
 good
• Assumption II: You agree that best
 way to accomplish that is writing
 tests before you code.
What is considered to be a
        good test?
Structure - AAA:


• Arrange
• Act
• Assert
Best practices for writing good automated tests
What makes a good test?
• Assumes a clean environment (empty
 database, redis, solr, etc.)

• Cleans up after itself
• Runs quickly
• Readable, simple to understand
• Tests only one thing at a time
• Isolates the class you are testing
• Stubs external dependencies (eg.: vcr)
• Thorough: covers happy and edge cases
• Repeatable: always provides same results
• Doesn’t make unnecessary assertions
• Clearly and consistently named
SOLID

• Single Responsibility Principle
• Open/closed principle
• Liskov substitution principle
• Interface segregation principle
• Dependancy Inversion principle
Why write tests before the code?
• SOLID code is HIGHLY testable
• TDD forces you to think about your
 design

• Bad code is hard to test
• It is a design process, not a testing process
“TDD is a robust way of designing
software components (“units”)
interactively so that their behaviour is
specified through unit tests.”




 http://blog.stevensanderson.com/2009/08/24/writing-great-unit-tests-best-and-worst-practises/
http://blog.stevensanderson.com/2009/08/24/writing-great-unit-tests-best-and-worst-practises/
TDD Mantra:

Red => Green => Refactor




http://www.agileapps.co.uk/methodology/continuous.html
Mocks and Stubs:

• Mocks set up expectations on messages that
 are passed between objects

• Stubs provide canned answers to calls made
 during the test

• Only mocks specify expected behavior and
 make assertions

• Also know as test doubles (eg.: in RSpec)

       Reference: http://martinfowler.com/articles/mocksArentStubs.html
Stub example:

controller.stub(:current_user).and_return(fake_user)




Mock example:
user.should_receive(:postcards).and_return([p1, p2, p3])
postcards = controller.postcards(user.id)
postcards.should =~ [p1, p2, p3]

More Related Content

What's hot (20)

PDF
Testing automation in agile environment
Perfecto Mobile
 
PPTX
Framework for Web Automation Testing
Taras Lytvyn
 
PPTX
Why Automated Testing Matters To DevOps
dpaulmerrill
 
PPTX
Elements of a Test Framework
SmartBear
 
PDF
Automated vs manual testing
Kanoah
 
PDF
Creating Maintainable Automated Acceptance Tests
Jez Humble
 
PPT
Eclipse UI automation
Srinivas Kantipudi
 
PPTX
Red7 Automating UAT Web Testing
Robert Grupe, CSSLP CISSP PE PMP
 
PPTX
How to Add Test Automation to your Quality Assurance Toolbelt
Brett Tramposh
 
PPTX
Testing Frameworks And Methodologies
Steven Cahill
 
PPTX
Software testing
K Lingaraju
 
PPTX
Software testing and quality assurance
Benjamin Baumann
 
PDF
Quality Assurance Guidelines
Tim Stribos
 
PDF
Career in Software Testing | Skills Required for Software Test Engineer | Edu...
Edureka!
 
PPTX
Test team dynamics, Антон Мужайло
Sigma Software
 
PPTX
Test Automation failure analysis
Prashant Chaudhary
 
PPTX
Functional & Performance Test Automation with CI
Leonard Fingerman
 
PPTX
Sustainable agile testing
mimmozzo_
 
PPTX
Test Automation
Tomas Riha
 
PDF
Testing Design System Changes Across Your Application -- Intuit Use Case -- w...
Applitools
 
Testing automation in agile environment
Perfecto Mobile
 
Framework for Web Automation Testing
Taras Lytvyn
 
Why Automated Testing Matters To DevOps
dpaulmerrill
 
Elements of a Test Framework
SmartBear
 
Automated vs manual testing
Kanoah
 
Creating Maintainable Automated Acceptance Tests
Jez Humble
 
Eclipse UI automation
Srinivas Kantipudi
 
Red7 Automating UAT Web Testing
Robert Grupe, CSSLP CISSP PE PMP
 
How to Add Test Automation to your Quality Assurance Toolbelt
Brett Tramposh
 
Testing Frameworks And Methodologies
Steven Cahill
 
Software testing
K Lingaraju
 
Software testing and quality assurance
Benjamin Baumann
 
Quality Assurance Guidelines
Tim Stribos
 
Career in Software Testing | Skills Required for Software Test Engineer | Edu...
Edureka!
 
Test team dynamics, Антон Мужайло
Sigma Software
 
Test Automation failure analysis
Prashant Chaudhary
 
Functional & Performance Test Automation with CI
Leonard Fingerman
 
Sustainable agile testing
mimmozzo_
 
Test Automation
Tomas Riha
 
Testing Design System Changes Across Your Application -- Intuit Use Case -- w...
Applitools
 

Viewers also liked (12)

PPTX
How to Design a Successful Test Automation Strategy
Impetus Technologies
 
PPT
Automation testing strategy, approach & planning
SivaprasanthRentala1975
 
PPT
Test Automation Framework Designs
Sauce Labs
 
ODP
Automating good coding practices
Kevin Peterson
 
PDF
Best Practises In Test Automation
99tests
 
PPT
Elemente de radiologie pentru rezidenti
Traian Mihaescu
 
PPT
AAA Automated Testing
Francesco Carucci
 
PPTX
Automation test scripting techniques
Zhu Zhong
 
PPTX
Elemente de radiologie toracică pentru rezidenţi
Sasu Cristi
 
PPT
Test Automation Best Practices (with SOA test approach)
Leonard Fingerman
 
PDF
BDD - Writing better scenario
Arnauld Loyer
 
PPT
Test Automation Strategies For Agile
Naresh Jain
 
How to Design a Successful Test Automation Strategy
Impetus Technologies
 
Automation testing strategy, approach & planning
SivaprasanthRentala1975
 
Test Automation Framework Designs
Sauce Labs
 
Automating good coding practices
Kevin Peterson
 
Best Practises In Test Automation
99tests
 
Elemente de radiologie pentru rezidenti
Traian Mihaescu
 
AAA Automated Testing
Francesco Carucci
 
Automation test scripting techniques
Zhu Zhong
 
Elemente de radiologie toracică pentru rezidenţi
Sasu Cristi
 
Test Automation Best Practices (with SOA test approach)
Leonard Fingerman
 
BDD - Writing better scenario
Arnauld Loyer
 
Test Automation Strategies For Agile
Naresh Jain
 
Ad

Similar to Best practices for writing good automated tests (20)

PPTX
An Introduction to unit testing
Steven Casey
 
PPTX
Testing 101
Noam Barkai
 
PPTX
Unit Testing and TDD 2017
Xavi Hidalgo
 
PDF
Test Driven Development Introduction
Nguyen Hai
 
PDF
Introduction to Automated Testing
Lars Thorup
 
PDF
Introduction to-automated-testing
BestBrains
 
PPT
Test Driven
Alex Chaffee
 
PDF
Test and Behaviour Driven Development (TDD/BDD)
Lars Thorup
 
PPTX
Testing
thehoagie
 
PPTX
An Introduction To Software Development - Test Driven Development, Part 1
Blue Elephant Consulting
 
PDF
Writing Tests Effectively
Paul Boocock
 
PDF
What is Unit Testing? - A Complete Guide
flufftailshop
 
PPTX
An Introduction to Developer Testing
Will Green
 
PPTX
Unit testing
PiXeL16
 
PPT
Automated Testing vs Manual Testing
didev
 
PPT
Automated Testing vs Manual Testing
Directi Group
 
PPT
Automated+Testing+Vs+Manual+Testing
gueste1e4db
 
KEY
WTF TDD?
jeremyw
 
PPTX
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
michael.labriola
 
An Introduction to unit testing
Steven Casey
 
Testing 101
Noam Barkai
 
Unit Testing and TDD 2017
Xavi Hidalgo
 
Test Driven Development Introduction
Nguyen Hai
 
Introduction to Automated Testing
Lars Thorup
 
Introduction to-automated-testing
BestBrains
 
Test Driven
Alex Chaffee
 
Test and Behaviour Driven Development (TDD/BDD)
Lars Thorup
 
Testing
thehoagie
 
An Introduction To Software Development - Test Driven Development, Part 1
Blue Elephant Consulting
 
Writing Tests Effectively
Paul Boocock
 
What is Unit Testing? - A Complete Guide
flufftailshop
 
An Introduction to Developer Testing
Will Green
 
Unit testing
PiXeL16
 
Automated Testing vs Manual Testing
didev
 
Automated Testing vs Manual Testing
Directi Group
 
Automated+Testing+Vs+Manual+Testing
gueste1e4db
 
WTF TDD?
jeremyw
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
michael.labriola
 
Ad

Best practices for writing good automated tests

Editor's Notes