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

PDF
Introduction to Test Automation - Technology and Tools
PDF
Introduction to Test Automation
PDF
10 Benefits of Automated Testing
PPT
Programming skills for test automation
PDF
John Fodeh Adventures in Test Automation - EuroSTAR 2013
PPTX
Introduction to Automation Testing
PPT
Tech talks #1- Unit testing and TDD
PDF
6 Traits of a Successful Test Automation Architecture
Introduction to Test Automation - Technology and Tools
Introduction to Test Automation
10 Benefits of Automated Testing
Programming skills for test automation
John Fodeh Adventures in Test Automation - EuroSTAR 2013
Introduction to Automation Testing
Tech talks #1- Unit testing and TDD
6 Traits of a Successful Test Automation Architecture

What's hot (20)

PDF
Testing automation in agile environment
PPTX
Framework for Web Automation Testing
PPTX
Why Automated Testing Matters To DevOps
PPTX
Elements of a Test Framework
PDF
Automated vs manual testing
PDF
Creating Maintainable Automated Acceptance Tests
PPT
Eclipse UI automation
PPTX
Red7 Automating UAT Web Testing
PPTX
How to Add Test Automation to your Quality Assurance Toolbelt
PPTX
Testing Frameworks And Methodologies
PPTX
Software testing
PPTX
Software testing and quality assurance
PDF
Quality Assurance Guidelines
PDF
Career in Software Testing | Skills Required for Software Test Engineer | Edu...
PPTX
Test team dynamics, Антон Мужайло
PPTX
Test Automation failure analysis
PPTX
Functional & Performance Test Automation with CI
PPTX
Sustainable agile testing
PPTX
Test Automation
PDF
Testing Design System Changes Across Your Application -- Intuit Use Case -- w...
Testing automation in agile environment
Framework for Web Automation Testing
Why Automated Testing Matters To DevOps
Elements of a Test Framework
Automated vs manual testing
Creating Maintainable Automated Acceptance Tests
Eclipse UI automation
Red7 Automating UAT Web Testing
How to Add Test Automation to your Quality Assurance Toolbelt
Testing Frameworks And Methodologies
Software testing
Software testing and quality assurance
Quality Assurance Guidelines
Career in Software Testing | Skills Required for Software Test Engineer | Edu...
Test team dynamics, Антон Мужайло
Test Automation failure analysis
Functional & Performance Test Automation with CI
Sustainable agile testing
Test Automation
Testing Design System Changes Across Your Application -- Intuit Use Case -- w...
Ad

Viewers also liked (12)

PPTX
How to Design a Successful Test Automation Strategy
PPT
Automation testing strategy, approach & planning
PPT
Test Automation Framework Designs
ODP
Automating good coding practices
PDF
Best Practises In Test Automation
PPT
Elemente de radiologie pentru rezidenti
PPT
AAA Automated Testing
PPTX
Automation test scripting techniques
PPTX
Elemente de radiologie toracică pentru rezidenţi
PPT
Test Automation Best Practices (with SOA test approach)
PDF
BDD - Writing better scenario
PPT
Test Automation Strategies For Agile
How to Design a Successful Test Automation Strategy
Automation testing strategy, approach & planning
Test Automation Framework Designs
Automating good coding practices
Best Practises In Test Automation
Elemente de radiologie pentru rezidenti
AAA Automated Testing
Automation test scripting techniques
Elemente de radiologie toracică pentru rezidenţi
Test Automation Best Practices (with SOA test approach)
BDD - Writing better scenario
Test Automation Strategies For Agile
Ad

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

PDF
CNUG TDD June 2014
PPTX
Test-Driven Development
PPTX
Clean code presentation
PPTX
Type mock isolator
PDF
Unit Testing Best Practices
PPTX
Type mock isolator
PPTX
Testing with laravel
PPTX
Writing Better Tests - Applying Clean-Code TDD at 99designs
PPTX
A Brief Introduction to Test-Driven Development
PDF
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
PDF
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
PPTX
Unit Testing and Tools
PDF
Clean tests
PDF
TDD and Related Techniques for Non Developers (2012)
PPTX
Clean Code Talk (draft)
PPTX
Clean code
PPTX
Unit Testing talk
PPTX
How to write test in node.js
PPTX
An Introduction to Unit Testing
PPTX
Eurosport's Kodakademi #2
CNUG TDD June 2014
Test-Driven Development
Clean code presentation
Type mock isolator
Unit Testing Best Practices
Type mock isolator
Testing with laravel
Writing Better Tests - Applying Clean-Code TDD at 99designs
A Brief Introduction to Test-Driven Development
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Unit Testing and Tools
Clean tests
TDD and Related Techniques for Non Developers (2012)
Clean Code Talk (draft)
Clean code
Unit Testing talk
How to write test in node.js
An Introduction to Unit Testing
Eurosport's Kodakademi #2

Best practices for writing good automated tests

Editor's Notes