Upcoming SlideShare
×

# Deixe o teste infectar você

609 views

Published on

Palestra sobre testes automatizados utilizando Test Driven Development com Ruby.

Published in: Technology, Education
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

Views
Total views
609
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
1
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Deixe o teste infectar você

1. 1. DEIXE O TESTE INFECTARVOCÊAUTOMATIZANDO OS TESTES DE SUASAPLICA ÕES RUBYÇHercules Lemke Merscher - @hlmerscher
2. 2. ITS ME, HERCULES.DESENVOLVEDORCRIADOR DO “DOJO, ONDE?”http://dojoonde.herculesdev.com.brIntegrante do TUX-ES
3. 3. POR QUE AUTOMATIZAR?PORQUE SIM!!!
4. 4. NÃO É RESPOTA
5. 5. MOTIVOSErros recorrentes
6. 6. MOTIVOSO COMPUTADOR TESTA MUITO MAIS RÁPIDO
7. 7. MOTIVOSO COMPUTADOR TESTA TUDO SEM CANSAR
8. 8. MOTIVOSLEI DE MURPHY
9. 9. #include<stdio.h>int main(){printf("Hello Worldn");return 0;}NO INÍCIO
10. 10. TESTANDO\$ gcc hello.c -o hello\$ ./helloHello World
11. 11. OK
12. 12. MAIS UM POUCO#include<stdio.h>int soma(int a, int b){return a + b;}int main(){printf("%dn", soma(2, 2) );return 0;}
13. 13. TESTANDO\$ gcc soma.c -o soma\$ ./soma4
14. 14. CARA EXPERTO#include<stdio.h>int soma(int a, int b){return a + b;}int main(){printf("A soma de 2+2 = ");printf("%dn", soma(2, 2) );return 0;}
15. 15. NOVAMENTE... COM ÉQUIO\$ gcc soma.c -o soma\$ ./somaA soma de 2+2 = 4
16. 16. AGORA VAMOS AO MUNDOREAL?
17. 17. E SE,TESTASSEMOS PRIMEIRO?
18. 18. É POSSÍVEL!
19. 19. TEST DRIVEN DEVELOPMENT
20. 20. CICLOS DO TDD
21. 21. BABY STEPS
22. 22. FRAMEWORKS DE TESTEPARA RUBYTest Unithttp://test-unit.rubyforge.org/RSpechttp://rspec.info/Mini Testhttp://docs.seattlerb.org/minitest/
23. 23. INSTALANDOTest Unit\$ gem install test-unitRSpec\$ gem install rspecMini Test\$ gem install minitest
24. 24. TEST UNITrequire "test/unit"require "./calc"class CalculadoraTest < Test::Unit::TestCasedef test_soma_2_mais_2calc = Calculadora.newassert_equal 4, calc.soma(2, 2)endend
25. 25. RED\$ ruby calc_test.rbRun options:# Running tests:EFinished tests in 0.000854s, 1171.2208 tests/s, 0.0000assertions/s.1) Error:test_soma_2_mais_2(CalculadoraTest):NameError: uninitialized constant CalculadoraTest::Calculadoracalc_test.rb:5:in `test_soma_2_mais_21 tests, 0 assertions, 0 failures, 1 errors, 0 skips
26. 26. REDclass Calculadoraend
27. 27. RED\$ ruby calc_test.rbRun options:# Running tests:EFinished tests in 0.000807s, 1238.5972 tests/s, 0.0000assertions/s.1) Error:test_soma_2_mais_2(CalculadoraTest):NoMethodError: undefined method `soma for#<Calculadora:0x00000000ef0b00>calc_test.rb:7:in `test_soma_2_mais_21 tests, 0 assertions, 0 failures, 1 errors, 0 skips
28. 28. REDclass Calculadoradef soma(a, b)– end–end
29. 29. RED\$ ruby calc_test.rbRun options:# Running tests:FFinished tests in 0.001068s, 936.3173 tests/s, 936.3173assertions/s.1) Failure:test_soma_2_mais_2(CalculadoraTest) [calc_test.rb:7]:<4> expected but was<nil>.1 tests, 1 assertions, 1 failures, 0 errors, 0 skips
30. 30. REDclass Calculadoradef soma(a, b)4– end–end
31. 31. GREEN\$ ruby calc_test.rbRun options:# Running tests:.Finished tests in 0.000806s, 1240.3147tests/s, 1240.3147 assertions/s.1 tests, 1 assertions, 0 failures, 0 errors,0 skips
32. 32. LEMBRE-SE DO BABY STEPS
33. 33. GREENrequire "test/unit"require "./calc"class CalculadoraTest < Test::Unit::TestCase– # test_soma_2_mais_2def test_soma_5_mais_3calc = Calculadora.newassert_equal 8, calc.soma(5, 3)endend
34. 34. RED\$ ruby calc_test.rbRun options:# Running tests:.FFinished tests in 0.001050s, 1903.8843 tests/s, 1903.8843assertions/s.1) Failure:test_soma_5_mais_3(CalculadoraTest) [calc_test.rb:12]:<8> expected but was<4>.2 tests, 2 assertions, 1 failures, 0 errors, 0 skips
35. 35. REDclass Calculadoradef soma(a, b)a + b– end–end
36. 36. GREEN\$ ruby calc_test.rbRun options:# Running tests:..Finished tests in 0.000776s, 2577.7647tests/s, 2577.7647 assertions/s.2 tests, 2 assertions, 0 failures, 0 errors,0 skips
37. 37. RSPECrequire "./calc"describe Calculadora doit "deve somar 2 + 2" docalc = Calculadora.newcalc.soma(2,2).should == 4endit "deve somar 5 + 3" docalc = Calculadora.newcalc.soma(5,3).should == 8endend
38. 38. RSPEC\$ rspec calc_spec.rbFFFailures:1) Calculadora deve somar 2 + 2Failure/Error: calc.soma(2,2).should == 4expected: 4got: 0 (using ==)# ./calc_spec.rb:6:in `block (2 levels) in <top (required)>(...)Finished in 0.00088 seconds2 examples, 2 failuresFailed examples:rspec ./calc_spec.rb:4 # Calculadora deve somar 2 + 2rspec ./calc_spec.rb:9 # Calculadora deve somar 5 + 3
39. 39. RSPEC..Finished in 0.0007 seconds2 examples, 0 failures
40. 40. TESTES DEREGRESSÃOQue bicho é esse?
41. 41. ACCEPTANCE TEST DRIVENDEVELOPMENT
42. 42. CAPYBARA NO RAILS\$ gem install capybara…require “capybara/rails”
43. 43. CAPYBARA\$ rails generate integration_test calcinvoke mini_testcreate test/integration/calc_test.rb
44. 44. CAPYBARArequire "test_helper"class CalcTest < ActionDispatch::IntegrationTesttest "deve somar 2 + 2" dovisit "/"fill_in "primeiro", with: 2fill_in "segundo", with: 2click_on "somar"assert page.has_content?("resultado = 4")endend
45. 45. CAPYBARA\$ rake minitestRun options:# Running tests:EFabulous tests in 0.056458s, 17.7121 tests/s, 0.0000assertions/s.1) Error:test_deve_somar_2_+_2(CalcTest):ActionController::RoutingError: No route matches [GET]"/"...1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
46. 46. CAPYBARAroot to: “calc#soma”
47. 47. CAPYBARA\$ rake minitestRun options:# Running tests:EFabulous tests in 0.278528s, 3.5903 tests/s, 0.0000assertions/s.1) Error:test_deve_somar_2_+_2(CalcTest):ActionView::MissingTemplate: Missing template calculadora/soma,application/soma with {:locale=>[:en], :formats=>[:html],:handlers=>[:erb, :builder]}. Searched in:...1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
48. 48. CAPYBARA\$ touch app/views/calc/soma.html.erb
49. 49. CAPYBARA\$ rake minitestRun options:# Running tests:EFabulous tests in 0.278528s, 3.5903 tests/s, 0.0000assertions/s.1) Error:test_deve_somar_2_+_2(CalcTest):Capybara::ElementNotFound: Unable to find field"primeiro"...1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
50. 50. CAPYBARA<%= form_tag("/calc/resultado",method: “post”) do %><%= label_tag(:primeiro, "primeiro") %><%= text_field_tag(:primeiro) %><% end %>
51. 51. CAPYBARA\$ rake minitestRun options:# Running tests:EFabulous tests in 0.278528s, 3.5903 tests/s, 0.0000assertions/s.1) Error:test_deve_somar_2_+_2(CalcTest):Capybara::ElementNotFound: Unable to find field "segundo"...1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
52. 52. CAPYBARA<%= form_tag("/calc/resultado",method: “post”) do %><%= label_tag(:primeiro, "primeiro") %><%= text_field_tag(:primeiro) %><%= label_tag(:segundo, "segundo") %><%= text_field_tag(:segundo) %><%= submit_tag(“somar”) %><% end %>
53. 53. CAPYBARA\$ rake minitestRun options:# Running tests:EFabulous tests in 0.278528s, 3.5903 tests/s, 0.0000assertions/s.1) Error:test_deve_somar_2_+_2(CalcTest):ActionView::Template::Error: No routematches{:controller=>"calc", :action=>"resultado"}...1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
54. 54. CAPYBARAroot to: "calc#soma"–post "calc/resultado"
55. 55. CAPYBARA\$ rake minitestRun options:# Running tests:EFabulous tests in 0.278528s, 3.5903 tests/s, 0.0000assertions/s.1) Error:test_deve_somar_2_+_2(CalcTest):ActionView::MissingTemplate: Missing template calc/resultado,application/resultado with {:locale=>[:en],:formats=>[:html], :handlers=>[:erb, :builder]}....1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
56. 56. CAPYBARACONTROLLERdef resultadoprimeiro = params[:primeiro].to_isegundo = params[:segundo].to_i@resultado = primeiro + segundoendVIEW\$ touch app/views/calc/resultado.html.erb<p> resultado = <%= @resultado %> </p>
57. 57. CAPYBARA\$ ruby -Itest test/integration/calc_test.rbRun options:Run options:# Running tests:.Finished tests in 0.779280s, 1.2832 tests/s, 1.2832assertions/s....1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
58. 58. LIVROS
59. 59. LIVROS
60. 60. DÚVIDAS?