Advanced Live Testing
Augusto Pascutti / Nelson Senna
Advanced Live Testing
Augusto Pascutti / Nelson Senna
Nossa experiência com TDD
Augusto Pascutti / Nelson Senna
Os pilotos
Augusto Pascutti
Twitter: @augustohp
Github: augustohp
Slides: http://bit.ly/augustohp
Blog: http://augustopascutti.com
Nelson Senna
Twitter: @nelson_senna
Github: nelsonsar
Slides: http://bit.ly/nelsonsar
Blog: http://nelsonsar.github.io
O que é um teste?
1 <?php
2
3 namespace App;
4
5 class Download
6 {
7 public function fromUrl($fromUrl, $toFile)
8 {
9 $contents = file_get_contents($fromUrl);
10 // var_dump($contents);
11 $bytes = file_put_contents(
12 $toFile,
13 $contents
14 );
15 // var_dump($bytes);
16 return $bytes;
17 }
18 }
1 <?php
2
3 namespace App;
4
5 class Download
6 {
7 public function fromUrl($fromUrl, $toFile)
8 {
9 $contents = file_get_contents($fromUrl);
10 // var_dump($contents);
11 $bytes = file_put_contents(
12 $toFile,
13 $contents
14 );
15 // var_dump($bytes);
16 return $bytes;
17 }
18 }
1 <?php
2
3 namespace App;
4
5 class Download
6 {
7 public function fromUrl($fromUrl, $toFile)
8 {
9 $contents = file_get_contents($fromUrl);
10 // var_dump($contents);
11 $bytes = file_put_contents(
12 $toFile,
13 $contents
14 );
15 // var_dump($bytes);
16 return $bytes;
17 }
18 }
1 <?php
2
3 namespace App;
4
5 class DownloadTest extends PHPUnit_Framework_TestCase
6 {
7 const DESTINATION_FILE = '/tmp/test-file';
8
9 public function tearDown()
10 {
11 unlink(self::DESTINATION_FILE);
12 }
13
14 public function testDownloadCreatesTheFile()
15 {
16 $file = self::DESTINATION_FILE;
17 $this->assertFileNotExists($file);
18
19 $downloader = new Download;
20 $url = 'http://example.org';
21 $bytesWritten = $downloader->fromUrl($url, $file);
22
23 $this->assertGreaterThan(0, $bytesWritten);
24 $this->assertFileExists($file);
25 }
26 }
1 <?php
2
3 namespace App;
4
5 class DownloadTest extends PHPUnit_Framework_TestCase
6 {
7 const DESTINATION_FILE = '/tmp/test-file';
8
9 public function tearDown()
10 {
11 unlink(self::DESTINATION_FILE);
12 }
13
14 public function testDownloadCreatesTheFile()
15 {
16 $file = self::DESTINATION_FILE;
17 $this->assertFileNotExists($file);
18
19 $downloader = new Download;
20 $url = 'http://example.org';
21 $bytesWritten = $downloader->fromUrl($url, $file);
22
23 $this->assertGreaterThan(0, $bytesWritten);
24 $this->assertFileExists($file);
25 }
26 }
Diferentes níveis de teste
1. Unitário (white-box)
2. Integração (white-box, black-box)
3. Acceptance (black-box)
O que é TDD?
–Ron Jeffries
“Código limpo que funciona.”
–Wikipedia: Test-driven development
“… metodologia que se baseia na repetição de
ciclos muito curtos de desenvolvimento.”
https://en.wikipedia.org/wiki/Test-driven_development
Um ciclo
Um ciclo
1. Criar um teste (RED)
Um ciclo
1. Criar um teste (RED)
2. Fazer uma mudança pequena (GREEN)
Um ciclo
1. Criar um teste (RED)
2. Fazer uma mudança pequena (GREEN)
3. Refactor
–Kent Beck / Nat Pryce
“O TDD não garante boa arquitetura. Ele dá um
retorno imediato do que, provavelmente, é uma
má arquitetura.”
Por que usar TDD?
Por que usar TDD?
• Evitar que defeito vire falha
• Evitar o “stress loop”
• Aumenta coesão, diminui acoplamento
• É uma forma de documentação
Mão na massa?
O que iremos fazer?
Iterações
1. Falha ao salvar um e-mail na newsletter.
2. Validação de email ao salvar.
3. Salvar um e-mail.
http://github.com/PHPSP/Jarbas

Nossa experiência com TDD

  • 1.
    Advanced Live Testing AugustoPascutti / Nelson Senna
  • 2.
    Advanced Live Testing AugustoPascutti / Nelson Senna
  • 3.
    Nossa experiência comTDD Augusto Pascutti / Nelson Senna
  • 4.
  • 5.
    Augusto Pascutti Twitter: @augustohp Github:augustohp Slides: http://bit.ly/augustohp Blog: http://augustopascutti.com
  • 6.
    Nelson Senna Twitter: @nelson_senna Github:nelsonsar Slides: http://bit.ly/nelsonsar Blog: http://nelsonsar.github.io
  • 7.
    O que éum teste?
  • 8.
    1 <?php 2 3 namespaceApp; 4 5 class Download 6 { 7 public function fromUrl($fromUrl, $toFile) 8 { 9 $contents = file_get_contents($fromUrl); 10 // var_dump($contents); 11 $bytes = file_put_contents( 12 $toFile, 13 $contents 14 ); 15 // var_dump($bytes); 16 return $bytes; 17 } 18 }
  • 9.
    1 <?php 2 3 namespaceApp; 4 5 class Download 6 { 7 public function fromUrl($fromUrl, $toFile) 8 { 9 $contents = file_get_contents($fromUrl); 10 // var_dump($contents); 11 $bytes = file_put_contents( 12 $toFile, 13 $contents 14 ); 15 // var_dump($bytes); 16 return $bytes; 17 } 18 }
  • 10.
    1 <?php 2 3 namespaceApp; 4 5 class Download 6 { 7 public function fromUrl($fromUrl, $toFile) 8 { 9 $contents = file_get_contents($fromUrl); 10 // var_dump($contents); 11 $bytes = file_put_contents( 12 $toFile, 13 $contents 14 ); 15 // var_dump($bytes); 16 return $bytes; 17 } 18 }
  • 11.
    1 <?php 2 3 namespaceApp; 4 5 class DownloadTest extends PHPUnit_Framework_TestCase 6 { 7 const DESTINATION_FILE = '/tmp/test-file'; 8 9 public function tearDown() 10 { 11 unlink(self::DESTINATION_FILE); 12 } 13 14 public function testDownloadCreatesTheFile() 15 { 16 $file = self::DESTINATION_FILE; 17 $this->assertFileNotExists($file); 18 19 $downloader = new Download; 20 $url = 'http://example.org'; 21 $bytesWritten = $downloader->fromUrl($url, $file); 22 23 $this->assertGreaterThan(0, $bytesWritten); 24 $this->assertFileExists($file); 25 } 26 }
  • 12.
    1 <?php 2 3 namespaceApp; 4 5 class DownloadTest extends PHPUnit_Framework_TestCase 6 { 7 const DESTINATION_FILE = '/tmp/test-file'; 8 9 public function tearDown() 10 { 11 unlink(self::DESTINATION_FILE); 12 } 13 14 public function testDownloadCreatesTheFile() 15 { 16 $file = self::DESTINATION_FILE; 17 $this->assertFileNotExists($file); 18 19 $downloader = new Download; 20 $url = 'http://example.org'; 21 $bytesWritten = $downloader->fromUrl($url, $file); 22 23 $this->assertGreaterThan(0, $bytesWritten); 24 $this->assertFileExists($file); 25 } 26 }
  • 13.
    Diferentes níveis deteste 1. Unitário (white-box) 2. Integração (white-box, black-box) 3. Acceptance (black-box)
  • 14.
  • 15.
  • 16.
    –Wikipedia: Test-driven development “…metodologia que se baseia na repetição de ciclos muito curtos de desenvolvimento.” https://en.wikipedia.org/wiki/Test-driven_development
  • 17.
  • 18.
    Um ciclo 1. Criarum teste (RED)
  • 19.
    Um ciclo 1. Criarum teste (RED) 2. Fazer uma mudança pequena (GREEN)
  • 20.
    Um ciclo 1. Criarum teste (RED) 2. Fazer uma mudança pequena (GREEN) 3. Refactor
  • 21.
    –Kent Beck /Nat Pryce “O TDD não garante boa arquitetura. Ele dá um retorno imediato do que, provavelmente, é uma má arquitetura.”
  • 22.
  • 23.
    Por que usarTDD? • Evitar que defeito vire falha • Evitar o “stress loop” • Aumenta coesão, diminui acoplamento • É uma forma de documentação
  • 24.
  • 25.
  • 27.
    Iterações 1. Falha aosalvar um e-mail na newsletter. 2. Validação de email ao salvar. 3. Salvar um e-mail.
  • 28.