Dotnet Visual Studio Code



This guide will walk you through the set-up process for building WebAssembly apps with Uno under Windows, Linux, or macOS.

  1. Once you have those configured, open up Visual Studio code and navigate to the debug tab. Click on the little gear icon to create a new launch configuration — select “.Net Core” from the.
  2. The Visual Studio Code notebook support that this extension uses is also in preview and design is ongoing, so the extension might not work.NET Interactive Notebooks. This extension adds support for using.NET Interactive in a Visual Studio Code notebook. Getting Started. Install the latest Visual Studio Code. Install the latest.NET 5 SDK.
  3. Dotnet, A Visual Studio Code extension A modern interface to the dotnet cli for Visual Studio Code. This extension provides access to the dotnet cli by using the Quick Pick UI.

Prerequisites

  • .NET Core SDK

    • .NET Core 3.1 SDK (version 3.1.8 (SDK 3.1.402) or later)
    • .NET Core 5.0 SDK (version 5.0 (SDK 5.0.100) or later)

    Use dotnet --version from the terminal to get the version installed.

You will need to restart Visual Studio after building the source generator to make errors go away and IntelliSense appear for generated source code. After you do that, things will work. Currently, Visual Studio integration is very early on. This current behavior will change in the future so that you don’t need to restart Visual Studio.

Create an Uno Platform project

Install Uno Platform Template

Dotnet

Launch Visual Studio Code and open a new terminal.

In the terminal, type the following to install the Uno Platform templates:

Create the project

In the terminal, type the following to create a new project:

MyApp is the name you want to give to your project.

This will create a solution that only contains the WebAssembly platform support.

Prepare the WebAssembly application for debugging

  1. In Visual Studio Code install these extensions:

    Configure this extension with the debug.javascript.usePreview setting set to true. (File -> Preference -> Settings and search for Use preview).

  2. Open the project using Visual Studio Code. In the terminal type

    For this command to work you need to previously have configured Visual Studio Code to be launched from the terminal.

  3. Visual Studio Code will ask to restore the NuGet packages.

Modify the template

  1. In MainPage.xaml, replace the Grid's content with the following:

  2. In your MainPage.xaml.cs, add the following method:

Run and Debug the application

  1. Starting the app with the WebAssembly debugger is a two-step process. Move to the Run tab on Visual Studio Code and

    • Start the app first using the .NET Core Launch (Uno Platform App) launch configuration

    • Then start the browser using the .NET Core Debug Uno Platform WebAssembly in Chrome launch configuration (requires Chrome).

      To use the latest stable release of Edge instead of Chrome, change the type of the launch configuration in .vscode/launch.json from pwa-chrome to pwa-msedge

  2. Place a breakpoint inside the OnClick method

  3. Click the button in the app, and the breakpoint will hit

Updating an existing application to work with VS Code

If you already have an Uno application, you can add some missing support files for VS Code to recognize your project.

Here's how to do this:

  1. Use the same command line above to create a project with the same name as your current project, in a different location.

  2. Once created, copy the generated .vscode folder next to your .sln file

  3. Update the Uno.UI package version to the latest stable version

  4. Update the Uno.Wasm.Bootstrap package to 1.3.4 or later version

  5. Add a reference to Uno.Wasm.Bootstrap.DevServer version 1.3.4 or later.

  6. In your Wasm project file, if you had a <DotNetCliToolReference /> line, remove it

  7. Restore your nuget packages

You should now be in the same configuration as the generated template files.

Video Tutorial

-->

This tutorial shows how to automate unit testing by adding a test project to a solution.

Prerequisites

  • This tutorial works with the solution that you create in Create a .NET class library using Visual Studio Code.

Create a unit test project

Unit tests provide automated software testing during your development and publishing. The testing framework that you use in this tutorial is MSTest. MSTest is one of three test frameworks you can choose from. The others are xUnit and nUnit.

  1. Start Visual Studio Code.

  2. Open the ClassLibraryProjects solution you created in Create a .NET class library using Visual Studio Code.

  3. Create a unit test project named 'StringLibraryTest'.

    The project template creates a UnitTest1.cs file with the following code:

    The source code created by the unit test template does the following:

    • It imports the Microsoft.VisualStudio.TestTools.UnitTesting namespace, which contains the types used for unit testing.
    • It applies the TestClassAttribute attribute to the UnitTest1 class.
    • It applies the TestMethodAttribute attribute to define TestMethod1.

    Each method tagged with [TestMethod] in a test class tagged with [TestClass] is executed automatically when the unit test is run.

  4. Add the test project to the solution.

Add a project reference

For the test project to work with the StringLibrary class, add a reference in the StringLibraryTest project to the StringLibrary project.

Studio
  1. Run the following command:

Add and run unit test methods

When Visual Studio runs a unit test, it executes each method that is marked with the TestMethodAttribute attribute in a class that is marked with the TestClassAttribute attribute. A test method ends when the first failure is found or when all tests contained in the method have succeeded.

The most common tests call members of the Assert class. Many assert methods include at least two parameters, one of which is the expected test result and the other of which is the actual test result. Some of the Assert class's most frequently called methods are shown in the following table:

Assert methodsFunction
Assert.AreEqualVerifies that two values or objects are equal. The assert fails if the values or objects aren't equal.
Assert.AreSameVerifies that two object variables refer to the same object. The assert fails if the variables refer to different objects.
Assert.IsFalseVerifies that a condition is false. The assert fails if the condition is true.
Assert.IsNotNullVerifies that an object isn't null. The assert fails if the object is null.
Visual

You can also use the Assert.ThrowsException method in a test method to indicate the type of exception it's expected to throw. The test fails if the specified exception isn't thrown.

In testing the StringLibrary.StartsWithUpper method, you want to provide a number of strings that begin with an uppercase character. You expect the method to return true in these cases, so you can call the Assert.IsTrue method. Similarly, you want to provide a number of strings that begin with something other than an uppercase character. You expect the method to return false in these cases, so you can call the Assert.IsFalse method.

Since your library method handles strings, you also want to make sure that it successfully handles an empty string (String.Empty) and a null string. An empty string is one that has no characters and whose Length is 0. A null string is one that hasn't been initialized. You can call StartsWithUpper directly as a static method and pass a single String argument. Or you can call StartsWithUpper as an extension method on a string variable assigned to null.

You'll define three methods, each of which calls an Assert method for each element in a string array. You'll call a method overload that lets you specify an error message to be displayed in case of test failure. The message identifies the string that caused the failure.

To create the test methods:

  1. Open StringLibraryTest/UnitTest1.cs and replace all of the code with the following code.

    The test of uppercase characters in the TestStartsWithUpper method includes the Greek capital letter alpha (U+0391) and the Cyrillic capital letter EM (U+041C). The test of lowercase characters in the TestDoesNotStartWithUpper method includes the Greek small letter alpha (U+03B1) and the Cyrillic small letter Ghe (U+0433).

  2. Save your changes.

  3. Run the tests:

    The terminal output shows that all tests passed.

Handle test failures

If you're doing test-driven development (TDD), you write tests first and they fail the first time you run them. Then you add code to the app that makes the test succeed. For this tutorial, you created the test after writing the app code that it validates, so you haven't seen the test fail. To validate that a test fails when you expect it to fail, add an invalid value to the test input.

  1. Modify the words array in the TestDoesNotStartWithUpper method to include the string 'Error'.

  2. Run the tests:

    The terminal output shows that one test fails, and it provides an error message for the failed test: 'Assert.IsFalse failed. Expected for 'Error': false; actual: True'. Because of the failure, no strings in the array after 'Error' were tested.

  3. Remove the string 'Error' that you added in step 1. Rerun the test and the tests pass.

Test the Release version of the library

Now that the tests have all passed when running the Debug build of the library, run the tests an additional time against the Release build of the library. A number of factors, including compiler optimizations, can sometimes produce different behavior between Debug and Release builds.

  1. Run the tests with the Release build configuration:

    The tests pass.

Debug tests

If you're using Visual Studio Code as your IDE, you can use the same process shown in Debug a .NET console application using Visual Studio Code to debug code using your unit test project. Instead of starting the ShowCase app project, open StringLibraryTest/UnitTest1.cs, and select Run All Tests between lines 7 and 8. If you're unable to find it, press Ctrl+Shift+P to open the command palette and enter Reload Window.

Visual Studio Code starts the test project with the debugger attached. Execution will stop at any breakpoint you've added to the test project or the underlying library code.

Additional resources

Next steps

In this tutorial, you unit tested a class library. You can make the library available to others by publishing it to NuGet as a package. To learn how, follow a NuGet tutorial:

If you publish a library as a NuGet package, others can install and use it. To learn how, follow a NuGet tutorial:

Visual Studio Code Dotnet Console

A library doesn't have to be distributed as a package. It can be bundled with a console app that uses it. To learn how to publish a console app, see the earlier tutorial in this series: