Slide 1

Slide 1 text

Introduction to Automated Tests Gabriel Sobrinho

Slide 2

Slide 2 text

GABRIEL SOBRINHO gabrielsobrinho.com twitter.com/sobrinho github.com/sobrinho speakerdeck.com/sobrinho

Slide 3

Slide 3 text

We are hiring! [email protected]

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Concepts

Slide 6

Slide 6 text

Software

Slide 7

Slide 7 text

Software is a set of instructions instructing a computer to do specific tasks

Slide 8

Slide 8 text

Bug

Slide 9

Slide 9 text

A software bug is an error in a program that causes it to produce an incorrect result or misbehave unintentionally

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

All of these failures are due to poor design, coding and testing

Slide 16

Slide 16 text

Tests are the answer to avoid these kind of failures

Slide 17

Slide 17 text

How?

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Human Tester

Slide 20

Slide 20 text

Normally done by the QA department, a person who exercises the software

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Automated Tests

Slide 23

Slide 23 text

A software that compares actual outcomes with predicted outcomes

Slide 24

Slide 24 text

Ensure code quality

Slide 25

Slide 25 text

Avoid regressions

Slide 26

Slide 26 text

Reduce maintenance cost

Slide 27

Slide 27 text

Code and behaviour documentation

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Examples

Slide 30

Slide 30 text

def sum(a, b) a + b end

Slide 31

Slide 31 text

if sum(1, 2) == 3 puts 'success' else abort 'failure' end

Slide 32

Slide 32 text

$ ruby test.rb success

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Methodologies

Slide 35

Slide 35 text

Test-Driven Development

Slide 36

Slide 36 text

Write tests before the code itself to get fast feedback and guidance

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Examples

Slide 40

Slide 40 text

if sum_all([1, 2, 3]) == 6 puts 'sucess' else abort 'failure' end

Slide 41

Slide 41 text

$ ruby tdd.rb tdd.rb:1:in `': undefined method `sum_all’ for main:Object (NoMethodError)

Slide 42

Slide 42 text

def sum_all end

Slide 43

Slide 43 text

$ ruby tdd.rb tdd.rb:1:in `sum_all': wrong number of arguments (given 1, expected 0) (ArgumentError) from tdd.rb:4:in `'

Slide 44

Slide 44 text

def sum_all(numbers) end

Slide 45

Slide 45 text

$ ruby tdd.rb failure

Slide 46

Slide 46 text

def sum_all(numbers) total = 0 numbers.each do |number| total += number end total end

Slide 47

Slide 47 text

$ ruby tdd.rb success

Slide 48

Slide 48 text

def sum_all(numbers) total = 0 numbers.each do |number| total += number end total end

Slide 49

Slide 49 text

def sum_all(numbers) numbers.reduce(:+) end

Slide 50

Slide 50 text

$ ruby tdd.rb success

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

Behavior-Driven Development

Slide 54

Slide 54 text

BDD is a subset of TDD that enforces the story telling format to be clear about the expectations

Slide 55

Slide 55 text

TDD focuses on each and every unit test for every function, doesn't matter what it does https://softwareengineering.stackexchange.com/a/224102

Slide 56

Slide 56 text

BDD focuses on software that matters https://softwareengineering.stackexchange.com/a/224102

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

Feature: Calculator As Einstein I want a calculator So I can perform basic operations Scenario: Subtraction Given I have two numbers When I subtract them Then I want the difference

Slide 59

Slide 59 text

Given 'I have two numbers' do @a = 6 @b = 3 end When 'I subtract them' do @result = subtract(@a, @b) end Then 'I want the difference' do assert @result == 3 end

Slide 60

Slide 60 text

Feature: Calculator As Einstein I want a calculator So I can perform basic operations Scenario: Subtraction Given I have two numbers When I subtract them undefined method `subtract' for # (NoMethodError) calc.rb:7:in `/^I\ subtract\ them$/' calc.feature:8:in `When I subtract them' Then I want the difference Failing Scenarios: cucumber calc.feature:6 # Scenario: Subtraction 1 scenario (1 failed) 3 steps (1 failed, 1 skipped, 1 passed) 0m0.021s

Slide 61

Slide 61 text

def subtract end

Slide 62

Slide 62 text

Feature: Calculator As Einstein I want a calculator So I can perform basic operations Scenario: Subtraction Given I have two numbers When I subtract them wrong number of arguments (given 2, expected 0) (ArgumentError) calc.rb:1:in `subtract' calc.rb:10:in `/^I\ subtract\ them$/' calc.feature:8:in `When I subtract them' Then I want the difference Failing Scenarios: cucumber calc.feature:6 # Scenario: Subtraction 1 scenario (1 failed) 3 steps (1 failed, 1 skipped, 1 passed) 0m0.021s

Slide 63

Slide 63 text

def subtract(a, b) end

Slide 64

Slide 64 text

Feature: Calculator As Einstein I want a calculator So I can perform basic operations Scenario: Subtraction Given I have two numbers When I subtract them SolidAssert::AssertionFailedError (SolidAssert::AssertionFailedError) calc.rb:14:in `/^I\ want\ the\ difference$/' calc.feature:9:in `Then I want the difference' Then I want the difference Failing Scenarios: cucumber calc.feature:6 # Scenario: Subtraction 1 scenario (1 failed) 3 steps (1 failed, 1 skipped, 1 passed) 0m0.021s

Slide 65

Slide 65 text

def subtract(a, b) a - b end

Slide 66

Slide 66 text

Feature: Calculator As Einstein I want a calculator So I can perform basic operations Scenario: Subtraction Given I have two numbers When I subtract them Then I want the difference 1 scenario (1 passed) 3 steps (3 passed) 0m0.019s

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

Types

Slide 70

Slide 70 text

Unit Testing

Slide 71

Slide 71 text

Tests about the smallest piece of the software isolated from the real world

Slide 72

Slide 72 text

Integrated Testing

Slide 73

Slide 73 text

Tests using connected pieces of the software like databases and APIs

Slide 74

Slide 74 text

describe ZipCode do it 'returns the street information' do # Triggers a HTTP API call zip_code = ZipCode.find('12345') expect(zip_code.number).to eq '12345' expect(zip_code.street).to eq 'Some Avenue' end end

Slide 75

Slide 75 text

Acceptance Testing

Slide 76

Slide 76 text

Tests if the software works as the customer or stakeholder expects

Slide 77

Slide 77 text

You may open the browser and click on a button and expect something to happen

Slide 78

Slide 78 text

feature 'Buy' do scenario 'Buy a product using credit card' do visit '/products/1' click_on 'Buy' expect(page).to have_content 'Order made' expect(credit_card).to have_charged 14.99 end end

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

Artifacts

Slide 81

Slide 81 text

Code to Test Ratio How many lines of production code there are against lines of test code

Slide 82

Slide 82 text

Code Coverage Percentage of the code that was exercised during the test suite

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

Recommendations

Slide 85

Slide 85 text

Write unit tests, integrated tests and acceptance tests

Slide 86

Slide 86 text

You can write tests before or after writing the code but write tests

Slide 87

Slide 87 text

You can use BDD, TDD or whatever as long you have a methodology

Slide 88

Slide 88 text

Use TDD for unit and integrated tests and BDD for acceptance tests

Slide 89

Slide 89 text

Write tests that asserts the collateral effect and not the implementation

Slide 90

Slide 90 text

Be Happy!

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

Thanks!

Slide 93

Slide 93 text

Questions?

Slide 94

Slide 94 text

Thanks!

Slide 95

Slide 95 text

We are hiring! [email protected]

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

• http://www.devmedia.com.br/artigo-engenharia-de-software-3-a-importancia-dos-testes- automatizados/9532 • http://www.eduardopires.net.br/2012/06/ddd-tdd-bdd/ • http://tdd.caelum.com.br • https://cbabhusal.wordpress.com/2016/02/26/tdd-why-red-green-refactor-is-important-in-tdd/ • https://ciclosw.wordpress.com/2014/09/04/diferenca-entre-tdd-e-bdd/ • http://blog.locaweb.com.br/artigos/metodologias-ageis/diferenca-entre-bdd-tdd/ • https://www.infoq.com/news/2015/02/bdd-ddd • http://www.princiweb.com.br/blog/programacao/tdd/tdd-ddd-e-bdd-praticas-de- desenvolvimento.html • http://www.agileandart.com/2010/07/16/ddd-introducao-a-domain-driven-design/ References