Visual Studio For Mac Debuggung Unit Tests

Use the Visual Studio debugger to quickly find and fix bugs across languages. The Visual Studio for Mac debugger lets you step inside your code by setting Breakpoints, Step Over statements, Step Into and Out of functions, and inspect the current state of the code stack through powerful visualizations. Go to 'Menu' - 'View' - 'Test'. Find your test. Right click on it. And 'Debug test'. Enter image description here.

-->

The Node.js Tools For Visual Studio allow you to write and run unit tests using some of the more popularJavaScript frameworks without the need to switch to a command prompt.

The supported frameworks are:

  • Mocha (mochajs.org)
  • Jasmine (Jasmine.github.io)
  • Tape (github.com/substack/tape)
  • Jest (jestjs.io)
  • Export Runner (this framework is specific to Node.js Tools for Visual Studio)

Warning

An issue in Tape currently prevents Tape tests from running. If PR #361 is merged, the issue should be resolved.

If your favorite framework is not supported, see Add support for a unit test framework for information on adding support.

Write unit tests

Before adding unit tests to your project, make sure the framework you plan to use is installed locally in your project. This is easy to do using the npm package installation window.

The preferred way to add unit tests to your project is by creating a tests folder inyour project, and setting that as the test root in project properties. You also needto select the test framework you want to use.

You can add simple blank tests to your project, using the Add New Item dialog box. Both JavaScript and TypeScript are supported in the same project.

For a Mocha unit test, use the following code:

If you haven't set the unit test options in the project properties, you must ensure the Test Frameworkproperty in the Properties window is set to the correct test framework for your unit test files. This isdone automatically by the unit test file templates.

Note

The unit test options will take preference over the settings for individual files.

It’s a command-line utility that allows you to install, reinstall, or uninstall with a simple and straightforward command: pip.If you’ve ever done any command-line work on Windows or Mac or Linux , then you can skip down to the installation instructions for your particular operating system. What Is PIP for Python?PIP is an acronym that stands for “PIP Installs Packages” or “Preferred Installer Program”. Install pip python 3.7. Is PIP Installed With Python?If you’re using Python 2.7.9 (or greater) or Python 3.4 (or greater), then PIP comes installed with Python by default. If you’re using an older version of Python, you’ll need to use the installation steps below.

Visual Studio For Mac Debuggung Unit Tests

After opening Test Explorer (choose Test > Windows > Test Explorer), Visual Studio discovers and displays tests. If tests are not showing initially, then rebuild the project to refresh the list.

Note

Do not use the outdir or outfile option in tsconfig.json, because Test Explorer won't be able to find your unit tests in TypeScript files.

Run tests

You can run tests in Visual Studio 2017 or from the command line.

Run tests in Visual Studio 2017

You can run the tests by clicking the Run All link in Test Explorer. Or, you can run tests by selecting one or more tests or groups, right-clicking, and selecting Run Selected Tests from the shortcut menu. Tests run in the background, and Test Explorer automatically updates and shows the results. Furthermore, you can also debug selected tests by selecting Debug Selected Tests.

Warning

Debugging unit tests using Node 8+ currently only works for JavaScript test files,TypeScript test files will fail to hit breakpoints. As a workaround use the debugger keyword.

Note

We don't currently support profiling tests, or code coverage.

Run tests from the command line

You can run the tests from the Developer Command Prompt for Visual Studio 2017 using the following command:

This command shows output similar to the following:

Note

If you get an error indicating that vstest.console.exe cannot be found, make sure you've opened the Developer Command Prompt and not a regular command prompt.

Add support for a unit test framework

You can add support for additional test frameworks by implementing the discovery and execution logic using JavaScript. You do this by adding a folder with the name of the test framework under:

<VisualStudioFolder>Common7IDEExtensionsMicrosoftNodeJsToolsTestAdapterTestFrameworks

This folder has to contain a JavaScript file with the same name which exports the following two functions:

  • find_tests
  • run_tests

For good a example of the find_tests and the run_tests implementations, see the implementation for the Mochaunit testing framework in:

<VisualStudioFolder>Common7IDEExtensionsMicrosoftNodeJsToolsTestAdapterTestFrameworksmochamocha.js

Discovery of available test frameworks occurs at Visual Studio start. If a framework is added whileVisual Studio is running, restart Visual Studio to detect the framework. However you don't need to restartwhen making changes to the implementation.

Unit tests in other project types

You are not limited to writing unit tests in just your Node.js projects. When you add the TestFramework andTestRoot properties to any C# or Visual Basic project, those tests will be enumerated and you can run them usingthe Test Explorer window.

To enable this, right-click the project node in the Solution Explorer, choose Unload Project, and then choose Edit Project. Then in the project file, add the following two elements to a property group.

Note

Make sure that the property group you're adding the elements to doesn't have a condition specified.This can cause unexpected behavior.

Next, add your tests to the test root folder you specified, and they will be available to run in theTest Explorer window. If they don't initially appear, you may need to rebuild the project.

Unit test .NET Core and .NET Standard

In addition to the properties above, you will also need to install the NuGet package Microsoft.JavaScript.UnitTest and set the property:

Some test frameworks may require additional npm packages for test detection. For example, jest requires the jest-editor-support npm package. If necessary, check the documentation for the specific framework.

Since we upgraded to newer versions of VS for Mac we are having problems running and debugging unit tests.

Are unit tests are written using NUnit 3 as .NET Core console applications.

The VS version information is:

Visual Studio Community 2019 for Mac
Version 8.2.1 (build 19)
Installation UUID: 095bc177-91e0-430d-9493-c9a96fe0c9ce
GTK+ 2.24.23 (Raleigh theme)
Xamarin.Mac 5.14.0.85 (d16-2-fix-mac-updates / 86973979)
Package version: 600000311
Mono Framework MDK
Runtime:
Mono 6.0.0.311 (2019-02/494641b300c) (64-bit)
Package version: 600000311
NuGet
Version: 5.1.0.6013
.NET Core
Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
2.2.6
2.2.2
2.1.9
2.1.2
SDK: /usr/local/share/dotnet/sdk/2.2.301/Sdks
SDK Versions:
2.2.301
2.2.104
2.1.505
2.1.302
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/6.0.0/lib/mono/msbuild/Current/bin/Sdks

We have also seen similar issues using 8.1.5 but not on 8.0.6.

We either see test projects fail to load, the run and debug options being greyed out or simply not in the menu at all.