Slide 1

Slide 1 text

Testing. You should do it. (and other wise sayings) Jon McCartie September 2011

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

“Don't touch anything that doesn't have [test]coverage. Otherwise, you're not refactoring; you're just changing $#!^.” Hamlet D'Arcy

Slide 4

Slide 4 text

Why Testing? Avoid the “click around” test Deferring bug fixes to users Cost of finding/fixing bugs now vs. later Only write the code that NEEDS to be written Bug fixes - write a failing test first • • • • •

Slide 5

Slide 5 text

Technical Debt We don’t have time to clean-up that function -- we’ll get to it later We need to get this feature out NOW -- let’s fix that in the next release #TODO Not sending local changes to be merged upstream • • • • “If the debt grows large enough, eventually the company will spend more on servicing its debt than it invests in increasing the value of its other assets.” Steve McConnell

Slide 6

Slide 6 text

Why Testing? Requirements Architecture Construction System Test Post-release Requirements Requirements Architecture Architecture Construction Construction System Test System Test Post-release Post-release 0 20 40 60 0 0 20 20 40 40 60 60 Phase of Development Phase of Development Cost (times) Cost (times) Cost to Fix a Defect Steve McConnell, Code Complete

Slide 7

Slide 7 text

Test-Driven Development (TDD)

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Test Driven Development (TDD) Writing tests now is easier than later Over time, you’ll forget what it the function did Writing tests first leads to more-easily testable code Tests can be used as specifications Immediate feedback -- “Am I done yet?” • • • • •

Slide 10

Slide 10 text

Test Driven Development (TDD) What the research team found was that the TDD teams produced code that was 60 to 90 percent better in terms of defect density than non-TDD teams. They also discovered that TDD teams took longer to complete their projects—15 to 35 percent longer. http://research.microsoft.com/en-us/news/features/nagappan-100609.aspx

Slide 11

Slide 11 text

Test Driven Development REFACTOR GREEN RED 1. Write a test that fails 2. Make the code work 3. Eliminate redundancy

Slide 12

Slide 12 text

Test Driven Development Write simplest and smallest amount of code to make the test pass -- prevents over-engineering. Make SURE you get red before you write code. False positives = BAD! • •

Slide 13

Slide 13 text

TDD - Tools Ruby: Rspec, Test::Unit, Minitest Python: unittest PHP: PHPunit Java: JUnit COBOL: ? • • • • •

Slide 14

Slide 14 text

Approaches to Testing White Box / Black Box Testing Unit Testing: verify the functionality of a specific section of code Integration Testing: verify the interaction between integrated components • • •

Slide 15

Slide 15 text

Testing for the Web: Starter Pack Model / Controller / View Model / Controller Model / Integration • • •

Slide 16

Slide 16 text

Models Test everything that could break One test for each of your validations One test for every method in your model • • •

Slide 17

Slide 17 text

Models it "does not save a post without title" do post = Post.new post.save.should be_false end The Test The Implementation class Post < ActiveRecord::Base validates_presence_of :title end

Slide 18

Slide 18 text

Controllers Was the web request successful? Was the user redirected to the right page? Was the user successfully authenticated? Was the correct object stored in the response template? Was the appropriate message displayed to the user in the view? • • • • •

Slide 19

Slide 19 text

Controllers it "is successful" do get :index response.should be_success assigns(:posts).should_not be_nil end The Test The Implementation def index @posts = Post.all end

Slide 20

Slide 20 text

Views Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. •

Slide 21

Slide 21 text

Views assert_select 'ul.navigation' do assert_select 'li.menu_item' end The Test The Implementation

Slide 22

Slide 22 text

Miskelaneaous Routes Mailers Helpers • • •

Slide 23

Slide 23 text

Fake It Don't test against your development environment -- test against a clean test environment Fixtures Factories Stubs Mocks • • • • •

Slide 24

Slide 24 text

Integration Tests Integration tests are used to test the interaction among any number of controllers. They are generally used to test important work flows within your application. • •

Slide 25

Slide 25 text

Integration Tests test "login and browse site" do https! get "/login" assert_response :success post "/login", :username => users(:avs).username, :password => users(:avs).password assert_equal '/welcome', path assert_equal 'Welcome avs!', flash[:notice] https!(false) get "/posts/all" assert_response :success assert assigns(:products) end

Slide 26

Slide 26 text

Acceptance Tests aka “Functional Tests” Black-box tests Automated • • •

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Behavior Driven Development BDD extends TDD by writing easily-readable tests that stakeholders can read/write. • "BDD is a second-generation, outside-in, pull-based, multiple-stakeholder, multiple-scale, high-automation, agile methodology. It describes a cycle of interactions with well-defined outputs, resulting in the delivery of working, tested software that matters." Dan North aka: Test what the user/stakeholder sees and how they interact with the application. •

Slide 29

Slide 29 text

Behavior Driven Development Gherkin syntax

Slide 30

Slide 30 text

Behavior Driven Development 1) Describe behavior in plain text

Slide 31

Slide 31 text

Behavior Driven Development 2) Write a step definition

Slide 32

Slide 32 text

Behavior Driven Development 3) Run and watch it fail

Slide 33

Slide 33 text

Behavior Driven Development 4) Write code to make the step pass

Slide 34

Slide 34 text

Behavior Driven Development 5) Run again and see the step pass

Slide 35

Slide 35 text

Behavior Driven Development 6) Repeat 2-5 until green like a cuke

Slide 36

Slide 36 text

Behavior Driven Development Example Rails feature

Slide 37

Slide 37 text

BDD - Tools Ruby: Cucumber Python: PySpec PHP: Behat Visual Basic: (ohrly?) • • • •

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Questions?