Visual Studio For Mac Unit Test

Visual studio

[.NET Core]

To get started, you can create a file named xunit.runsettings in your unit test project to hold the same settings you use in Visual Studio. However, the file name must match exactly (as Visual Studio for Mac and MonoDevelop do not allow you to select any.runsettings file, so I decide to use a hardcoded name). Visual Studio App Center. Pricing Documentation Blog. Sign In 🚀 Get started 👋 Welcome to App Center. Continuously build, test, release, and monitor apps for every platform. App Center is awesome for. Automatically run unit tests, release to testers and stores, or test your UI on real devices. Start writing unit test with Visual Studio for Mac. Hi everyone, long time no see. This is my first writing medium in English. If you have any suggestion. Please feel free let me know.

By Andrew Hall & Joe Morris | September 2017

Hopefully you’ve heard by now that the Microsoft .NET isn’t just for Windows anymore. .NET Core brings the ability to write applications that run on any OS you choose (Windows, macOS or Linux) using the language you prefer (C#, Visual Basic or F#). Of course, with the ability to write applications targeting the platform of your choice, you expect the same great developer tools that have always been a hallmark of .NET development. The great news is, as our supported platforms have grown, so have the tools available for creating great .NET applications.

In this article, we’ll walk you through the development tools available based on your personal work style and OS. We’ll start with the command-line tools included with the SDK that you can pair with any editor (although we recommend Visual Studio Code), then we’ll show the great new capabilities of Visual Studio 2017 before introducing you to our newest member of the Visual Studio family, Visual Studio for Mac. Finally, we’ll conclude by showing how these tools empower you to leverage software containers and try your apps seamlessly in the Microsoft Azure cloud.

Basic Concepts

Studio

For the purposes of this article, we’ll use the term “.NET Core” every­where for the sake of consistency, but there are two important technologies covered by this name: .NET Core and .NET Standard. .NET Core is the cross-platform runtime that executes applications. .NET Standard is a compiler-enforced set of APIs that are used as a target for class libraries, so a single library can run on any runtime that supports the standard. For example, .NET Core 2.0, .NET Framework 4.6.1 and Mono 4.8 support .NET Standard 2.0 (and previous) class libraries. For a better understanding of this concept, see the companion article, “Demystifying .NET Core and .NET Standard” in this issue.

It’s also worth noting the languages supported by .NET Core and .NET Standard 2.0. At a high level, both technologies support C#, Visual Basic and F# in 2.0. However, our aim with Visual Basic this release was to enable .NET Standard 2.0 class libraries. This means Visual Basic only offers templates for class libraries and console apps, while C# and F# also include templates for ASP.NET Core 2.0 apps.

Improved Common Project File

One of the first things you’ll notice about working with .NET Core projects is that they share a highly simplified common project format. This means that a project created with one tool (for example, the command-line tools) will work on anything that supports .NET Core (such as Visual Studio or Visual Studio for Mac). With our new project file format, we’ve removed the need for GUIDs, and for explicitly listing included files—which greatly reduces merge conflicts when committing updates to version control. The following code shows the entire contents of the project file for a new C# .NET Core console app:

Additionally, the common format means that members of the same team can work using the device and platform of their choice. If one team member wants to work on a Mac, another on Windows, and a third on Linux, they can all seamlessly contribute to the same project. To learn more about the updated project format, visit aka.ms/newprojectfile.

Command-Line Tools

The foundation of any developer tool is the Software Development Kit (SDK), and .NET Core is no different. One of the integral parts of the .NET Core SDK is the command-line interface (CLI), which enables you to create, build and run apps from the command line. With the CLI you can use your favorite editor to create apps with no need to install heavier-weight tools. To get started, install the free .NET Core SDK (available from dot.net).

Once the SDK is installed, open your favorite command prompt and run “dotnet --help” to see a list of all the commands available to you. The dotnet command is the driver for the .NET CLI, where the grammar is “dotnet <verb> [parameters]”. For example, to see all the project type templates available to create, type “dotnet new.” To create a new project, provide the short name as the parameter to the “new” action; for example, “dotnet new razor” will create a C# ASP.NET Core Web App with the new .NET Core 2.0 Razor Pages (to learn about this great new feature, see aka.ms/razorpages). To run the application, type “dotnet run” and it will build and start the application. Opening a browser to the URL indicated in the output string (for example, http://localhost:5000) lets you interact with the app you’re building. From this point, development is a simple matter of choosing your favorite editor to build and run the app.

Unit Testing

Once you have an app, you’ll want to add unit testing support relatively early in the lifecycle. Unit testing works with your IDEs as you’d expect, but you can also build and run unit tests directly from the .NET CLI.

You may have noticed that “dotnet new” offers two different unit-testing project types: xunit and mstest. To get started, create a unit test project with your preferred unit test testing framework, for example “dotnet new xunit,” and then in your test project folder type “dotnet add reference <path to project to test>” to have the CLI add the reference to the project file for you. Once you have a unit-testing project, run your test projects from the CLI using the “dotnet test” command. Regardless of which project type you chose, both xunit and mstest projects are supported by Visual Studio and Visual Studio for Mac, as well as the CLI.

Visual Studio Code

While you have the freedom to use any editor you wish, we think Visual Studio Code offers the best experience for users who desire a lightweight editing experience in conjunction with the .NET Core CLI. Visual Studio Code is our lightweight cross-platform editor with source control and debugging support built in. Using the .NET CLI with Visual Studio Code, you can build .NET applications on any platform you choose. To get started you’ll need to install the .NET Core SDK as previ­ously discussed. Once you have it on your machine, install Visual Studio Code from code.visualstudio.com, and then install the C# extension from Microsoft—C# for Visual Studio Code (powered by OmniSharp)—by selecting the extension tab on the left side of Visual Studio Code. (If you’re using F#, you’ll want to install the Ionide-fsharp extension, as well. Note that the C# extension is needed for .NET Core build and debugging support.) You’re now ready to develop .NET Core projects using Visual Studio Code.

The C# extension enables Visual Studio code to call the CLI on your behalf to build and run applications for debugging. Once you’ve created your project, open Visual Studio Code and from the File menu choose Open Folder. You’ll see a prompt when you open a C# file (.cs)—a suggestion bar at the top of the editor offering to generate the required assets for building and debugging, as shown in Figure 1. Choose Yes and it will generate two files, tasks.json, which enables building from Visual Studio Code, and launch.json, which enables debugging. You’re now ready to edit, compile and debug directly from Visual Studio Code.

Figure 1 Visual Studio Code Offering to Generate Required Files for Building and Debugging

Visual Studio

Visual Studio continues to support a world-class developer experience, and the tooling support for .NET Core is no different. .NET Core 2.0 support was introduced in Visual Studio 2017 (version 15.3). To get started writing a .NET Core 2.0 app in Visual Studio, you need to download and install the .NET Core 2.0 SDK. Once it’s on your machine, restart Visual Studio and you’ll see .NET Core 2.0 as a target for new projects.

Our goal was to make Visual Studio 2017 the most productive development experience we’ve ever offered. To hit a few highlights, we’ve added code navigation improvements, a significant number of new refactoring/quick fix commands, code-style enforcement and Live Unit Testing.

Navigation and RefactoringWe know that as you’re working in your code it’s important to have great support for navigating around your code base. The most popular improvements to code navigation in Visual Studio 2017 are:

  • Go To Implementation (Ctrl+F12): Navigates from any base type or member to its various implementations.
  • Go To All (Ctrl+T or Ctrl+,): Navigates directly to any file/type/member/symbol declaration. You can use the icons along the top of the feature to filter your result list or use the query syntax (for example, “f searchTerm” for files, “t searchTerm” for types and so on).
  • Find All References (Shift+F12): Now with syntax colorization, Find All References results can be custom grouped by a combination of project, definition and path. You can also “lock” results so you can continue to find other references without losing your original results.
  • Indent Guides: Dotted, gray, vertical lines act as landmarks in code to provide context within your frame of view. You may recognize these from the popular Productivity Power Tools.

Additionally, it’s not enough to be able to simply navigate your code base; we know that when you find places in your code to change or clean up, you need tools to help you with that refactoring. To assist with this, we’ve added support for moving a type to a file with same name, syncing a file and type name, adding a null-check for a parameter, adding a parameter, adding a missing switch/Select case, making a method synchronous, converting a method to property (and vice versa), and resolving a merge conflict—just to name a few.

Code Style In Visual Studio 2017, you can configure and enforce your team’s coding conventions to drive consistency across your entire repository with EditorConfig. EditorConfig is an open file format and we worked with its community to support .NET code style within this format. Teams can configure convention preferences and choose how they’re enforced inside the editor (as suggestions, warnings or errors). The rules apply to whatever files are in the directory that contains the EditorConfig file. If you have different conventions for different projects, you can define each project’s rules in different EditorConfig files as long as the projects are in separate directories. Because, at the end of the day, EditorConfig is just a text file, so it’s easy to check it into source control and have it live and travel with your source. To learn more about EditorConfig support in Visual Studio, see aka.ms/editorconfig.

Live Unit Testing Once you have an MSTest or xUnit or NUnit test project, you can enable Live Unit Testing (LUT) for your .NET Core projects. LUT is a new feature introduced in Visual Studio 2017 Enterprise Edition. When you enable LUT, you get unit test coverage and pass/fail feedback, live in the code editor as you type, as shown in Figure 2. This means you no longer need to leave the editor to run unit tests to verify code changes. As soon as you type, you’ll see instant feedback in the editor that shows the results for all tests that are impacted by the code change.

Figure 2 Live Unit Testing Running on a .NET Core Project

Teams that are passionate about quality and users who love test-driven development (TDD) will love this new addition. To turn on LUT, navigate to the Test entry in the Visual Studio menu bar, and select Live Unit Testing | Start.

Visual Studio for Mac

Visual Studio for Mac is the newest member of the Visual Studio family for developers who want an IDE experience on the macOS. Visual Studio for Mac has evolved from Xamarin Studio, which brings support for C# and F# projects. To get started, first install the .NET Core 2.0 SDK (available to download from dot.net), choose Create Project, then select the App entry under the .NET Core category to choose your desired project template.

You’ll find you have a fully featured IDE for developing .NET Core apps on your Mac, as shown in Figure 3. One of the things we’re working hard at is to bring a consistent experience between Visual Studio and Visual Studio for Mac. At this point, you have most of the features you’ve come to rely on in Visual Studio, including IntelliSense, code navigation, refactoring, unit testing and source control integration. For Web development, we’ve brought the Visual Studio HTML, CSS and JSON editors to Visual Studio for Mac. You’ll notice that Razor files (.cshtml) and JavaScript/TypeScript are not currently supported, but we’re working to bring that support to Visual Studio for Mac in a future update.

Figure 3 Visual Studio for Mac

Tools with the Modern Cloud in Mind

As the world of technology evolves, one thing is clear—more and more apps are going to be designed to run in the cloud from the start. This means that your tools need to enable design patterns and practices with the modern cloud in mind, as well as enable you to quickly move an app from your local development machine to the cloud. With this goal in mind, both Visual Studio and Visual Studio for Mac have built-in support for publishing directly to Microsoft Azure, and for packaging applications as Docker containers.

Publish Directly to Azure When you’re ready to move an appli­cation to run in the cloud, you’ll likely want to begin by trying it in the cloud with as little effort as possible. Both Visual Studio and Visual Studio for Mac enable you to publish an app directly from your machine to Azure App Service. App Service is a fully managed cloud environment that enables you to run your app without the need to worry about complex configuration or infrastructure management.

Publishing your app from your local machine to Azure App Service from Visual Studio or Visual Studio for Mac is as simple as right-clicking the project and choosing Publish. You’ll be asked to enter some information, such as the unique URL for your app, and you’ll be able to choose either an existing App Service Plan (how many resources you want reserved for the collection of member apps) or create a new one if you don’t have one yet. Once you’ve entered the information, your app will be running in the cloud in a matter of minutes.

Container Development Tools One of the things that’s becoming clear about modern cloud development is that containers are going to revolutionize the way people architect and build software. Containers enable you to package your application and all of its dependencies, including a copy of the runtime, into a single unit, guaranteeing that changes to the underlying server will never break your application. This enables a microservice architecture pattern in which you deploy each unit of logic within your application as a separate container, with a defined protocol to communicate between them. This lets you scale high-demand parts of the app as needed without paying the cost of scaling the entire application, and it means that fixes affect only the container that’s being updated rather than the entire application.

Visual Studio For Mac Unit Tests

With that in mind, one of our goals with .NET Core is to make it the premier runtime for creating containerized microservices. Visual Studio, Visual Studio for Mac and Visual Studio Code all support building apps as Docker containers. (Note that at the time of this writing, Docker tooling support for Visual Studio for Mac requires the Docker Tools extension, and that Microsoft offers a Docker extension for Visual Studio Code to support creating Docker containers.)

To build a Docker container, you need Docker’s tools for your OS installed (learn more and download from docker.com). Once you have the requirements installed, all you need to do in Visual Studio or Visual Studio for Mac is right-click the project, choose Add and then select Docker Support. This will add a docker-compose project to your solution, and a Dockerfile to the project. These assets enable your project to be built into a Docker container. When you then run the app from the IDE, you’ll be running and debugging your application inside a container rather than on the host machine directly. The great news is that the publishing to Azure App Service experience we discussed also supports publishing Linux Docker containers.

Wrapping It Up

If you haven’t tried building a .NET Core app yet, there’s no better time to start than now. .NET Core and .NET Standard 2.0 (as covered in the “Demystifying .NET Core and .NET Standard” article) make creating a .NET application easy no matter what tools, OS, language or work style you choose.

Unit

We hope you found this tour of the available .NET Core tools useful. You can continue to follow our team’s progress and future improvements on our blog at aka.ms/dotnetblog. Please try out the tools we discussed and give us feedback on what we can do to further improve your experience creating .NET Core apps. You can leave comments and questions on our blog posts, or send us feedback directly through Visual Studio and Visual Studio for Mac by using the Report a Problem and Provide a Suggestion features built into the IDEs.

Andrew Hallis the program manager lead for the .NET languages and tools in Visual Studio. After graduating from college, he wrote line-of-business applications before returning to school for his master’s degree in computer science. After completing his master’s degree, he joined the diagnostics team in Visual Studio where he worked on the debugger, profiling and code analysis tools. He then moved to the .NET languages and tools team where he works on the .NET languages and supporting tools, including tools for ASP.NET, Web and Azure App Service. You can reach Hall via Twitter: @AndrewBrianHall.

Joe Morrisis a program manager for .NET Core tools and Live Unit Testing in Visual Studio. He holds a master’s degree in Computer Science. He started his career as an application developer consultant to enterprises in the United States in the 1990s. He joined Microsoft in 1997 with Microsoft Consulting Services, Iowa, and later moved to Redmond. In the last two years his focus has been on static code analysis and developer productivity tools. You can reach Morris via Twitter: @_jomorris.

Visual Studio For Mac Download

Thanks to the following Microsoft technical experts who reviewed this article: Dustin Campbell, Phillip Carter, Livar Cunha, Mikayla Hutchinson, Mads Kristensen, Jordan Matthiesen and Kasey Uhlenhuth