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.

Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010


Published on

Intro to the MSTest framework (aka. Visual Studio Unit Testing), some additional tools (e.g. Moq, Moles, White), how this is supported in Visual Studio, and how it integrates into the broader context of the TFS environment.

Published in: Technology
  • Excellent introduction walkthrough from basic test attributes through advanced mocking and a couple supporting test frameworks and tools.

    NOTE: VS2012 makes significant changes to test automation. One example is the move away from Test Lists (described in slides ~20-22). The rest is still fairly relevant.
    Are you sure you want to  Yes  No
    Your message goes here

Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

  1. 1. Getting to know MSTest<br />With Visual Studio 2010 and <br />Team Foundation Server 2010<br />Thomas Weller<br /><br /><br />July 2010<br />
  2. 2. Outline<br />Authoringtests (withMSTest)<br /> Visual Studio 2010<br /> Team Foundation Server 2010<br />Advancedtestingconcepts<br /> UI Automation<br />
  3. 3. What‘s in a test ?<br /><ul><li> Anatomy of a test class
  4. 4. What makes a good (unit) test...
  5. 5. Test context
  6. 6. Initalize/Cleanup
  7. 7. Attributes:
  8. 8. [ExpectedException]
  9. 9. [Description]
  10. 10. [TestProperty]
  11. 11. [TestCategory] </li></li></ul><li>Sample „test target“<br />
  12. 12. Anatomy of a test class (I)<br /><ul><li> Ordinary C# class with attributes:
  13. 13. Containing class: [TestClass]
  14. 14. Test: [TestMethod], public void, paramless</li></li></ul><li>Anatomy II: Assert class<br /><ul><li> To verify a fact:
  15. 15. Throws a special exception to fail a test
  16. 16. StringAssert, CollectionAssert …</li></li></ul><li>Anatomy III: AAA structure<br /><ul><li> General structure of a test method – AAA:
  17. 17. Arrange
  18. 18. Act
  19. 19. Assert</li></li></ul><li>What makes a good (unit) test…<br /><ul><li> verify only one fact
  20. 20. try to isolate code under test
  21. 21. short – simple – fast (CI) – readable
  22. 22. Test  documentation, usage example
  23. 23. nodependencies on other tests</li></ul> (random order!) <br />
  24. 24. TestContext class<br /><ul><li> run-time environment data (e.g. details, directories…)
  25. 25. WriteLine()for additional info in test results:</li></li></ul><li>TestContext class (II)<br /><ul><li> simply declare an automatic property:</li></li></ul><li>Inizalization and Cleanup<br /><ul><li> methods marked with attributes</li></ul> (xxxInitialize/xxxCleanup)<br /><ul><li> three levels:
  26. 26. all methods are optional</li></li></ul><li>[ExpectedException]<br /><ul><li> testing, that a certain exception is thrown</li></ul> by the method under test:<br /><ul><li> counterpart to Assert class, very frequently used
  27. 27. also Exception messages, if needed</li></li></ul><li>[Description]<br /><ul><li> short summary text (similar to xml comments):
  28. 28. shows up in the Properties window:</li></li></ul><li>[TestProperty]<br /><ul><li> arbitrary name/value pairs:
  29. 29. show up in the Properties window:</li></li></ul><li>[TestCategory]<br /><ul><li> assign (arbitrary) categories to a test:
  30. 30. filter by category on automated builds:</li></li></ul><li>[TestCategory]: Usage example<br /><ul><li> define some constants:
  31. 31. apply to test methods as appropriate:</li></li></ul><li>Visual Studio 2010<br /><ul><li> Test runconfiguration (*.testsettings)
  32. 32. Test lists/metadata (*.vsmdi)
  33. 33. Tool Windows:
  34. 34. Test Viewwindow
  35. 35. Test List Editor window
  36. 36. Test Resultswindow
  37. 37. Test Runs window
  38. 38. Test Results Details window</li></li></ul><li>Test run config (*.testsettings)<br /><ul><li> set of test run and environment settings etc.</li></ul> (e.g. naming schemes, directories, depl. scripts etc.)<br /><ul><li> *.testsettings  *.xml</li></li></ul><li>Test lists/metadata (*.vsmdi)<br /><ul><li> organizing / grouping tests with lists
  39. 39. e.g. for different test runs
  40. 40. similar use to that of TestCategory attribute
  41. 41. edit with Test List Editor window (see below)
  42. 42. *.vsmdi  *.xml</li></li></ul><li>Test View window<br /><ul><li> run, view, group, filter, select tests on different criteria</li></li></ul><li>Test List Editor window<br /><ul><li> manage, create, and organize test lists ( *.vsmdi)</li></li></ul><li>Test Results window<br /><ul><li> opens automatically when running a test inside VS</li></li></ul><li>Test Results Details window<br /><ul><li> test details + error information (e.g. Stack trace, logs, </li></ul> console output ...)<br />
  43. 43. Team Foundation Server 2010<br /><ul><li> Walkthrough
  44. 44. Creating a Bug work item
  45. 45. Creating the test
  46. 46. Creating a Test Case work item for the Bug
  47. 47. Associating test and work item
  48. 48. Check in
  49. 49. Tests and Automated Builds</li></li></ul><li>Creating a (Bug) work item (I)<br /><ul><li> Project Portal (Web):
  50. 50. Visual Studio (Team Explorer):</li></li></ul><li>Creating a (Bug) work item (II)<br />
  51. 51. Writing the test<br />
  52. 52. Create a Test Case for the Bug<br /><ul><li> In Visual Studio (on Bug work item):</li></li></ul><li>Associating test and work item<br /><ul><li> From Test View window:
  53. 53. From work item:</li></li></ul><li>Check in<br /><ul><li> on check-in:
  54. 54. version history:</li></li></ul><li>Tests and Automated Builds<br /><ul><li> CI run: only fast running tests</li></ul> Project „heartbeat“<br /><ul><li> Nightly run: longer running tests</li></ul> xml documentation<br /><ul><li> Manual: „Dev build“</li></ul> UI automation tests<br />ClickOnce<br />
  55. 55. Some more advanced concepts<br /><ul><li>Testing private code: PrivateObject/PrivateType
  56. 56. Testinginternalcode: InternalsVisibleTo
  57. 57. Mocking
  58. 58. „Moling“
  59. 59. Data-driventests: xml
  60. 60. Data-driventests: database</li></li></ul><li>PrivateObject/PrivateType<br /><ul><li> wrappers around reflection
  61. 61. PrivateType class  static elements</li></li></ul><li>PrivateObject : Example<br /><ul><li> method to test:
  62. 62. test method:</li></li></ul><li>InternalsVisibleTo<br /><ul><li> better alternative (no reflection)
  63. 63. in AssemblyInfo.cs:
  64. 64. test method:</li></li></ul><li>Mocking<br /><ul><li> Creating dummy objects on the fly
  65. 65. Isolating the tested code from dependencies
  66. 66. Specify arguments and return values
  67. 67. Verify method calls
  68. 68. Moq </li></ul><br /><br /><ul><li> Other (OS) alternatives: Rhino.Mocks, NMock... </li></li></ul><li>Mocking: Example (I)<br />
  69. 69. Mocking: Example (II)<br />
  70. 70. „Moling“<br /><ul><li> Mocking: Virtuals and Interface implementations
  71. 71. MS Moles: Replace anything with a delegate
  72. 72. Generates moles-assembly, uses runtime </li></ul> instrumentation (performance!)<br /><ul><li> Visual Studio Gallery download: </li></ul><br />
  73. 73. „Moling“: Example (I)<br /><ul><li>Add New Item ...:
  74. 74. Assembly references:</li></li></ul><li>„Moling“: Example (II)<br />
  75. 75. „Moling“: Example (III)<br /><ul><li> method to test:
  76. 76. test method:</li></li></ul><li>Data-driven tests: xml (I)<br /><ul><li> create an xml file:</li></li></ul><li>Data-driven tests: xml (II)<br /><ul><li> test method:
  77. 77. data accessible via TestContext</li></li></ul><li>Data-driven tests: xml (III)<br /><ul><li>Test Results Details window:</li></li></ul><li>Data-driven tests: database (I)<br /><ul><li> create a database/table:</li></li></ul><li>Data-driven tests: database (II)<br /><ul><li> Properties window </li></ul> (wizard) …<br /><ul><li> … or manually</li></li></ul><li>Automated UI testing<br /><ul><li> The White Framework
  78. 78. The recorder
  79. 79. UI Spy
  80. 80. UI object model
  81. 81. Example (Concurrency)
  82. 82. how to proceed…</li></li></ul><li>The White Framework<br /><ul><li>
  83. 83. .NET based, OS, by ThoughtWorks (CC.Net)
  84. 84. OO wrapper around MS UIAutomation library </li></li></ul><li>The recorder (alpha)<br /><ul><li> generates C# sourcecode</li></li></ul><li>UI Spy<br /><ul><li>SimilartoSpy++</li></ul><br /><ul><li> Windows SDK 6.0 </li></li></ul><li>UI object model (partial)<br />
  85. 85. Example - Concurrency (I)<br />
  86. 86. Example - Concurrency (II)<br />
  87. 87. …<br />?<br />