• 4 heures
  • Facile

Ce cours est visible gratuitement en ligne.

course.header.alt.is_video

course.header.alt.is_certifying

J'ai tout compris !

Mis à jour le 28/03/2024

Run Unit Tests

As you write applications, you naturally begin to write and change code over time. How do you ensure that what you are adding or changing doesn't break something else in your code? Also, if you're working with a team of developers on the same application, you may not know that the code you just modified broke someone else's code.

Unit tests

This is where unit tests come into play. Unit testing is the process where you test the smallest functional parts of your application, a unit, and verify that it's still working.

Let's take a look at how you can manage unit tests with Visual Studio. Notice in the Solution Explorer that we have a project called HelloWorld.Tests, which contains one test class called CheckingAccountTests.cs

Tests in the Solution Explorer
Tests in the Solution Explorer

test  class contains test methods that are run (executed) as a test or collection of tests. The figure below shows an example of how to run all the unit tests from the Main menu. 

Run all Unit Tests from the Main menu
Run all unit tests from the main menu

As you run the unit tests, the results appear in the Test Explorer window as shown below. Successful tests are shown in green while failed tests appear in red. 

Test results appear in the Test Explorer
Test results appear in the Test Explorer

In the video below, you'll see a specific test method in the CheckingAccountTests class. Then, we will modify the code to see how a failed test appears and discover how to resolve the issue. 

Test execution reports

A test execution report is a document which summarizes test activities and final test results. It is important to be able to communicate the results effectively so decisions can be made for the software release or other earlier steps in an application's life cycle (i.e., if further testing is required).

To print or save a test execution report:

  • Click on HelloWorld.Tests which is highlighted in the figure below.

  • Click on the Copy All link to copy the report to the clipboard.

Test Explorer
Test Explorer
  • Paste into either an email or any text editor as seen below.

Paste results into a document
Paste results into a document

Let's recap!

  • Unit tests help test code for issues and prevent breakage of previously written code.

  • A unit corresponds to a single functionality of code and is the smallest part of our application that can be tested.

  • The Test Explorer window displays all unit tests available for an application.

  • Unit tests are contained in a separate project file.

  • A test execution report serves to communicate the results of test activities.

Exemple de certificat de réussite
Exemple de certificat de réussite