Slide 1

Slide 1 text

Testing Ruby Radoslav Stankov 04/12/2014

Slide 2

Slide 2 text

Кой съм аз? @rstankov http://rstankov.com http://github.com/rstankov

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

• 2002 - първия ми “професионален проект” (flash) • 2008 - първи PHP unit test • 2009 - първи Ruby on Rails проект • 2009 - първи Ruby unit test • 2010 - официално Ruby developer • 2010 - първи selenium acceptance test • 2014 - все още пиша главно на Ruby … :P

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

I have a plan!

Slide 7

Slide 7 text

• За какво са ни нужни тестове? • Какво е автоматичен тест? • Какви видове тестове има? • Какво е добър тест? • Как да пишем добри тестове? • Какво трябва да тестваме? • Какво са “Mocks”? • Какво е “RSpec”? • Какво е “Test Driven Development”? • Какво е “Fragile tests”?

Slide 8

Slide 8 text

За какво са ни нужни тестове?

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

puts "1 + 1 = #{Calculation.new("1 + 1").value}" puts "2 - 1 = #{Calculation.new("2 - 1").value}" puts "3 * 2 = #{Calculation.new("3 * 2").value}"

Slide 17

Slide 17 text

class Calculation # ... def to_s "#{expression} = #{value}" end end

Slide 18

Slide 18 text

puts Calculation.new("1 + 1") puts Calculation.new("2 - 1") puts Calculation.new("3 * 2")

Slide 19

Slide 19 text

raise "INVALID" unless Calculation.new("1 + 1").value == 2 raise "INVALID" unless Calculation.new("2 - 1").value == 1 raise "INVALID" unless Calculation.new("3 * 2").value == 6 puts 'Everything okay :)'

Slide 20

Slide 20 text

def assert_calculation(expression, expected) calculation = Calculation.new(expression) if calculation.value != expected raise "#{calculation}, but expected #{expected}" end end

Slide 21

Slide 21 text

assert_calculation '1 + 1', 2 assert_calculation '2 - 1', 1 assert_calculation '3 * 2', 6 puts 'Everything okay :)'

Slide 22

Slide 22 text

• code task 1 • check task 1
 • code task 2 • check task 2 • check task 1
 • code task 3 • check task 3 • check task 2 • check task 1
 • code task 4 • check task 4 • check task 3 • check task 2 • check task 1 • code task 1 • code task 1 test • run tests
 • code task 2 • code task 2 test • run tests
 • code task 3 • code task 3 test • run tests
 • code task 4 • code task 4 test • run tests Manual Automatic

Slide 23

Slide 23 text

• code task 1 • check task 1
 • code task 2 • check task 2 • check task 1
 • code task 3 • check task 3 • check task 2 • check task 1
 • code task 4 • check task 4 • check task 3 • check task 2 • check task 1 • code task 5 • check task 4 • check task 4 • check task 3 • check task 2 • code task 1 • code task 1 test • run tests
 • code task 2 • code task 2 test • run tests
 • code task 3 • code task 3 test • run tests
 • code task 4 • code task 4 test • run tests • code task 5 • code task 5 test • run tests Manual Automatic

Slide 24

Slide 24 text

• Safety net • Reduces bugs in new or existing features • Forces you to understand the features and communication between components • Reduces the cost for new features • Making code testable decreases complexity and modularity

Slide 25

Slide 25 text

Какво е автоматичен тест?

Slide 26

Slide 26 text

class CalculationTest < Minitest::Test def test_add assert_equal 2, Calculation.new('1 + 1').value end def test_subtract assert_equal 1, Calculation.new('2 - 1').value end def test_multiply assert_equal 6, Calculation.new('3 * 2').value end end

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

class ArrayTest < Minitest::Test def setup @array = [] end def test_empty assert @array.empty? end def test_push @array << 1 assert_equal [1], @array end end

Slide 29

Slide 29 text

class ArrayTest < Minitest::Test def setup @array = [] end def test_empty assert @array.empty? end def test_push @array << 1 assert_equal [1], @array end end Test Suite

Slide 30

Slide 30 text

class ArrayTest < Minitest::Test def setup @array = [] end def test_empty assert @array.empty? end def test_push @array << 1 assert_equal [1], @array end end

Slide 31

Slide 31 text

class ArrayTest < Minitest::Test def setup @array = [] end def test_empty assert @array.empty? end def test_push @array << 1 assert_equal [1], @array end end Test Setup

Slide 32

Slide 32 text

class ArrayTest < Minitest::Test def setup @array = [] end def test_empty assert @array.empty? end def test_push @array << 1 assert_equal [1], @array end end

Slide 33

Slide 33 text

class ArrayTest < Minitest::Test def setup @array = [] end def test_empty assert @array.empty? end def test_push @array << 1 assert_equal [1], @array end end Test Case

Slide 34

Slide 34 text

class ArrayTest < Minitest::Test def setup @array = [] end def test_empty assert @array.empty? end def test_push @array << 1 assert_equal [1], @array end end

Slide 35

Slide 35 text

class ArrayTest < Minitest::Test def setup @array = [] end def test_empty assert @array.empty? end def test_push @array << 1 assert_equal [1], @array end end Assertion

Slide 36

Slide 36 text

class ArrayTest < Minitest::Test def setup @array = [] end def test_empty assert @array.empty? end def test_push @array << 1 assert_equal [1], @array end end

Slide 37

Slide 37 text

Kent Beck • based on SUnit (Smalltalk) • released around 1998 xUnit

Slide 38

Slide 38 text

Какви видове тестове има?

Slide 39

Slide 39 text

System under test (SUT) Tests Unit test Integration test Acceptance test SUD

Slide 40

Slide 40 text

Unit Test Integration Test Acceptance Test Tests one object Tests one “component” Tests the whole stack Isolated with mocks Some times uses stubs Uses the UI
 Expresses domain terms Fast Slow Very slow Helps for good design Helps for verification Helps for verification

Slide 41

Slide 41 text

Какво е добър тест?

Slide 42

Slide 42 text

“Не трябва да губя енергия за пускане на тестове ” Лесни за пускане

Slide 43

Slide 43 text

“Пускането на тестове не трябва да ме разсейва от проблема, който решавам ” Бързи

Slide 44

Slide 44 text

“Не трябва да губя излишна енегрия зада разбера даден тест” Прости

Slide 45

Slide 45 text

“Трябва да имам чувството, че ако тестовете ми минават, значи кода ми работи” Надежни

Slide 46

Slide 46 text

“Не трябва да се налага да се пренаписват при всяка промяна ” Гъвкави

Slide 47

Slide 47 text

“Когато даден тест се счупи, трябва да знам точно кой тест се е счупил и защо” Локализирани

Slide 48

Slide 48 text

Как да пишем добри тестове?

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

4 Phase testing

Slide 51

Slide 51 text

4 Phase testing Setup

Slide 52

Slide 52 text

4 Phase testing Setup Action

Slide 53

Slide 53 text

4 Phase testing Setup Action Assertion

Slide 54

Slide 54 text

4 Phase testing Setup Action Assertion Teardown

Slide 55

Slide 55 text

4 Phase testing Setup Action Assertion Teardown

Slide 56

Slide 56 text

class OrderTest < Minitest::Test def test_proccess order = Order.new assert_equal Date.today, order.initiated_at.to_date assert_nil order.user assert order.empty? product = Product.new order << product assert_equal 1, order.size assert !order.valid? user = User.new order.user = user assert_equal user, order.user assert !order.valid?

Slide 57

Slide 57 text

assert !order.valid? user = User.new order.user = user assert_equal user, order.user assert !order.valid? def Order.can_be_processed_today? true end assert order.valid? order.cancel assert_equal :canceled, order.state end end

Slide 58

Slide 58 text

class OrderTest < Minitest::Test def test_proccess order = Order.new assert_equal Date.today, order.initiated_at.to_date assert_nil order.user assert order.empty? product = Product.new order << product assert_equal 1, order.size assert !order.valid? user = User.new order.user = user assert_equal user, order.user assert !order.valid? def Order.can_be_processed_today? true end assert order.valid? order.cancel assert_equal :canceled, order.state end end

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

class OrderTest < Minitest::Test def setup @order = Order.new end def around(&block) Order.stub :can_be_processed_today?, true, &block end def test_initiated_at assert_equal Date.today, @order.initiated_at.to_date end def test_add_product product = Product.new @order << product assert_equal [product], @order.products assert_equal 1, @order.size assert [email protected]? end

Slide 63

Slide 63 text

def test_add_product product = Product.new @order << product assert_equal [product], @order.products assert_equal 1, @order.size assert [email protected]? end def test_valid @order.user = User.new @order << Product.new assert @order.valid? end def test_valid_when_no_user @order << Product.new assert [email protected]? end

Slide 64

Slide 64 text

def test_valid_when_no_user @order << Product.new assert [email protected]? end def test_valid_when_no_products @order.user = User.new assert [email protected]? end def test_cancel @order.cancel assert_equal :canceled, @order.state end end

Slide 65

Slide 65 text

class OrderTest < Minitest::Test def setup @order = Order.new end def around(&block) Order.stub :can_be_processed_today?, true, &block end def test_initiated_at assert_equal Date.today, @order.initiated_at.to_date end def test_add_product product = Product.new @order << product assert_equal [product], @order.products assert_equal 1, @order.size assert [email protected]? end def test_valid @order.user = User.new @order << Product.new assert @order.valid? end def test_valid_when_no_user @order << Product.new assert [email protected]? end def test_valid_when_no_products @order.user = User.new assert [email protected]? end def test_cancel @order.cancel assert_equal :canceled, @order.state end end

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

Какво трябва да тестваме?

Slide 68

Slide 68 text

Query method car.top_speed user.name array.empty?

Slide 69

Slide 69 text

Command method car.drive_to(supermarket) user.destroy! array << “1”

Slide 70

Slide 70 text

Object Incomming Outgoing Send to self

Slide 71

Slide 71 text

Incomming Send to self Outgoing object.method() def method private_method() end def other_method(object) object.method() end

Slide 72

Slide 72 text

Query Command ▶︎ Incomming Check result directly Check side effects via public api Send to self (private) ✕ No tests ✕ No tests ◀ Outgoing ✕ 
 No tests Check with expect to send (mock)

Slide 73

Slide 73 text

▶︎ Incomming query class ArrayTest < Minitest::Test def test_empty array = [1] assert !array.empty? end 
 end

Slide 74

Slide 74 text

▶︎ Incomming command class ArrayTest < Minitest::Test def test_empty array = [1] assert !array.empty? end end

Slide 75

Slide 75 text

◀ Outgoing command class MyObjectTest < Minitest::Test def test_my_method array = Minitest::Mock.new array.expect :clear, :returned custom = MyObject.new custom.clear array array.verify end end

Slide 76

Slide 76 text

RSpec

Slide 77

Slide 77 text

Dave Astels David Chelimsky
 release as 0.0.10 - July 2, 2008

Slide 78

Slide 78 text

class ArrayTest < Minitest::Test def setup @array = [] end def test_empty assert @array.empty? end def test_push @array << 1 assert_equal [1], @array end end

Slide 79

Slide 79 text

describe Array do before do @array = [] end it "can tell when it is empty" do expect(@array).to be_empty end it "can filled with items" do @array << 1 expect(@array).to eq [1] end end

Slide 80

Slide 80 text

describe Array do before do @array = [] end it "can tell when it is empty" do expect(@array).to be_empty end it "can filled with items" do @array << 1 expect(@array).to eq [1] end end Example Group

Slide 81

Slide 81 text

describe Array do before do @array = [] end it "can tell when it is empty" do expect(@array).to be_empty end it "can filled with items" do @array << 1 expect(@array).to eq [1] end end

Slide 82

Slide 82 text

describe Array do before do @array = [] end it "can tell when it is empty" do expect(@array).to be_empty end it "can filled with items" do @array << 1 expect(@array).to eq [1] end end Setup

Slide 83

Slide 83 text

describe Array do before do @array = [] end it "can tell when it is empty" do expect(@array).to be_empty end it "can filled with items" do @array << 1 expect(@array).to eq [1] end end

Slide 84

Slide 84 text

describe Array do before do @array = [] end it "can tell when it is empty" do expect(@array).to be_empty end it "can filled with items" do @array << 1 expect(@array).to eq [1] end end Example

Slide 85

Slide 85 text

describe Array do before do @array = [] end it "can tell when it is empty" do expect(@array).to be_empty end it "can filled with items" do @array << 1 expect(@array).to eq [1] end end

Slide 86

Slide 86 text

describe Array do before do @array = [] end it "can tell when it is empty" do expect(@array).to be_empty end it "can filled with items" do @array << 1 expect(@array).to eq [1] end end Expectation

Slide 87

Slide 87 text

describe Array do before do @array = [] end it "can tell when it is empty" do expect(@array).to be_empty end it "can filled with items" do @array << 1 expect(@array).to eq [1] end end

Slide 88

Slide 88 text

describe Array do let(:array) { [] } it "can tell when it is empty" do expect(array).to be_empty end it "can filled with items" do array << 1 expect(array).to eq [1] end end

Slide 89

Slide 89 text

describe Array do let(:array) { [] } it "can tell when it is empty" do expect(array).to be_empty end it "can filled with items" do array << 1 expect(array).to eq [1] end end

Slide 90

Slide 90 text

describe Array do describe "#empty?" do it "returns true when empty" do expect(array).to be_empty end it "returns false when not empty" do array = [1] expect(array).to be_empty end end end

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

Mocks

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

Fake Used as a simpler implementation, e.g. using an in-memory database in the tests instead of doing real database access.

Slide 95

Slide 95 text

Dummy Used when a parameter is needed for the tested method but without actually needing to use the parameter.

Slide 96

Slide 96 text

Stub Test stubs are objects with pre-programmed behaviour.

Slide 97

Slide 97 text

Spy Records arguments, return value, the value of this and exception thrown (if any) for all its calls.

Slide 98

Slide 98 text

Mock Mocks are fake methods (like spies) with pre-programmed behaviour (like stubs) as well as pre-programmed expectations. A mock will fail your test if it is not used as expected.

Slide 99

Slide 99 text

Test double Stub Spy Mock Fake Dummy

Slide 100

Slide 100 text

describe PostsController do stub_rendering stub_current_user let(:post) { double 'Post' } describe "GET 'index'" do it "responds with posts of a given category" do allow(Post).to receive(:for_category).with(‘1’).and_return [post] get :index, category_id: '1' expect(controller).to respond_with [post] end end describe "POST 'create'" do it "creates new post" do allow(Post).to receive(:create).and_return post post :create, post: {title: 'test'} expect(Post).to have_received(:create).with(user: current_user, title: 'tes end end end

Slide 101

Slide 101 text

Test Driven Development

Slide 102

Slide 102 text

Automated Testing Test Driven Development

Slide 103

Slide 103 text

Test Driven Development

Slide 104

Slide 104 text

Test Driven Development 1 Добавя се тест ... за несъществуващ код

Slide 105

Slide 105 text

Test Driven Development 1 Добавя се тест ... за несъществуващ код 2 Пише се код ... колкото само тестът да мине

Slide 106

Slide 106 text

Test Driven Development 1 Добавя се тест ... за несъществуващ код 3 Правят се подобрения ... подобрява се качеството на кода 2 Пише се код ... колкото само тестът да мине

Slide 107

Slide 107 text

Test Driven Development 1 Добавя се тест ... за несъществуващ код 3 Правят се подобрения ... подобрява се качеството на кода 2 Пише се код ... колкото само тестът да мине

Slide 108

Slide 108 text

describe Calculation do it "can sum numbers" do calculation = Calculation.new('1 + 1') expect(calculation.value).to eq 2 end end

Slide 109

Slide 109 text

uninitialized constant Calculation (NameError)

Slide 110

Slide 110 text

class Calculation def initialize(expression) end def value end end

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

class Calculation def initialize(expression) @expression = expression end def value a, b = @expression.split '+' a.to_i + b.to_i end end

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

describe Calculation do # . . . it "can subtract numbers" do calculation = Calculation.new('2 - 1') expect(calculation.value).to eq 1 end end

Slide 115

Slide 115 text

No content

Slide 116

Slide 116 text

class Calculation def initialize(expression) @expression = expression end def value a, operation, b = @expression.split ' ' if operation == '+' a.to_i + b.to_i elsif operation == '-' a.to_i - b.to_i end end end

Slide 117

Slide 117 text

No content

Slide 118

Slide 118 text

describe Calculation do # . . . it "can multiply numbers" do calculation = Calculation.new('3 * 2') expect(calculation.value).to eq 6 end end

Slide 119

Slide 119 text

No content

Slide 120

Slide 120 text

class Calculation def initialize(expression) @expression = expression end def value a, operation, b = @expression.split ' ' if operation == '+' a.to_i + b.to_i elsif operation == '-' a.to_i - b.to_i elsif operation == '*' a.to_i * b.to_i end end end

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

describe Calculation do # . . . it "raises an error on unknown operation" do calculation = Calculation.new('3 ~ 2') expect { calculation.value }.to raise_error end end

Slide 123

Slide 123 text

class Calculation def initialize(expression) @expression = expression end def value a, operation, b = @expression.split ' ' if operation == '+' a.to_i + b.to_i elsif operation == '-' a.to_i - b.to_i elsif operation == '*' a.to_i * b.to_i else raise "Can't parse expression - #{@expression}" end end end

Slide 124

Slide 124 text

class Calculation def initialize(expression) @expression = expression end OPERATIONS = { '+' => ->(a, b) { a + b }, '-' => ->(a, b) { a - b }, '*' => ->(a, b) { a * b } } def value a, operand, b = @expression.split ' ' operation = OPERATIONS[operand] raise "Can't parse expression - #{@expression}" unless operation operation.call a.to_i, b.to_i end end

Slide 125

Slide 125 text

describe Calculation do # . . . it "can divide numbers" do calculation = Calculation.new('6 / 3') expect(calculation.value).to eq 2 end end

Slide 126

Slide 126 text

No content

Slide 127

Slide 127 text

class Calculation def initialize(expression) @expression = expression end OPERATIONS = { '+' => ->(a, b) { a + b }, '-' => ->(a, b) { a - b }, '*' => ->(a, b) { a * b }, '/' => ->(a, b) { a / b } } def value a, operand, b = @expression.split ' ' operation = OPERATIONS[operand] raise "Can't parse expression - #{@expression}" unless operation operation.call a.to_f, b.to_f end end

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

class Calculation OPERATIONS = { '+' => ->(a, b) { a + b }, '-' => ->(a, b) { a - b }, '*' => ->(a, b) { a * b }, '/' => ->(a, b) { a / b } } attr_reader :expression def initialize(expression) @expression = expression end def value @value ||= calculate_value end def to_s “#{expression} = #{value}" end private def calculate_value left, operand, right = expression.split ' ' operation = OPERATIONS[operand] raise "Can't parse expression - #{@expression}" unless operation operation.call left.to_f, right.to_f end end

Slide 130

Slide 130 text

Fragile Tests

Slide 131

Slide 131 text

Bad fixture (fragile test)

Slide 132

Slide 132 text

class OrderTest < Minitest::Test def test_expired order = Order.new( user: User.new, product: [Product.new] town: 'Sofia' initiated_at: 5.days.ago ) assert order.expired? end end ✗

Slide 133

Slide 133 text

class OrderTest < Minitest::Test def test_expired order = create_order initiated_at: 5.days.ago assert order.expired? end end ✔

Slide 134

Slide 134 text

Behaviour changes (fragile test)

Slide 135

Slide 135 text

class OrderTest < Minitest::Test def setup @order = Order.new end def test_valid @order.user = User.new @order << Product.new assert @order.valid? end def test_valid_when_no_user @order << Product.new assert [email protected]? end def test_valid_when_no_products @order.user = User.new assert [email protected]? end end ✗

Slide 136

Slide 136 text

class OrderTest < Minitest::Test def setup @order = Order.new( user: User.new, products: [Product.new] ) end def test_valid assert @order.valid? end def test_valid_when_no_user @order.user = nil assert [email protected]? end def test_valid_when_no_products @order.products = [] assert [email protected]? end end ✔

Slide 137

Slide 137 text

Indirection (fragile test)

Slide 138

Slide 138 text

describe "Task" do describe ".complete_all" do it "completes a task by text mask" do task = create_task text: 'Create slides' other = create_task text: 'Create examples' Task.complete_all query: 'slides' expect(Task.ongoing).to eq [task] end end end ✗

Slide 139

Slide 139 text

describe "Task" do describe ".complete_all" do it "completes tasks who match query" do task = create_task text: 'Create slides' Task.complete_all query: 'slides' expect { task.reload }.to change { task.completed? }.to true end it "doesn't complete tasks who don't match query" do task = create_task text: 'Create examples' Task.complete_all query: 'slides' expect { task.reload }.not_to change { task.completed? } end end end ✔

Slide 140

Slide 140 text

describe "Task" do # . . . describe ".ongoing" do it "selects ongoing tasks" do ongoing = create_task state: :ongoing completed = create_task state: :completed expect(Task.ongoing).to eq ongoing end end end ✔

Slide 141

Slide 141 text

Coupling (fragile test)

Slide 142

Slide 142 text

describe Wallet do describe "#buy" do amount = true gateway = double 'Gateway' processor = double 'Processor' allow(gateway).to receive(:active?).and_return true allow(gateway).to receive(:processor).and_return processor allow(processor).to receive(:enough_money?).and_return true allow(processor).to receive(:process) Wallet.new(gateway).buy amount: 5 expect(processor).to have_received(:process).with(5) end end ✗

Slide 143

Slide 143 text

class Wallet def initialize(gateway) @gateway = gateway end def buy(amount: amount) return false if @gateway.active? processor = @gateway.processor return false if processor.enough_money?(amount) processor.process amount end end

Slide 144

Slide 144 text

describe Wallet do describe "#buy" do amount = true processor = double 'Processor' allow(gateway).to receive(:process) Wallet.new(gateway).buy amount: 5 expect(processor).to have_received(:process).with(5) end end ✔

Slide 145

Slide 145 text

class Wallet def initialize(gateway) @gateway = gateway end def buy(amount: amount) @gateway.process amount end end

Slide 146

Slide 146 text

Tools

Slide 147

Slide 147 text

• Rspec • FactoryGirl • Timecop • DatabaseCleaner • Capybara

Slide 148

Slide 148 text

Книги

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

No content

Slide 151

Slide 151 text

Обобщение

Slide 152

Slide 152 text

• За какво са ни нужни тестове? • Какво е автоматичен тест? • Какви видове тестове има? • Какво е добър тест? • Как да пишем добри тестове? • Какво трябва да тестваме? • Какво са “Mocks”? • Какво е “RSpec”? • Какво е “Test Driven Development”? • Какво е “Fragile tests”?

Slide 153

Slide 153 text

https://speakerdeck.com/rstankov/testing-ruby

Slide 154

Slide 154 text

https://github.com/rstankov/talks-code

Slide 155

Slide 155 text

No content

Slide 156

Slide 156 text

No content

Slide 157

Slide 157 text

@rstankov Благодаря за вниманието :)

Slide 158

Slide 158 text

Въпроси?