getting the most out of rails tests

34
Getting the most out of Rails tests

Upload: justin-weiss

Post on 04-Dec-2014

174 views

Category:

Software


2 download

DESCRIPTION

The different kinds of tests in Rails each have their advantages and disadvantages. But if you can find the right balance between these different types of tests in your app, you can have better test performance, and better test coverage. It'll be easier to improve your test performance later on. And you can use your tests to help you beat procrastination and get started building new features more quickly.

TRANSCRIPT

Page 1: Getting the most out of rails tests

Getting the mostout of Rails tests

Page 2: Getting the most out of rails tests

Balanced tests

Page 3: Getting the most out of rails tests

Integration testsand Unit tests

Page 4: Getting the most out of rails tests

My story

Page 5: Getting the most out of rails tests

Lots of unitsno integration

Page 6: Getting the most out of rails tests

Lots of unitsno integrationthen...

Lots of integrationLots of units

Page 7: Getting the most out of rails tests

Lots of unitsno integrationthen...

Lots of integrationLots of unitsthen...

?

Page 8: Getting the most out of rails tests
Page 9: Getting the most out of rails tests

The testing pyramid

Page 10: Getting the most out of rails tests

» Use integration tests to test-drive your

features.

» Use unit tests to test specific objects,

models, modules, etc.

» Have many more unit tests than integration

tests.

Page 11: Getting the most out of rails tests

Why balanced tests?

Page 12: Getting the most out of rails tests

Faster test runs

Page 13: Getting the most out of rails tests

(http://xkcd.com/303/, slightly edited)

Page 14: Getting the most out of rails tests

Easier to improveperformance later

Page 15: Getting the most out of rails tests

Easier to test-driveentire features

Page 16: Getting the most out of rails tests

Less brittle tests

Page 17: Getting the most out of rails tests

Better code coverage

Page 18: Getting the most out of rails tests

A simpledevelopment process

Page 19: Getting the most out of rails tests

Start with a sketch

Page 20: Getting the most out of rails tests
Page 21: Getting the most out of rails tests

Write a failingintegration test

Page 22: Getting the most out of rails tests

require 'test_helper'

class CreateBugsTest < CapybaraTestCase test "can create a new bug" do visit bugs_path click_link "File new bug" assert_equal current_path, new_bug_path

fill_in "Title", with: "My new bug" fill_in "Description", with: "Short description." click_button "File bug" # ...and so on... endend

Page 23: Getting the most out of rails tests

Our goalGet the first part of the test to pass

Page 24: Getting the most out of rails tests

Write a failingcontroller test

Page 25: Getting the most out of rails tests

test "should create bug" do assert_difference("Bug.count", 1) do post(:create, bug: { title: "My new bug", description: "Short description." }) end assert_redirected_to bug_path(assigns(:bug))end

Page 26: Getting the most out of rails tests

Write some failing

unit tests

Page 27: Getting the most out of rails tests

test "can create a bug" do bug = Bug.new(title: "My new bug", description: "This bug was just filed by a test.") assert bug.save, "Bug couldn't be saved: #{bug.errors.full_messages}"end

test "should not create a bug without a description" do skipend

test "should not create a bug without a title" do skipend

test "should not create a bug with too long of a title" do skipend

# ... and so on ...

Page 28: Getting the most out of rails tests

test "can create a bug" do bug = Bug.new(title: "My new bug", description: "This bug was just filed by a test.") assert bug.save, "Bug couldn't be saved: #{bug.errors.full_messages}"end

# should not create a bug without a description# should not create a bug without a title# should not create a bug with too long of a title

# ... and so on ...

Page 29: Getting the most out of rails tests

Make your units green,Make your controllers green

Page 30: Getting the most out of rails tests

Repeat!

Page 31: Getting the most out of rails tests

So, once again:1.Draw a sketch

2.Translate that sketch into a failing integration test

3.Get the first part of that test to pass3a. Write a failing controller test3b. Write some failing unit tests3c. Get the tests to pass3d. Repeat!

Page 32: Getting the most out of rails tests

Aim for agood balance

Page 33: Getting the most out of rails tests

Justin Weisshttp://[email protected]@justinweiss

Page 34: Getting the most out of rails tests

http://www.justinweiss.com/book