Visual Studio For Mac Unit Test

  1. Visual Studio For Mac Debug Unit Test
  2. Visual Studio For Mac
There are several unit test options available in Visual Studio 2019, but the decision to use MSTest for a particular project was out of my hands, so this post covers getting MSTest unit tests working on Visual Studio 2019 for Mac.

Unit Tests Pad success/fail glyphs not updated 0 Solution Mouse click way off (down) in latest update 1 Solution Visual Studio for Mac 2019 preview could not load unit tests in existing solution 2 Solution Firewall question repeatedly asked when running test on iOS simulator on Mac. Go ahead and create a regular class and add a couple tests against it. If you can't see the 'Unit Test' pane (or pad as they call it on the Mac), open it now: View / Pads / Unit Tests. You may need to click the build button (black triangle in upper-left) to see your new tests. Or just click the 'Run All' button in the Unit Tests pad.


Visual Studio For Mac Unit TestThe first step is to make sure you are using Visual Studio, and not confusing it with Visual Studio Code. The Visual Studio icon looks like this:
And the Visual Studio Code icon looks like this:

These instructions apply to Visual Studio (for Mac), not Visual Studio Code.
If you want to download a pre-loaded solution with a working example of an MSTest unit test, feel free to clone this GitHub repository and make any necessary changes. Replace the C# files with yours as needed (or just copy and paste contents). All of the Assert class methods can be found here; they are necessary to write other forms of unit test functions.
MacImportant Note:C++ If you clone the above repository, but have never added all of the NuGet testing plugins, the project will probably fail to build. The resolution is to follow the first half of the instructions below to install all of the necessary NuGet packages.

Visual Studio For Mac Debug Unit Test


To update an existing project...

Visual Studio For Mac Unit TestIf you run into trouble, it might help to clone the example repository above and compare your solution with mine.
Some guidance instructs you to create a separate sub-project under your project's solution to contain the unit test code. I suspect those instructions work properly for Visual Studio running on Windows, but there seem to be namespace issues in Visual Studio for Mac that prevents that approach from working correctly.
The solution is to apply all of the following instructions to the solution that your source files are stored in and not create a separate sub-project to contain the unit tests.
Add the proper unit test packages via NuGet. <ctrl>-click DependenciesVisual studio for mac unit test free and select Manage NuGet Packages...



Use the search box in the upper right of that screen to find and add the following three packages:
  • Microsoft.NET.Test.Sdk
  • MSTest.TestAdapter
  • MSTest.TestFramework

Once you have added these packages, your solution will probably be broken with the following build message:
error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.
Many thanks to Andrew Lock for solving this one. His solution is geared towards xUnit, but it works in this case as well.
You should really read his incredibly detailed rundown; I did, and I learned a lot. But if you are short on time, the TL;DR is to add the following line of XML to any <PropertyGroup> section of your project's .csproj file:
<GenerateProgramFile>false</GenerateProgramFile>



And finally, to actually run the unit tests, select 'Run Unit Tests' from the 'Run' menu.

After you run the unit tests for the first time, you should have a button like this at the bottom of your IDE:

Visual Studio For Mac

Click 'Test Results' to display the test results browser and debug any test failures.