Test Driven Development

2,109 views

Published on

Published in: Technology, Business
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,109
On SlideShare
0
From Embeds
0
Number of Embeds
272
Actions
Shares
0
Downloads
63
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide
  • Necessidade rara de debugar Facilidade em capturar erros antes de irem para produção Economia no retrabalho Guiado a criar codigo altamente coeso e com baixo acomplamento, mais modularizado, extensivel e flexivel, portanto de fácil manutenção. Matematico processo de assertions e precondições [Design by contracts] Principio de pareto 80-20 80% das consequências advém de 20% das causas
  • Test Driven Development

    1. 1. Test Driven Development [TDD] Christiano Milfont #XPCE 2009, Fortaleza Copyleft 2009 Milfont.org Desenvolvimento guiado a testes
    2. 2. Test Driven Development <ul><li>“ Desenvolvimento guiado por testes é um caminho de gerenciamento do medo durante a programação.” </li></ul><ul><li>Kent Beck - Test Driven </li></ul><ul><li>Development by Example </li></ul>
    3. 3. Test Driven Development Standup Meeting @ 9h Pair Up Test First [Prática] Code Refactor Integrar ou Disponibilizar Ir para casa @ 17h
    4. 4. Test Driven Development <ul><li>O ritmo em 3 A’s </li></ul><ul><li>Arrange [Criar um objeto] </li></ul><ul><li>Act [Invocar um método] </li></ul><ul><li>Assert [Verificar o resultado] </li></ul><ul><li>Refactoring Workbook, Bill Wake </li></ul>
    5. 5. Test Driven Development <ul><li>RED - GREEN - REFACTOR </li></ul><ul><li>Escreva um teste que não funciona. </li></ul><ul><li>Escreva o código e faço-o funcionar. </li></ul><ul><li>Refatore e elimine o código repetitivo. </li></ul>
    6. 6. Test Driven Development <ul><li>Red Bar Patterns </li></ul><ul><li>One Step Test </li></ul><ul><li>Starter Test </li></ul><ul><li>Explanation Test </li></ul><ul><li>Learning Test </li></ul><ul><li>Another Test </li></ul><ul><li>Regression Test </li></ul><ul><li>Break </li></ul><ul><li>Do Over </li></ul>
    7. 7. Test Driven Development <ul><li>Red Bar Patterns </li></ul><ul><li>One Step Test </li></ul><ul><li>Starter Test </li></ul><ul><li>Explanation Test </li></ul><ul><li>Learning Test </li></ul><ul><li>Another Test </li></ul><ul><li>Regression Test </li></ul><ul><li>Break </li></ul><ul><li>Do Over </li></ul><ul><ul><ul><li>Issue issue = member </li></ul></ul></ul><ul><ul><ul><ul><li>. createIssue (name) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>.withType( type ) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>.withLevel( level ) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>.withSummary( summary ) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>.toProject( project ); </li></ul></ul></ul></ul>
    8. 8. Test Driven Development <ul><li>Red Bar Patterns </li></ul><ul><li>One Step Test </li></ul><ul><li>Starter Test </li></ul><ul><li>Explanation Test </li></ul><ul><li>Learning Test </li></ul><ul><li>Another Test </li></ul><ul><li>Regression Test </li></ul><ul><li>Break </li></ul><ul><li>Do Over </li></ul>@Test public void createIssueFromMember() throws IllegalArgumentIssueException { member = new Member(); issue = member . createIssue (&quot;Issue created&quot;); Assert.assertNotNull( ISSUE_IN_NULL, issue); Assert.assertEquals( &quot;State is not unconfirmed&quot;, Status.UNCONFIRMED, issue.getStatus()); }
    9. 9. Test Driven Development <ul><li>Red Bar Patterns </li></ul><ul><li>One Step Test </li></ul><ul><li>Starter Test </li></ul><ul><li>Explanation Test </li></ul><ul><li>Learning Test </li></ul><ul><li>Another Test </li></ul><ul><li>Regression Test </li></ul><ul><li>Break </li></ul><ul><li>Do Over </li></ul>issue = new Member() . createIssue (&quot;Issue created&quot;); Assert. assertNotNull ( ISSUE_IN_NULL, issue); Assert. assertEquals ( &quot;State is not unconfirmed&quot;, Status.UNCONFIRMED, issue.getStatus());
    10. 10. Test Driven Development <ul><li>Red Bar Patterns </li></ul><ul><li>One Step Test </li></ul><ul><li>Starter Test </li></ul><ul><li>Explanation Test </li></ul><ul><li>Learning Test </li></ul><ul><li>Another Test </li></ul><ul><li>Regression Test </li></ul><ul><li>Break </li></ul><ul><li>Do Over </li></ul>type = new Type(){{ this.setId(Long.valueOf(10)); this.setName(BUG); }}; member = new Member().withType(type); issue = member.getIssueInProgress(); Assert. assertNotNull (ISSUE_IN_NULL, issue); Assert. assertNotNull (&quot;Type is null&quot;, issue.getType()); Assert. assertTrue (&quot;Type is not BUG&quot;, issue.getType().getId() == type.getId()); Assert. assertTrue (&quot;Type is not BUG&quot;, issue.getType().getName() == type.getName()); Assert. assertEquals (&quot;Type is not BUG&quot;, issue.getType().getName(), BUG);
    11. 11. Test Driven Development <ul><li>Red Bar Patterns </li></ul><ul><li>One Step Test </li></ul><ul><li>Starter Test </li></ul><ul><li>Explanation Test </li></ul><ul><li>Learning Test </li></ul><ul><li>Another Test </li></ul><ul><li>Regression Test </li></ul><ul><li>Break </li></ul><ul><li>Do Over </li></ul>@Test public void createIssueFromMemberWithNameEmpty () { ... } @Test public void setTypeInIssueFromMember () throws IllegalArgumentIssueException { … }
    12. 12. Test Driven Development <ul><li>Red Bar Patterns </li></ul><ul><li>One Step Test </li></ul><ul><li>Starter Test </li></ul><ul><li>Explanation Test </li></ul><ul><li>Learning Test </li></ul><ul><li>Another Test </li></ul><ul><li>Regression Test </li></ul><ul><li>Break </li></ul><ul><li>Do Over </li></ul><ul><ul><li>Issue issue = member </li></ul></ul><ul><ul><ul><li>.createIssue(name) </li></ul></ul></ul><ul><ul><ul><li>.withType( type ) </li></ul></ul></ul><ul><ul><ul><li>.withLevel( level ) </li></ul></ul></ul><ul><ul><ul><li>.withSummary( summary ) </li></ul></ul></ul><ul><ul><ul><li>.toProject( project ); </li></ul></ul></ul><ul><ul><li>Assert.assertNotNull( </li></ul></ul><ul><ul><li>&quot;Issue não gerada com sucesso!&quot;, issue); </li></ul></ul><ul><ul><li>Assert.assertTrue( </li></ul></ul><ul><ul><li>&quot;Issue não gerada e id não atribuído&quot;, issue.getId() > 0); </li></ul></ul>
    13. 13. Test Driven Development <ul><li>Red Bar Patterns </li></ul><ul><li>One Step Test </li></ul><ul><li>Starter Test </li></ul><ul><li>Explanation Test </li></ul><ul><li>Learning Test </li></ul><ul><li>Another Test </li></ul><ul><li>Regression Test </li></ul><ul><li>Break </li></ul><ul><li>Do Over </li></ul>@Test public void createIssueFromMemberWithNameNull() { try { issue = new Member() .createIssue( null ); Assert.fail( &quot;Didn't find expected exception of type &quot; + IllegalArgumentIssueException.class.getName()); } catch (IllegalArgumentIssueException e) { Assert.assertEquals(&quot;Exception correctly catch&quot;, &quot;Name is null or empty“, e.getMessage()); } }
    14. 14. Test Driven Development <ul><li>Red Bar Patterns </li></ul><ul><li>One Step Test </li></ul><ul><li>Starter Test </li></ul><ul><li>Explanation Test </li></ul><ul><li>Learning Test </li></ul><ul><li>Another Test </li></ul><ul><li>Regression Test </li></ul><ul><li>Break </li></ul><ul><li>Do Over </li></ul>
    15. 15. Test Driven Development <ul><li>Green Bar Patterns </li></ul><ul><li>Fake It (‘till you make it) </li></ul><ul><li>Triangulate </li></ul><ul><li>Obvious Implementation </li></ul><ul><li>One to Many </li></ul>
    16. 16. Test Driven Development <ul><li>Green Bar Patterns </li></ul><ul><li>Fake It (Till you make it) </li></ul><ul><li>Triangulate </li></ul><ul><li>Obvious Implementation </li></ul><ul><li>One to Many </li></ul>context.checking(new Expectations() {{ oneOf (repository).persist(with(any(Issue.class))); will (new CustomAction(&quot;Add id value to issue&quot;) { public Object invoke (Invocation invocation) throws Throwable { ( (Issue) invocation.getParameter(0)). setId(Long.valueOf(1)); return null; } });}});
    17. 17. Test Driven Development <ul><li>Green Bar Patterns </li></ul><ul><li>Fake It (Till you make it) </li></ul><ul><li>Triangulate </li></ul><ul><li>Obvious Implementation </li></ul><ul><li>One to Many </li></ul>@Test public void setNullSummaryInIssueFromMember () {...} @Test public void setSummaryInIssueFromMember () {...} @Test public void setEmptySummaryInIssueFromMember () { ..}
    18. 18. Test Driven Development <ul><li>Green Bar Patterns </li></ul><ul><li>Fake It (Till you make it) </li></ul><ul><li>Triangulate </li></ul><ul><li>Obvious Implementation </li></ul><ul><li>One to Many </li></ul>... List<Issue> issues = new ArrayList<Issue>() { { this.add( new Issue(Long.valueOf(134)) ); } }... Assert.assertTrue(“blable”, issues.size()==1);
    19. 19. Test Driven Development <ul><li>Green Bar Patterns </li></ul><ul><li>Fake It (Till you make it) </li></ul><ul><li>Triangulate </li></ul><ul><li>Obvious Implementation </li></ul><ul><li>One to Many </li></ul>... List<Issue> issues = new ArrayList<Issue>() { { this.add( new Issue(Long.valueOf(134)) ); } }... Assert.assertTrue(“blable”, issues.size()==1 );
    20. 20. Test Driven Development <ul><li>Testing Patterns </li></ul><ul><li>Child Test </li></ul><ul><li>Mock Object </li></ul><ul><li>Self Shunt </li></ul><ul><li>Log String </li></ul><ul><li>Crash Test Dummy </li></ul><ul><li>Broken Test </li></ul><ul><li>Clean Check-In </li></ul>
    21. 21. Test Driven Development <ul><li>Testing Patterns </li></ul><ul><li>Child Test </li></ul><ul><li>Mock Object </li></ul><ul><li>Self Shunt </li></ul><ul><li>Log String </li></ul><ul><li>Crash Test Dummy </li></ul><ul><li>Broken Test </li></ul><ul><li>Clean Check-In </li></ul>@RunWith(JMock.class) public class LifeCycleOfIssueInProjectTest { ... } @RunWith(JMock.class) public class ReportIssuesTest { ... }
    22. 22. Test Driven Development <ul><li>Testing Patterns </li></ul><ul><li>Child Test </li></ul><ul><li>Mock Object </li></ul><ul><li>Self Shunt </li></ul><ul><li>Log String </li></ul><ul><li>Crash Test Dummy </li></ul><ul><li>Broken Test </li></ul><ul><li>Clean Check-In </li></ul>context.checking(new Expectations() {{ oneOf (repository).persist(with(any(Issue.class))); will (new CustomAction(&quot;Add id value to issue&quot;) { public Object invoke (Invocation invocation) throws Throwable { ( (Issue) invocation.getParameter(0)). setId(Long.valueOf(1)); return null; } }); }});
    23. 23. Test Driven Development <ul><li>Testing Patterns </li></ul><ul><li>Child Test </li></ul><ul><li>Mock Object </li></ul><ul><li>Self Shunt </li></ul><ul><li>Log String </li></ul><ul><li>Crash Test Dummy </li></ul><ul><li>Broken Test </li></ul><ul><li>Clean Check-In </li></ul>context.checking(new Expectations() {{ oneOf (repository).persist(with(any(Issue.class))); will (new CustomAction(&quot;Add id value to issue&quot;) { public Object invoke (Invocation invocation) throws Throwable { ( (Issue) invocation.getParameter(0)). setId(Long.valueOf(1)); repository.issues.add( ( (Issue) invocation.getParameter(0))); return null; } }); }}); ... Assert.assertTrue( repository.size() == 12 );
    24. 24. Test Driven Development <ul><li>Testing Patterns </li></ul><ul><li>Child Test </li></ul><ul><li>Mock Object </li></ul><ul><li>Self Shunt </li></ul><ul><li>Log String </li></ul><ul><li>Crash Test Dummy </li></ul><ul><li>Broken Test </li></ul><ul><li>Clean Check-In </li></ul>
    25. 25. Test Driven Development <ul><li>Testing Patterns </li></ul><ul><li>Child Test </li></ul><ul><li>Mock Object </li></ul><ul><li>Self Shunt </li></ul><ul><li>Log String </li></ul><ul><li>Crash Test Dummy </li></ul><ul><li>Broken Test </li></ul><ul><li>Clean Check-In </li></ul>@Test public void createIssueFromMemberWithNameNull() { try { issue = new Member() .createIssue( null ); Assert. fail ( &quot;Didn't find expected exception of type &quot; + IllegalArgumentIssueException.class.getName()); } catch ( IllegalArgumentIssueException e) { Assert.assertEquals(&quot; Exception correctly catch &quot;, &quot; Name is null or empty “, e.getMessage() ); } }
    26. 26. Test Driven Development <ul><li>Testing Patterns </li></ul><ul><li>Child Test </li></ul><ul><li>Mock Object </li></ul><ul><li>Self Shunt </li></ul><ul><li>Log String </li></ul><ul><li>Crash Test Dummy </li></ul><ul><li>Broken Test </li></ul><ul><li>Clean Check-In </li></ul>
    27. 27. Test Driven Development <ul><li>Testing Patterns </li></ul><ul><li>Child Test </li></ul><ul><li>Mock Object </li></ul><ul><li>Self Shunt </li></ul><ul><li>Log String </li></ul><ul><li>Crash Test Dummy </li></ul><ul><li>Broken Test </li></ul><ul><li>Clean Check-In </li></ul>
    28. 28. Test Driven Development <ul><li>Test Double </li></ul><ul><li>Dummy </li></ul><ul><li>Fake </li></ul><ul><li>Stubs </li></ul><ul><li>Spies </li></ul><ul><li>Mocks </li></ul>
    29. 29. Test Driven Development <ul><li>Test Double </li></ul><ul><li>Dummy </li></ul><ul><li>Fake </li></ul><ul><li>Stubs </li></ul><ul><li>Spies </li></ul><ul><li>Mocks </li></ul>... List<Issue> issues = new ArrayList<Issue>() { { this.add( new Issue(Long.valueOf(134)) ); } }... Assert.assertTrue(“blable”, issues.size()==1);
    30. 30. Test Driven Development <ul><li>Test Double </li></ul><ul><li>Dummy </li></ul><ul><li>Fake </li></ul><ul><li>Stubs </li></ul><ul><li>Spies </li></ul><ul><li>Mocks </li></ul>... IssueRepository repository = new FakeRepository(); List<Issue> issuesUnconfirmeds = repository. getIssuesUnconfirmeds(); Assert.assertTrue(“blable”, issuesUnconfirmeds != null); ... public class FakeRepository implements IssueRepository { public List<Issue> getIssuesUnconfirmeds() { return new ArrayList<Issue>(); } }
    31. 31. Test Driven Development <ul><li>Test Double </li></ul><ul><li>Dummy </li></ul><ul><li>Fake </li></ul><ul><li>Stubs </li></ul><ul><li>Spies </li></ul><ul><li>Mocks </li></ul>context.checking(new Expectations() {{ ignoring (repository).count(); will ( returnValue(42)); }}); ... Assert.assertEquals( 12 , repository.count() );
    32. 32. Test Driven Development <ul><li>Test Double </li></ul><ul><li>Dummy </li></ul><ul><li>Fake </li></ul><ul><li>Stubs </li></ul><ul><li>Spies </li></ul><ul><li>Mocks </li></ul>context.checking(new Expectations() {{ oneOf (repository).persist(with(any(Issue.class))); will (new CustomAction(&quot;Add id value to issue&quot;) { public Object invoke (Invocation invocation) throws Throwable { ( (Issue) invocation.getParameter(0)). setId(Long.valueOf(1)); repository.issues.add( ( (Issue) invocation.getParameter(0))); return null; } }); }}); ... Assert.assertTrue( repository.size() == 12 );
    33. 33. Test Driven Development <ul><li>Test Double </li></ul><ul><li>Dummy </li></ul><ul><li>Fake </li></ul><ul><li>Stubs </li></ul><ul><li>Spies </li></ul><ul><li>Mocks </li></ul>context.checking(new Expectations() {{ oneOf (repository).persist(with(any(Issue.class))); will (new CustomAction(&quot;Add id value to issue&quot;) { public Object invoke (Invocation invocation) throws Throwable { ( (Issue) invocation.getParameter(0)). setId(Long.valueOf(1)); return null; } }); }});
    34. 34. Test Driven Development <ul><li>Fixture Setup </li></ul><ul><li>Setup </li></ul><ul><li>Tear Down </li></ul>@Before public void setUp() throws Exception { Connection conn; try { ... IDatabaseConnection connection = new DatabaseConnection(conn); DatabaseOperation.INSERT.execute(connection, new FlatXmlDataSet( new FileInputStream( “ issuetrackr.xml&quot;))); conn.close(); } catch (Exception exc) { ... } }

    ×