Slide 1

Slide 1 text

Introduction to Automated Tests Gabriel Sobrinho

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

hite.com.br

Slide 4

Slide 4 text

Concepts

Slide 5

Slide 5 text

TDD BDD Unit Tests Integrated Tests Acceptance Tests Coverage Test Ratio Human Tester Test Automation

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Human Tester

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Automated Tests

Slide 12

Slide 12 text

Automated Tests In software testing, a software that compares actual outcomes with predicted outcomes

Slide 13

Slide 13 text

Automated Tests • Ensure code quality • Avoid regressions • Reduce maintenance cost • Code and behavior documentation

Slide 14

Slide 14 text

Automated Tests def sum(a, b) a + b end

Slide 15

Slide 15 text

Automated Tests if sum(1, 2) == 3 puts 'okay' else puts 'error' end

Slide 16

Slide 16 text

Automated Tests $ ruby calc.rb okay

Slide 17

Slide 17 text

Automated Tests if sum(0.1, 0.2) == 0.3 puts 'okay' else puts 'error' end

Slide 18

Slide 18 text

Automated Tests $ ruby calc.rb error

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Test-Driven Development

Slide 21

Slide 21 text

Test-Driven Development Write tests before the code itself to get fast feedback

Slide 22

Slide 22 text

Test-Driven Development Red-Green-Refactor Cycle

Slide 23

Slide 23 text

Test-Driven Development Recommended by XP community

Slide 24

Slide 24 text

https://bitbucket.org/spooning/

Slide 25

Slide 25 text

Test-Driven Development if sum_all([1, 2, 3]) == 6 puts 'okay' else puts 'error' end

Slide 26

Slide 26 text

Test-Driven Development $ ruby calc.rb calc.rb:1:in `': undefined method `sum_all’ for main:Object (NoMethodError)

Slide 27

Slide 27 text

Test-Driven Development def sum_all end

Slide 28

Slide 28 text

Test-Driven Development $ ruby calc.rb calc.rb:1:in `sum_all’: wrong number of arguments (given 1, expected 0) (ArgumentError) from calc.rb:4:in `'

Slide 29

Slide 29 text

Test-Driven Development def sum_all(numbers) end

Slide 30

Slide 30 text

Test-Driven Development $ ruby calc.rb error

Slide 31

Slide 31 text

Test-Driven Development def sum_all(numbers) total = 0 numbers.each do |number| total += number end total end

Slide 32

Slide 32 text

Test-Driven Development $ ruby calc.rb okay

Slide 33

Slide 33 text

Test-Driven Development def sum_all(numbers) total = 0 numbers.each do |number| total += number end total end

Slide 34

Slide 34 text

Test-Driven Development def sum_all(numbers) numbers.reduce(:+) end

Slide 35

Slide 35 text

Test-Driven Development $ ruby calc.rb okay

Slide 36

Slide 36 text

Test-Driven Development Red-Green-Refactor Cycle

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Behavior-Driven Development

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

Behavior-Driven Development BDD focuses on software that matters https://softwareengineering.stackexchange.com/a/224102

Slide 42

Slide 42 text

Behavior-Driven Development class TestCalculator < Test::Unit::TestCase def test_sum_all assert sum_all([1, 2, 3]) == 6 end end

Slide 43

Slide 43 text

Behavior-Driven Development describe '#sum_all' do it 'sums the given numbers' do expect(sum_all([1, 2, 3])).to eq 6 end end

Slide 44

Slide 44 text

Behavior-Driven Development Failures: 1) #sum_all sums the given numbers Failure/Error: expect(sum_all([1, 2, 3])).to eq 6 NoMethodError: undefined method `sum_all’ for # # calc.rb:5:in `block (2 levels) in ' Finished in 0.00052 seconds (files took 0.09426 seconds to load) 1 example, 1 failure

Slide 45

Slide 45 text

Behavior-Driven Development def sum_all(numbers) total = 0 numbers.each do |number| total += number end total end

Slide 46

Slide 46 text

Behavior-Driven Development $ rspec calc.rb . Finished in 0.00207 seconds (files took 0.09914 seconds to load) 1 example, 0 failures

Slide 47

Slide 47 text

Behavior-Driven Development def sum_all(numbers) numbers.reduce(:+) end

Slide 48

Slide 48 text

Behavior-Driven Development $ rspec calc.rb . Finished in 0.00085 seconds (files took 0.09119 seconds to load) 1 example, 0 failures

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

Unit Testing

Slide 51

Slide 51 text

Unit Testing Tests about the smallest piece of the software

Slide 52

Slide 52 text

Unit Testing - TDD class TestCalculator < Test::Unit::TestCase def test_sum_all assert sum_all([1, 2, 3]) == 6 end end

Slide 53

Slide 53 text

Unit Testing - BDD describe '#sum_all' do it 'sums the given numbers' do expect(sum_all([1, 2, 3])).to eq 6 end end

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

Integrated Testing

Slide 56

Slide 56 text

Integrated Testing Tests the connected pieces of the software like databases or APIs

Slide 57

Slide 57 text

Integrated Testing describe ZipCode do it 'returns the street information' do # Hits an API call zip_code = ZipCode.find('21510-140') expect(zip_code.number).to eq '21510-140' expect(zip_code.street).to eq 'Some Avenue' end end

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

Acceptance Testing

Slide 60

Slide 60 text

Acceptance Testing Tests if the software really works as the final customer or stakeholder expects it to work

Slide 61

Slide 61 text

Acceptance Testing Feature: Buy a product Scenario: Using a discount code Given I have a discount code When I buy a product using that code Then the total must include that discount

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

Artefacts

Slide 64

Slide 64 text

Ratio Artefact about how many line of tests there are for how many lines of code you have

Slide 65

Slide 65 text

Coverage Artefact about if the line of code was run or wasn’t run during the test suite

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

FAQ

Slide 68

Slide 68 text

What to test? Everything, from isolated code, to integrated pieces and to the entire system connected

Slide 69

Slide 69 text

How to test? By the collacteral effects that the application causes, not by the implementation itself

Slide 70

Slide 70 text

Write the tests before or after? Doesn’t matter after all, use the best for your project and team

Slide 71

Slide 71 text

Which methodology? Doesn’t matter after all, use the best for your project and team

Slide 72

Slide 72 text

Which test runner? Doesn’t matter after all, use the best for your project and team

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

Questions?

Slide 75

Slide 75 text

Thanks!

Slide 76

Slide 76 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