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

Chapter 4 Unit Testing

Figure 4-8.  Test timeout exceeded

Identifying critical methods in your code and setting a specific timeout on that method will allow developers to catch issues early on when tests start exceeding the timeout set. You can then go back and immediately refactor the code that was recently changed to improve the execution time.

Using Live Unit Tests

First introduced in Visual Studio 2017, Live Unit Testing runs your unit tests automatically as you make changes to your code. You can then see the results of your unit tests in real time.

Live Unit Testing is only available in Visual Studio Enterprise edition for C# and Visual Basic projects targeting the .NET Framework or .NET Core. For a full comparison between the editions of Visual Studio, refer to the following link: https://visualstudio.microsoft.com/vs/compare/.

The benefits of Live Unit Testing are as follows:

•\ You will immediately see failing tests, allowing you to easily identify breaking code changes.

224

Chapter 4 Unit Testing

•\ It indicates Code Coverage, allowing you to see what code is not covered by any unit tests.

Live Unit Testing persists the data of the status of the tests it ran. It then uses the persisted data to dynamically run your tests as your code changes. Live Unit Testing supports the following test frameworks:

•\

xUnit.net – Minimum version xunit 1.9.2

•\

NUnit – Minimum version NUnit version 3.5.0

•\

MSTest – Minimum version MSTest.TestFramework 1.0.5-preview

Before you can start using Live Unit Testing, you need to configure it by going to Tools Options and selecting Live Unit Testing in the left pane (Figure 4-9).

Figure 4-9.  Configure Live Unit Testing

Once you have configured the Live Unit Testing options, you can enable it from TestLive Unit Testing Start. To see the Live Unit Testing window, click the Live Unit Testing button as seen in Figure 4-6.

225

Chapter 4 Unit Testing

The Live Unit Testing window is displayed as seen in Figure 4-10.

Figure 4-10.  Live Unit Testing window

Make some breaking changes to your code and save the file. You will see that the Live Unit Testing window is updated to display the failing tests as seen in Figure 4-11.

Figure 4-11.  Live Unit Testing results updated

Live Unit Testing gives you a good insight into the stability of the code you write, as you write the code. Let’s go a little further. Add the class in Listing 4-7 to your project under test.

226

Chapter 4 Unit Testing

Listing 4-7.  Container Class Implementing ICloneable

public class Container : ICloneable

{

public string ContainerNumber { get; set; } public string ShipNumber { get; set; } public double Weight { get; set; }

public object Clone() => throw new NotImplementedException();

}

Don’t add any implementation to the Clone method. Swing back to the test project and add a Unit Test for the Container class as in Listing 4-8.

Listing 4-8.  Unit Test for the Container Class

[TestMethod]

public void Test_Container()

{

var containerA = new Container(); var containerB = containerA.Clone();

var result = (containerA == containerB); Assert.IsFalse(result);

}

Start Live Unit Testing, and you will notice that your test fails as seen in Figure 4-12.

Figure 4-12.  Live Unit Test results failed

Have a look at the Container class, and you will notice that Live Unit Testing has also updated the code file with the faulting method (Figure 4-13).

227

Chapter 4 Unit Testing

Figure 4-13.  Container class Live Unit Test results

As soon as you add implementation to the Clone method, your Live Unit Test results are updated as seen in Figure 4-14.

Figure 4-14.  Implementing the Clone method

With Live Unit Testing, areas of code indicated by a dash are not covered by any tests. A green tick indicates that the code is covered by a passing test. A red X indicates that the code is covered by a failing test.

228