deixe o teste infectar você
DESCRIPTION
Palestra sobre testes automatizados utilizando Test Driven Development com Ruby.TRANSCRIPT
![Page 1: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/1.jpg)
DEIXE O TESTE INFECTAR VOCÊ
AUTOMATIZANDO OS TESTES DE SUAS APLICA ÕES RUBYÇ
Hercules Lemke Merscher - @hlmerscher
![Page 2: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/2.jpg)
IT'S ME, HERCULES.
DESENVOLVEDOR
CRIADOR DO “DOJO, ONDE?”http://dojoonde.herculesdev.com.br
Integrante do TUX-ES
![Page 3: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/3.jpg)
POR QUE AUTOMATIZAR?
PORQUE SIM!!!
![Page 4: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/4.jpg)
NÃO É RESPOTA
![Page 5: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/5.jpg)
MOTIVOS
Erros recorrentes
![Page 6: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/6.jpg)
MOTIVOS
O COMPUTADOR TESTA MUITO MAIS RÁPIDO
![Page 7: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/7.jpg)
MOTIVOS
O COMPUTADOR TESTA TUDO SEM CANSAR
![Page 8: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/8.jpg)
MOTIVOS
LEI DE MURPHY
![Page 9: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/9.jpg)
![Page 10: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/10.jpg)
#include<stdio.h>
int main(){
printf("Hello World\n");
return 0;
}
NO INÍCIO
![Page 11: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/11.jpg)
TESTANDO
$ gcc hello.c -o hello
$ ./hello
Hello World
![Page 12: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/12.jpg)
OK
![Page 13: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/13.jpg)
MAIS UM POUCO
#include<stdio.h>
int soma(int a, int b){
return a + b;
}
int main(){
printf("%d\n", soma(2, 2) );
return 0;
}
![Page 14: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/14.jpg)
TESTANDO
$ gcc soma.c -o soma
$ ./soma
4
![Page 15: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/15.jpg)
CARA EXPERTO
#include<stdio.h>
int soma(int a, int b){
return a + b;
}
int main(){
printf("A soma de 2+2 = ");
printf("%d\n", soma(2, 2) );
return 0;
}
![Page 16: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/16.jpg)
NOVAMENTE... COM ÉQUIO
$ gcc soma.c -o soma
$ ./soma
A soma de 2+2 = 4
![Page 17: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/17.jpg)
![Page 18: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/18.jpg)
AGORA VAMOS AO MUNDO REAL?
![Page 19: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/19.jpg)
E SE,TESTASSEMOS PRIMEIRO?
![Page 20: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/20.jpg)
É POSSÍVEL!
![Page 21: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/21.jpg)
TEST DRIVEN DEVELOPMENT
![Page 22: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/22.jpg)
CICLOS DO TDD
![Page 23: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/23.jpg)
BABY STEPS
![Page 24: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/24.jpg)
FRAMEWORKS DE TESTEPARA RUBY
Test Unithttp://test-unit.rubyforge.org/
RSpechttp://rspec.info/
Mini Testhttp://docs.seattlerb.org/minitest/
![Page 25: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/25.jpg)
INSTALANDO
Test Unit$ gem install test-unit
RSpec$ gem install rspec
Mini Test$ gem install minitest
![Page 26: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/26.jpg)
TEST UNIT
require "test/unit"
require "./calc"
class CalculadoraTest < Test::Unit::TestCase
def test_soma_2_mais_2
calc = Calculadora.new
assert_equal 4, calc.soma(2, 2)
end
end
![Page 27: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/27.jpg)
RED
$ ruby calc_test.rb
Run options:
# Running tests:
E
Finished tests in 0.000854s, 1171.2208 tests/s, 0.0000 assertions/s.
1) Error:
test_soma_2_mais_2(CalculadoraTest):
NameError: uninitialized constant CalculadoraTest::Calculadora
calc_test.rb:5:in `test_soma_2_mais_2'
1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
![Page 28: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/28.jpg)
RED
class Calculadora
end
![Page 29: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/29.jpg)
RED
$ ruby calc_test.rb
Run options:
# Running tests:
E
Finished tests in 0.000807s, 1238.5972 tests/s, 0.0000 assertions/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_2'
1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
![Page 30: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/30.jpg)
RED
class Calculadora
def soma(a, b)
– end
–
end
![Page 31: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/31.jpg)
RED
$ ruby calc_test.rb
Run options:
# Running tests:
F
Finished tests in 0.001068s, 936.3173 tests/s, 936.3173 assertions/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
![Page 32: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/32.jpg)
RED
class Calculadora
def soma(a, b)
4– end
–
end
![Page 33: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/33.jpg)
GREEN
$ ruby calc_test.rb
Run options:
# Running tests:
.
Finished tests in 0.000806s, 1240.3147 tests/s, 1240.3147 assertions/s.
1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
![Page 34: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/34.jpg)
LEMBRE-SE DO BABY STEPS
![Page 35: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/35.jpg)
GREEN
require "test/unit"
require "./calc"
class CalculadoraTest < Test::Unit::TestCase
– # test_soma_2_mais_2
def test_soma_5_mais_3
calc = Calculadora.new
assert_equal 8, calc.soma(5, 3)
end
end
![Page 36: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/36.jpg)
RED
$ ruby calc_test.rb
Run options:
# Running tests:
.F
Finished tests in 0.001050s, 1903.8843 tests/s, 1903.8843 assertions/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
![Page 37: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/37.jpg)
RED
class Calculadora
def soma(a, b)
a + b– end
–
end
![Page 38: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/38.jpg)
GREEN
$ ruby calc_test.rb
Run options:
# Running tests:
..
Finished tests in 0.000776s, 2577.7647 tests/s, 2577.7647 assertions/s.
2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
![Page 39: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/39.jpg)
RSPEC
require "./calc"
describe Calculadora do
it "deve somar 2 + 2" do
calc = Calculadora.new
calc.soma(2,2).should == 4
end
it "deve somar 5 + 3" do
calc = Calculadora.new
calc.soma(5,3).should == 8
end
end
![Page 40: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/40.jpg)
RSPEC
$ rspec calc_spec.rb
FF
Failures:
1) Calculadora deve somar 2 + 2
Failure/Error: calc.soma(2,2).should == 4
expected: 4
got: 0 (using ==)
# ./calc_spec.rb:6:in `block (2 levels) in <top (required)>'
(...)
Finished in 0.00088 seconds
2 examples, 2 failures
Failed examples:
rspec ./calc_spec.rb:4 # Calculadora deve somar 2 + 2
rspec ./calc_spec.rb:9 # Calculadora deve somar 5 + 3
![Page 41: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/41.jpg)
RSPEC
..
Finished in 0.0007 seconds
2 examples, 0 failures
![Page 42: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/42.jpg)
TESTES DE REGRESSÃO
Que bicho é esse?
![Page 43: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/43.jpg)
ACCEPTANCE TEST DRIVEN DEVELOPMENT
![Page 44: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/44.jpg)
CAPYBARA NO RAILS
$ gem install capybara
…
require “capybara/rails”
![Page 45: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/45.jpg)
CAPYBARA
$ rails generate integration_test calc
invoke mini_test
create test/integration/calc_test.rb
![Page 46: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/46.jpg)
CAPYBARA
require "test_helper"
class CalcTest < ActionDispatch::IntegrationTest
test "deve somar 2 + 2" do
visit "/"
fill_in "primeiro", with: 2
fill_in "segundo", with: 2
click_on "somar"
assert page.has_content?("resultado = 4")
end
end
![Page 47: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/47.jpg)
CAPYBARA
$ rake minitest
Run options:
# Running tests:
E
Fabulous tests in 0.056458s, 17.7121 tests/s, 0.0000 assertions/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
![Page 48: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/48.jpg)
CAPYBARA
root to: “calc#soma”
![Page 49: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/49.jpg)
CAPYBARA
$ rake minitest
Run options:
# Running tests:
E
Fabulous tests in 0.278528s, 3.5903 tests/s, 0.0000 assertions/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
![Page 50: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/50.jpg)
CAPYBARA
$ touch app/views/calc/soma.html.erb
![Page 51: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/51.jpg)
CAPYBARA
$ rake minitest
Run options:
# Running tests:
E
Fabulous tests in 0.278528s, 3.5903 tests/s, 0.0000 assertions/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
![Page 52: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/52.jpg)
CAPYBARA
<%= form_tag("/calc/resultado",
method: “post”) do %>
<%= label_tag(:primeiro, "primeiro") %>
<%= text_field_tag(:primeiro) %>
<% end %>
![Page 53: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/53.jpg)
CAPYBARA
$ rake minitest
Run options:
# Running tests:
E
Fabulous tests in 0.278528s, 3.5903 tests/s, 0.0000 assertions/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
![Page 54: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/54.jpg)
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 %>
![Page 55: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/55.jpg)
CAPYBARA
$ rake minitest
Run options:
# Running tests:
E
Fabulous tests in 0.278528s, 3.5903 tests/s, 0.0000 assertions/s.
1) Error:
test_deve_somar_2_+_2(CalcTest):
ActionView::Template::Error: No route matches{:controller=>"calc", :action=>"resultado"}
...
1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
![Page 56: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/56.jpg)
CAPYBARA
root to: "calc#soma"–
post "calc/resultado"
![Page 57: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/57.jpg)
CAPYBARA
$ rake minitest
Run options:
# Running tests:
E
Fabulous tests in 0.278528s, 3.5903 tests/s, 0.0000 assertions/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
![Page 58: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/58.jpg)
CAPYBARA
CONTROLLER def resultado
primeiro = params[:primeiro].to_i
segundo = params[:segundo].to_i
@resultado = primeiro + segundo
end
VIEW$ touch app/views/calc/resultado.html.erb
<p> resultado = <%= @resultado %> </p>
![Page 59: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/59.jpg)
CAPYBARA
$ ruby -Itest test/integration/calc_test.rb
Run options:
Run options:
# Running tests:
.
Finished tests in 0.779280s, 1.2832 tests/s, 1.2832 assertions/s.
...
1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
![Page 60: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/60.jpg)
LIVROS
![Page 61: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/61.jpg)
LIVROS
![Page 62: Deixe o teste infectar você](https://reader030.vdocuments.pub/reader030/viewer/2022020115/54bd164f4a795957558b458d/html5/thumbnails/62.jpg)
DÚVIDAS?