The 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.
Important Note: 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.
To update an existing project...
If 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 Dependencies and select Manage NuGet Packages...
You should see a screen similar to this one:
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:
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>