Visual Studio For Mac Unit Test

Jul 14, 2020 Visual Studio includes the Microsoft unit testing frameworks for both managed and native code. However, Test Explorer can also run any unit test framework that has implemented a Test Explorer adapter. For more information about installing third-party unit test frameworks, see Install third-party unit test frameworks Run tests in Test Explorer.

  1. Visual Studio For Mac Tutorial
  2. Visual Studio For Mac Review

Visual Studio For Mac Tutorial

Last month at the AkronCodeClub they selected the magic square kata, which was a new one for me. Basically, you arrange 9 unique numbers in a 3x3 grid such that they add up to the same number horizontally, vertically and diagonally. I paired up with someone else who knew C#, so it was a good opportunity to try doing the kata in Visual Studio for Mac!

  1. This article will help you learn how to more easily test Xamarin.Forms applications with MSTest based unit tests and Xamarin.UITest based UI tests. As I’ve covered in previous posts, it is not possible to cover all the code in mobile applications, as well as UI applications in general, with tests based on console Unit Tests.
  2. Apr 07, 2020 You can run unit tests in Visual Studio by using third-party test frameworks such as Boost, Google, and NUnit, depending on your programming language. To use a third-party framework: Use the NuGet Package Manager to install the NuGet package for the framework of your choice.
  3. Getting Started In Visual Studio For Mac. If you're using Visual Studio for Mac the NUnit templates extension can't be used. This guide assumes that you have a solution with either a PCL or a Shared project and a number of platform specific projects.
  4. After searching a while I figured that one need to install the extension called “xUnit.NET 2 testing framework support” from the Extension Gallery. After that one need to restart Visual Studio for Mac and do a full rebuild. The tests will then show up in the “Unit Tests” pad.

Although I've kicked the tires on VS4Mac a bit, one of the things I hadn't tried testing out was, well.. testing!

Method 1: An NUnit Library Project

Visual studio for mac unit testing

The easiest method is to just create a new 'NUnit Library Project'. The VS4Mac team actually added a project type that includes the NUnit package and a test file out of the box. How convenient is that??

Create a new project

Go to: File / New Solution / Other / .NET / NUnit Library Project

Create a class and some tests

Like I said, there's already a 'Test.cs' file ready to go, with the proper NUnit attributes and everything. Go ahead and create a regular class and add a couple tests against it.

Run the tests

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.

Now change the logic so the tests fail (if they didn't already) and you can see the failure results in the 'Test Results' pad at the bottom. If you don't see that pad, open it now: View / Pads / Test Results

That's it! If you're using VS4Mac for TDD during a code kata, it doesn't get much easier than that. :)

Method 2: Add NUnit to an Existing Project

Visual Studio For Mac Unit Test

But what if you already have a project and now you want to add tests to it? Let's start by creating a Library project to act as the 'existing project': File / New Solution / Other / .NET / Library

You should have a blank screen, along with the 'Solution' pad on the side of the screen. If you don't see that pad, go to: View / Pads / Solution

Create a Test File

Right-click your project and choose Add / New File. Select General / Empty Class and name it 'MagicSquareTests.cs'. I also repurposed the default 'MyClass.cs' as my MagicSquare class. You should end up with something like this:

Add the NUnit Package via NuGet

Right-click on Packages in the Solution pad and choose 'Add Packages'. All you need is NUnit - don't bother with the NUnit Console Runner.

You should see NUnit under the Packages folder.

Create a Few Tests

Add some new tests to run against whatever logic your old project has. In my case, I added a single function for the magic square kata, and wrote a couple tests against it that I was sure would fail.

The test runner tells you what failed and where.

Run / Observe / Fix / Repeat!

Try adding enough code to get your tests to pass, and run again.

More Reading

If you'd like, you can read more about what I've discovered... or just download Visual Studio for Mac and try it out yourself!

-->

Visual Studio for Mac testing tools can help you and your team develop and sustain high standards of code excellence. Unit tests can be written and run using the Microsoft unit test framework (MSTest), xUnit, or NUnit.

Creating tests

Visual Studio For Mac Review

To get started with testing, you can create a new test project in your solution by right-clicking your solution and choosing the Add > New Project... menu. Then choose one of the Test categories on the left-side of the dialog (For example, the Web and Console > Tests category). Select the type of test project you want to create, and click Next. Follow the instructions in the dialogs that appear and then a new test project will be added to your solution.

Note

For more information about unit testing your .NET Core applications and selecting unit test frameworks, see the Unit testing in .NET Core and .NET Standard documentation.

Running tests

The Unit Tests window is used to run unit tests and is opened using the View > Tests menu. Unit tests in your solution are automatically discovered and shown in this window, where you can run all of the tests or a set of tests that you've selected.

When editing a C# class that contains unit tests, you can run tests by right clicking in the test class or a test method and choosing the Run Test(s) or Debug Test(s) menu. Choosing the Run Test(s) menu item will run the tests in the test window, choosing the Debug Test(s) menu will do the same and attach the debugger so you can troubleshoot your code.

As tests are running, a Test Results window appears so you can review successful or failed tests, and the output from running those tests.

See also