Exercise 1: Unit Testing

In this exercise, you will learn about some of the improvements made to the unit testing story in Visual Studio 11, including performance and extensibility improvements.

  1. Log in as Julia. All user passwords are P2ssw0rd.
  2. Launch Visual Studio 11 from the shortcut on the taskbar or from Start | All Programs | Microsoft Visual Studio 11.

    Figure 1

    Starting Visual Studio 11

  3. Open the FabrikamFiber.CallCenter solution from the Dev branch in Source Control Explorer.

    Figure 2

    Source Control Explorer window

  4. Open the Unit Test Explorer window from Unit Test | Windows | Unit Test Explorer.

    Figure 3

    Unit Test Explorer window

  5. Select Build | Build Solution from the main menu to start the process of discovering new unit tests and code changes that may affect existing tests. Note that discovered tests are initially set to the Not Run state.

    Figure 4

    Discovered tests

  6. Select the Run All link within the Unit Test Explorer window.

    Figure 5

    Running all unit tests

    Note:
    Visual Studio 11 also provides Continuous Test Runner functionality that can be enabled with the Unit Test | Unit Test Settings | Run Test After Build option. With this option selected, tests will be ran after builds when necessary.

  7. Double-click on the test with the name CreateInsertsCustomerAndSaves to open the source code.

    Figure 6

    Navigating to a test method that uses MSTest

  8. In the CustomersControllerTest.cs file that opens, note that the test method uses the expected TestMethod attribute that is used by MSTest to mark unit tests.

    Figure 7

    MSTest unit test code

  9. In the Search box at the top of the Unit Test Explorer window, enter “index” and note the available search filters.

    Figure 8

    Searching within Unit Test Explorer

  10. Press the Enter key to perform the search.
  11. In the search results, double-click on the only test listed with the name IndexReturnsNonNullView to open the source code.

    Figure 9

    Navigating to a test method that uses XUnit

  12. In the HomeControllerTest.cs file that opens, you can see that the IndexReturnsNonNullView test actually uses the XUnit testing framework. The XUnit adapter is currently a prototype, but Microsoft is working with the XUnit team to create an official implementation that will work inside Visual Studio 11.

    Figure 10

    Example of a XUnit test in Visual Studio 11

  13. Select the Home button in Unit Test Explorer to view all tests.

    Figure 11

    Location of Home button

  14. Note that results are grouped by tests passed and failed, with execution times listed.

    Figure 12

    Test run results

  15. Test execution performance has been given some additional attention in Visual Studio 11. Take note of the execution times and then select the Run All link once again to see the difference in execution time.

    Note:
    The execution times that you see may be different from those shown in the screenshots. The first time you run unit tests they will execute more slowly than on subsequent runs as the tests and testing engine are being loaded for the first time.

    Figure 13

    Second test run without changes to tests is faster

  16. Select the failed test shown in the Unit Test Explorer window to view a summary view of the test results.

    Figure 14

    Selecting the failed test

    Note:
    You can also right-click on test results and use the Copy command to copy details to the clipboard. This would be useful when sending an email, for example.

  17. The summary view of the failed test run shows that an ArgumentNullException exception occurred during the test run and even provides the stack track at the time of the exception. Note that we can follow links to go directly to the source code for the test or to points within the stack trace. Select the source link to go to the source code for the test method.

    Figure 15

    Navigate to the source of the test method

    Figure 16

    Initial view after navigating to source of the test method

  18. Find the line of code that is commented out and un-comment it. Assume that this is the root cause for the failed test.

    Figure 17

    Fixing the root problem of the failed test

  19. Press Ctrl+S to save your changes.
  20. Right-click on the failed test in Unit Test Explorer and select Run Selected Tests to make sure that the problem is fixed.

    Figure 18

    Running the failed test

  21. Close the Unit Test Explorer window.