Chapter 6. Unit Testing

ReSharper comes with a very user-friendly test runner which, by default, supports tests written with NUnit and MSTest as well as tests written for JavaScript.

As with any ReSharper feature, support for unit tests looks the same in all supported Visual Studio versions. Thanks to this, you can easily run NUnit and JavaScript unit tests in Visual Studio versions that only support MSTest.

In this chapter, we will cover the following topics:

  • Test runner
  • Testing JavaScript
  • Analyzing code coverage with dotCover

Test runner

ReSharper provides you with the following two new windows that allow you to work with unit tests:

  • Unit Test Explorer: To open this window, navigate to RESHARPER | Windows | Unit Tests from the Visual Studio menu or use the Ctrl + Alt + U shortcut
  • Unit Test Sessions: To open this window, navigate to RESHARPER | Windows | Unit Test Sessions from the Visual Studio menu or use the Ctrl + Alt + T shortcut

Additionally, for new windows, the ReSharper test runner is integrated with the Code Editor in Visual Studio. It adds a new icon for every class that contains unit tests as well as for each particular test method, as shown in the following screenshot:

Test runner

The first icon allows you to quickly run or debug all the tests in a class, and the second one allows you to run or debug particular tests.

Depending on the other tools that are installed, ReSharper also allows you to use other options such as profiling tests with dotTrace or checking code coverage with dotCover.

By default, ReSharper v8.1 comes with support for NUnit 2.6.3. This version of NUnit is built-in ReSharper and is used to run your tests. If you would like to use a different version of NUnit, you need to go to the NUnit configuration in ReSharper (RESHARPER | Options | Tools | Unit Testing | NUnit). These options are shown in the following screenshot:

Test runner

ReSharper can also work with NUnit plugins—to use them, you need to put them into the folder shown in the previous screenshot.

ReSharper can also work with other unit test frameworks by installing proper plugins. A list of available plugins is present on the page at https://resharper-plugins.jetbrains.com/packages?q=Tags:"unittest".

The Unit Test Explorer window

The Unit Test Explorer window displays all the unit tests found in your solution. ReSharper can automatically recognize the tests from supported frameworks.

This window also provides you with the same options as the menu that is integrated with the Code Editor, and allows you to run or debug tests and add tests to sessions.

A sample view of Unit Test Explorer is shown in the following screenshot:

The Unit Test Explorer window

Apart from the menu options and icons shown in the preceding screenshot, you can run tests via shortcuts. Use Ctrl + U, R to run tests or Ctrl + U, D to debug them.

The Unit Test Sessions window

ReSharper runs all unit test in sessions. You can think about sessions as groups. When you click on the Run Test option, ReSharper automatically creates a new session for you. You can add as many sessions as you need and configure the tests that these sessions contain. This allows you to group unit tests according to your needs. For example, in one session, you can have a test for your routing table in ASP.NET MVC and in another session, you can have tests related to your business logic. Thanks to this, you can easily run the needed tests together even if they are placed in different files or projects.

Tip

You can quickly run unit tests from the current session by using the Ctrl + U, Y shortcut or all unit tests in the solution by using the Ctrl + U, L shortcut.

The Unit Test Sessions window is also the place where ReSharper displays test results. To make it easier to analyze failed tests, ReSharper displays the code of the failed tests and allows you to quickly navigate to the place at which the test failed, as shown in the following screenshot:

The Unit Test Sessions window

As with many other windows, in ReSharper, you can group and filter the displayed tests based on your needs. ReSharper also allows you to select the platform and .NET Framework version on which you would like to run the tests, as shown in the following screenshot:

The Unit Test Sessions window
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.247.53