Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
strauss_d_getting_started_with_visual_studio_2022_learning_a.pdf
Скачиваний:
123
Добавлен:
26.06.2023
Размер:
17.04 Mб
Скачать

Chapter 4 Unit Testing

Figure 4-6.  Test Explorer menu

From the Test Explorer, you can

•\

Run all tests or just the last test

•\

Only run failed tests (great if you have many tests in your project)

•\

Filter the test results

•\

Group tests

•\

Start Live Unit Testing (more on this later)

•\

Create and run a test playlist

•\

Modify test settings

Let’s have a look at creating a test playlist.

Create and Run a Test Playlist

If your project contains many tests, and you want to run those tests as a group, you can create a playlist. To create a playlist, select the tests that you want to group from the Test Explorer, and right-click them. From the context menu that pops up, select Add to Playlist New Playlist as seen in Figure 4-7.

221

Chapter 4 Unit Testing

Figure 4-7.  Create a playlist

This will open a new Test Explorer window where you can run the tests and save the tests you selected under a new playlist name. This will create a .playlist file for you.

I created a new playlist called Temperature_Tests.playlist from the Celsius and Fahrenheit temperature conversion tests. The playlist file it creates is simply an XML file that in my example looks as in Listing 4-4.

Listing 4-4.  Temperature_Tests.playlist File Contents

<Playlist Version="1.0">

<Add Test="VisualStudioTests.ConversionHelperTests.Test_Fahrenheit_Calc" /> <Add Test="VisualStudioTests.ConversionHelperTests.Test_Celsius_Calc" /> </Playlist>

To open and run a playlist again, click the Create or run test playlist button and select the playlist file you want to run.

222

Chapter 4 Unit Testing

Testing Timeouts

The speed of your code is also very important. If you are using the MSTest framework, you can set a timeout attribute to set a timeout after which a test should fail. This is convenient because as you write code for a specific method, you can immediately identify if the code you are adding to a method is causing a potential bottleneck. Consider the Test_Fahrenheit_Calc test we created earlier.

Listing 4-5.  Adding a Timeout Attribute

[TestMethod]

[Timeout(2000)]

public void Test_Fahrenheit_Calc()

{

//arrange - setup var celsius = -7.0;

var expectedFahrenheit = 19.4;

//act - test

var result = ConversionHelpers.ToFahrenheit(celsius); // assert - check Assert.AreEqual(expectedFahrenheit, result);

}

As seen in Listing 4-5, I have added a timeout of 2000 milliseconds. If you run your tests now, it will pass because the calculation it performs is all it does. To see the timeout attribute in action, swing back to the ToFahrenheit method in the ConversionHelpers class and modify it by sleeping the thread for 2.5 seconds as seen in Listing 4-6.

Listing 4-6.  Sleeping the Thread

public static double ToFahrenheit(double celsius)

{

Thread.Sleep(2500);

return celsius * F_MULTIPLIER + F_ADDITION;

}

Run your tests again and see that, this time, your test has failed because it has exceeded the specified timeout value set by the Timeout attribute (Figure 4-8).

223