Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Testes? Mas isso não aumenta o tempo de projecto? Não quero...

3,008 views

Published on

Os Testes são cada vez mais uma necessidade nos projectos de desenvolvimento de software... Sejam eles unitários, de carga ou de "User Interface", uma boa framework de testes ajuda a resolver os problemas mais cedo, de forma mais eficaz e mais barata.
No final da sessão vamos perceber não só para que servem, como são feitos e como o Visual Studio 2010 pode ajudar.

Published in: Technology

Testes? Mas isso não aumenta o tempo de projecto? Não quero...

  1. 1. http://netponto.org<br />1ª Reunião em Coimbra - 09/04/2011<br />Testes? Mas isso não aumenta o tempo de projecto? Não quero...Pedro Rosa<br />
  2. 2. Patrocinadores desta reunião<br />
  3. 3. Pedro Rosa<br />LinkedIn<br />http://pt.linkedin.com/in/pedrobarraurosa<br />Twitter<br />http://twitter.com/pedrorosa<br />
  4. 4. Agenda<br />Why?<br />Code Coverage<br />Static / Dynamic Analysis<br />Unit Testing<br />WebTesting<br />Load (Performance) Testing<br />
  5. 5. Também disponível em vídeo...<br />Assista!<br />http://www.vimeo.com/22446417<br />
  6. 6. The State of Application Development <br /><ul><li>Only 1 in 3 projects succeed
  7. 7. Projects cost 50% more then budgeted
  8. 8. Lack of software quality cost US businesses $59B / year.
  9. 9. Bugs account for 55% of downtime </li></ul>Source: Standish Group, 2006 Third Quarter Research Report, CHAOS Research Results<br />
  10. 10. Have you heard any of these?<br />“my testers are spending too long testing the same thing”<br />“tooling is expensive (time, licenses, people)”<br />“the developers say the defects are useless”<br />“when is my software ready to ship?”<br />“developers and testers work in silos and don’t communicate/speak the same language”<br />“we have a changed requirement, what do I need to test?”<br />
  11. 11. 68% <br />
  12. 12. What if you could…<br />reduce the time it takes to determine the root cause of a bug<br />enable users to easily run acceptance tests and track their results<br />reduce the time it takes to configure and manage test environments<br />reduce the time it takes to verify the status of a reported bug fix<br />repair a bug without having to reproduce it<br />
  13. 13. software bugs cost the US economy an estimated $59.5 billion every year<br />on average professional coders make 100 to 150 errors in every 1000 lines of code they write<br />last year, canceled projects cost firms $67 billion; overruns on the other projects racked up another $21billion<br />
  14. 14. Need something a little more concrete?<br />
  15. 15. USS Yorktown, SmartShip<br />crew member entered 0 in a data entry field, caused a “divide by 0” error that shut down propulsion<br />dead in the water for 2hrs 45mins<br />
  16. 16. Ariane 5 Flight 501<br />re-used code from ariane 4, but took a different flight path because of different booster design<br />conversation from 64bit float to 16bit signed int caused overflow (exception handler was disabled for perf reasons)<br />cost: > $370 million @ 1996 prices<br />
  17. 17. F-22 Raptor<br />deploying overseas to japan the first time<br />crossed international dateline, computers crashed losing all navigation and communications systems<br />clear weather allowed them to follow tankers back to hawaii<br />Raptor is likely the most advanced manned aircraft that will ever fly…<br />
  18. 18. Need something a little more visual?<br />
  19. 19.
  20. 20.
  21. 21.
  22. 22.
  23. 23. Still Not Convinced?<br />
  24. 24. Climbing<br />Cost of Failure<br />Conditioning<br />Training<br />Training Phase<br />
  25. 25. Release<br />Cost of Bugs<br />Test<br />Development<br />Software Phase<br />
  26. 26. Push Quality Upstream<br />Release<br />Cost of Bugs<br />Test<br />Development<br />Software Phase<br />
  27. 27. Where does testing happen?<br />70% of testing happens here<br />Majority of test tools target here<br />Black Box Testing<br />White Box Testing<br />API Testing<br />
  28. 28. Visual Studio 2010<br />
  29. 29. Test Manager 2010<br />UML Modeling<br />Manual Testing<br />Layer Diagram<br />Load Testing<br />Web Testing<br />Test Case Management<br />IntelliTrace™<br />Architecture Explorer<br />Logical Class Designer<br />Cloud Development<br />Office Development<br />Windows Development<br />New WPF Editor<br />Customizable IDE<br />Multi-core Development<br />Silverlight Tools<br />Web Development<br />SharePoint Development<br />Generate from Usage<br />Static Code Analysis<br />Database Deployment<br />Code Metrics<br />Database Unit Testing<br />Test Data Generation<br />Test Impact Analysis<br />UI Test Automation<br />Code Coverage<br />Performance Profiling<br />Database Change Mgmt<br />Fast Forward for Manual Testing<br />
  30. 30. Visual Studio 2010 Test Types<br /><ul><li>Unit Test
  31. 31. Database Unit Test
  32. 32. Web PerformanceTest
  33. 33. Load Test
  34. 34. Manual Test (TestCase)
  35. 35. Generic Test
  36. 36. Ordered Test
  37. 37. Coded UI Test</li></li></ul><li>What is Unit Testing?<br />
  38. 38. Unit Test – Definition<br />A unit test is a piece of code written by a developer that exercises a very small, specific area of functionality of the code being tested.<br />“Program testing can be used to show the presence of bugs, but never to show their absence!”<br />EdsgerDijkstra, [1972] <br />
  39. 39. Manual Testing<br /><ul><li>You have already done unit testing
  40. 40. Manually, by hand
  41. 41. Manual tests are less efficient
  42. 42. Not structured
  43. 43. Not repeatable
  44. 44. Not on all your code
  45. 45. Not easy to do as it should be</li></li></ul><li>Unit Test Example<br />int Sum(int[] array)<br />{<br /> sum = 0;<br /> for (int i=0; i<array.Length; i++)<br /> sum += array[i];<br /> return sum;<br />}<br />void TestSum()<br />{<br /> if (Sum(new int[]{1,2}) != 3)<br /> throw new TestFailedException("1+2 != 3");<br /> if (Sum(new int[]{-2}) != -2)<br /> throw new TestFailedException("-2 != -2");<br /> if (Sum(new int[]{}) != 0)<br /> throw new TestFailedException("0 != 0");<br />}<br />
  46. 46. Unit Testing Some Facts<br />Tests are specific pieces of code<br />Unit testing framework is needed<br />Visual Studio Team Test<br />NUnit<br />MbUnit<br />Unit tests are written by developers, not by QA engineers<br />Unit tests are released into the code repository along with the code they test<br />
  47. 47. Unit Testing – More Facts<br />All classes should be tested<br />Test anything that could have bugs<br />All methods should be tested<br />Trivial code may be omitted <br />E.g. property getters and setters<br />Ideally all unit tests should pass before check-in into the source control repository<br />
  48. 48. Why Unit Tests?<br />Unit tests dramatically decrease the number of defects in the code <br />Unit tests improve design <br />Unit tests are good documentation<br />Unit tests reduce the cost of change<br />Unit tests allow refactoring<br />Unit tests decrease the defect-injection rate due to refactoring / changes<br />
  49. 49. Code and Test vs. Test Driven Development<br />
  50. 50. Unit Testing Approaches<br />"Code and Test" approach<br />Classical approach<br />"Test First" approach<br />Test driven development (TDD)<br />
  51. 51. Code and Test Approach<br />Write code<br />Write unit test<br />Run and succeed<br />Time flow<br />
  52. 52. Create a testlist<br />Pick а test<br />Compile and fail<br />Write enough code to compile<br />Run test and fail<br />Write code to pass test <br />Test Driven Development<br />Write test<br />Time flow<br />Remove duplication<br />
  53. 53. Why Test Driven Development?<br />Helps find design issues early and avoids rework<br />Writing code to satisfy a test is a focused activity – less chance of error<br />Tests will be a more comprehensive than when written after code<br />
  54. 54. Unit Testing Frameworks and Visual Studio Team Test<br />
  55. 55. Unit Testing Frameworks<br />JUnit<br />The first popular unit testing framework<br />Based on Java<br />Similar frameworks have been developed for a broad range of computer languages<br />NUnit – for C# and all .NET languages<br />cppUnit, jsUnit, PhpUnit, PerlUnit, ...<br />Visual Studio Team Test (VSTT)<br />Developed by Microsoft, integrated in VS<br />
  56. 56. Visual Studio Team Test – Features<br />Team Test (TT) is very well integrated with Visual Studio<br />Create test projects and unit tests<br />Execute unit tests<br />View execution results<br />View code coverage<br />Located in the assembly Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll<br />
  57. 57. Visual Studio Team Test – Attributes<br />Test code is annotated using custom attributes<br /><ul><li>[TestClass]– denotes a class holding unit tests
  58. 58. [TestMethod]– denotes a unit test method
  59. 59. [ExpectedException]– test causes an exception
  60. 60. [Timeout]– sets a timeout for test execution
  61. 61. [Ignore]– temporary ignored test case
  62. 62. [ClassInitialize], [ClassCleanup]– setup / cleanup logic for the testing class
  63. 63. [TestInitialize], [TestCleanup]– setup / cleanup logic for each test case</li></li></ul><li>Assertions<br />Predicate is a true / false statement <br />Assertion is a predicate placed in the program code <br />Indicates that the developer thinks that the predicate is always true at that place<br />If an assertion fails, the method call does not return and an error is reported<br />Example:<br />Assert.AreEqual(expectedValue, actualValue, <br />"Error message.");<br />
  64. 64. VSTT – Assertions<br />Assertions check condition and throw exception if condition is not satisfied<br />Comparing values<br />AreEqual(expected value, calculated value [,message]) – compare two values for equality<br />Comparing objects<br />AreSame(expected object, current object [,message])– compare object references<br />
  65. 65. VSTT – Assertions (2)<br />Checking for null value<br />IsNull(object [,message])<br />IsNotNull(object [,message])<br />Conditions<br />IsTrue(condition)<br />IsFalse(condition)<br />Forced test fail<br />Fail(message)<br />
  66. 66. The 3A Pattern<br />Arrange all necessary preconditions and inputs<br />Act on the object or method under test<br />Assert that the expected results have occurred<br />[TestMethod]<br />public void TestDeposit()<br />{<br />BanckAccount account = new BanckAccount();<br />account.Deposit(125.0);<br />account.Deposit(25.0);<br />Assert.AreEqual(150.0, account.Balance, <br />"Balance is wrong.");<br />}<br />
  67. 67. VSTT – Example<br />public class Account<br />{<br />private decimal balance;<br />public void Deposit(decimal amount)<br />{<br /> this.balance += amount;<br />}<br />public void Withdraw(decimal amount)<br />{<br /> this.balance -= amount;<br />}<br />public void TransferFunds(<br />Account destination, decimal amount)<br /> { ... }<br />public decimal Balance<br />{ ... }<br />}<br />
  68. 68. VSTT – Example (2)<br />using Microsoft.VisualStudio.TestTools.UnitTesting;<br />[TestClass]<br />public class AccountTest<br />{<br /> [TestMethod]<br /> public void TransferFunds()<br /> {<br /> Account source = new Account();<br />source.Deposit(200.00M);<br /> Account dest = new Account();<br /> dest.Deposit(150.00F);<br /> source.TransferFunds(dest, 100.00F);<br /> Assert.AreEqual(250.00F, dest.Balance);<br /> Assert.AreEqual(100.00F, source.Balance);<br /> }<br />}<br />
  69. 69. VSTT – Screenshot<br />
  70. 70. Visual Studio Team Test<br />Live Demo<br />
  71. 71. Unit Testing Best Practices<br />
  72. 72. Naming Standards for Unit Tests<br />The test name should express a specific requirement that is tested<br />Usually prefixed with [Test]<br />E.g. TestAccountDepositNegativeSum()<br />The test name should include<br />Expected input or state <br />Expected result output or state<br />Name of the tested method or class<br />
  73. 73. Naming Standards for Unit Tests Example<br />Given the method:<br /> with requirement to ignore numbers greater than 100 in the summing process<br />The test name should be:<br />Sum_NumberIgnoredIfGreaterThan100<br />public int Sum(params int[] values)<br />
  74. 74. When Should a Test be Changed or Removed?<br />Generally, a passing test should never be removed<br />These tests make sure that code changes don’t break working code<br />A passing test should only be changed to make it more readable<br />When failing tests don’t pass, it usually means there are conflicting requirements<br />
  75. 75. When Should a Test be Changed or Removed? (2)<br />Example:<br />New features allows negative numbers<br />[ExpectedException(typeof(Exception), <br /> "Negatives not allowed")]<br />void Sum_FirstNegativeNumberThrowsException()<br />{<br /> Sum (-1,1,2);<br />}<br />
  76. 76. When Should a Test be Changed or Removed?(3)<br />New developer writes the following test:<br />Earlier test fails due to a requirement change<br />void Sum_FirstNegativeNumberCalculatesCorrectly()<br />{<br />int sumResult = sum(-1, 1, 2);<br />Assert.AreEqual(2, sumResult);<br />}<br />
  77. 77. When Should a Test be Changed or Removed? (4)<br />Two course of actions:<br />Delete the failing test after verifying if it’s valid<br />Change the old test:<br />Either testing the new requirement<br />Or test the older requirement under new settings<br />
  78. 78. Tests Should Reflect Required Reality<br />What’s wrong with the following test?<br />A failing test should prove that there is something wrong with the production code<br />Not with the unit test code<br />int Sum(int a, int b) –> returns sum of a and b<br />public void Sum_AddsOneAndTwo()<br />{<br /> int result = Sum(1,2);<br /> Assert.AreEqual(4, result, "Bad sum");<br />}<br />
  79. 79. What Should Assert Messages Say?<br />Assert message in a test is one of the most important things<br />Tells us what we expected to happen but didn’t, and what happened instead<br />Good assert message helps us track bugs and understand unit tests more easily<br />Example:<br />"Withdrawal failed: accounts are not supposed to have negative balance."<br />
  80. 80. What Should Assert Messages Say? (2)<br />Express what should have happened and what did nothappen<br />“Verify() did not throw any exception”<br />“Connect() did not open the connection before returning it”<br />Do not:<br />Provide empty or meaningless messages <br />Provide messages that repeat the name of the test case<br />
  81. 81. Avoid Multiple Asserts in a Single Unit Test<br />void Sum_AnyParamBiggerThan1000IsNotSummed() <br />{<br /> Assert.AreEqual(3, Sum(1001, 1, 2);<br /> Assert.AreEqual(3, Sum(1, 1001, 2);<br /> Assert.AreEqual(3, Sum(1, 2, 1001);<br />}<br />Avoid multiple asserts in a single test case<br />If the first assert fails, the test execution stops for this test case<br />Affect future coders to add assertions to test rather than introducing a new one<br />
  82. 82. Unit Testing – The Challenge<br />The concept of Unit Testing has been around for many years<br />New methodologies in particular XP, have turned unit testing into a cardinal foundation of software development<br />Writing good & effective Unit Tests is hard!<br />This is where supporting integrated tools and suggested guidelines enter the picture<br />The ultimate goal is tools that generate unit tests automatically<br />
  83. 83. What is a load test?<br /><ul><li>An automated collection of tests run in parallel to simulate multiple users or systems
  84. 84. Load tests run software unit tests, database unit tests, and web performance tests
  85. 85. Visual Studio captures details metrics while the load test runs</li></li></ul><li>Load Test Concepts<br />Scenario<br /><ul><li>A collection of load test parameters that simulate real world scenario.</li></ul>Load Pattern<br /><ul><li>Determines the number of users the scenario simulates. May be constant or stepped to simulate an increasing number of users</li></ul>Test Mix<br /><ul><li>Determines the unit tests and web tests.</li></ul>Network Mix<br /><ul><li>Determines the connection speeds used by virtual users</li></ul>Run Settings<br /><ul><li>A set of properties that determine the way the load test runs</li></li></ul><li>Core Scenarios<br /><ul><li>Single user performance testing
  86. 86. Create and debug Web Performance Tests
  87. 87. Use Coded UI tests to measure end user performance
  88. 88. Run performance tests
  89. 89. Analyze results
  90. 90. Report to stakeholders
  91. 91. Fix the problems using the profiler</li></li></ul><li>Network Emulation<br /><ul><li>Productizing NEWT network device driver
  92. 92. Throttles bandwidth, introduces latency and errors at the network layer
  93. 93. Scenarios
  94. 94. Single user testing over slow network
  95. 95. Automated performance testing over slow links</li></li></ul><li>Web Performance Test Recorder<br />Visual Studio Launches Internet Explorer<br />RecorderListener<br />Browser Recorder<br />Wininet Recorder<br />Merging Recorder<br />Internet Explorer<br />Visual Studio<br />Recorder BHO<br />Browser events<br />Record events<br />Browser Control<br />Wininet Shim<br />Record events<br />Wininet<br />Record events<br />Intercepts wininet calls<br />
  96. 96. Web Test Recorder Plugin<br />WebTestRecorderPlugins<br />Visual Studio Launches Internet Explorer<br />RecorderListener<br />Browser Recorder<br />Wininet Recorder<br />Merging Recorder<br />Internet Explorer<br />Visual Studio<br />Recorder BHO<br />Browser events<br />Recorded Web Test<br />Record events<br />Browser Control<br />Wininet Shim<br />Recorder Result<br />Wininet<br />Record events<br />Intercept s wininet calls<br />Record events<br />
  97. 97. Load Test Agent Architecture<br />Test agents run tests and collect data<br />Test agent #1<br />Test agent #2<br />Test agent #3<br />Test Agent<br />Run Tests<br />Visual Studio Ultimate 2010<br />Test Controller<br />Target Server<br />Collector<br />Agent<br />Asp.Net<br />Profiler<br />Test Controller manages test agents<br />
  98. 98. Visual Studio Team Test<br />Live Demo<br />
  99. 99. ?<br />?<br />?<br />?<br />?<br />Questions?<br />?<br />?<br />?<br />?<br />?<br />
  100. 100. Patrocinadores desta reunião<br />
  101. 101. Próximas reuniões presenciais<br />09/04/2011 - Abril (Coimbra)<br />16/04/2011 - Abril (Lisboa)<br />21/05/2011 - Maio (Lisboa)<br />18/06/2011 - Junho (Lisboa)Reserva estes dias na agenda! :)<br />
  102. 102. Obrigado!<br />Pedro Rosa<br />http://pt.linkedin.com/in/pedrobarraurosa<br />http://twitter.com/pedrorosa<br />

×