Master C# Development in VSCode: Writing and Running Tests Guide
Visual Studio Code (VSCode) has been my go-to integrated development environment (IDE) since my university days when I first ventured into Python development.
What initially drew me to it were the extensive customization options, intuitive shortcuts, and the ever-expanding library of extensions. These features reduced the need for frequent application-switching, helping me maintain focus and minimize distractions during coding sessions.
In this article, I'm excited to guide you through the process of setting up VSCode for C# development, with a special focus on writing and running tests. As a Senior Software Development Engineer in Test (SDET), I understand the importance of efficient testing, and I'm eager to show you how to streamline your testing workflow within VSCode.
For reference, I'll occasionally compare the VSCode experience with Visual Studio, the IDE I currently use for C# development. I won't delve into the Visual Studio vs. Rider comparison since plenty of literature already covers that topic. An additional benefit of using VSCode is its accessibility to individual users without the need for costly licenses.
C# Dev Kit Extension
One of the standout features of VSCode is its extensive library of extensions. These extensions provide support for a wide range of programming languages and offer tools for configuring, running, and visualizing various applications directly within the IDE. (Feel free to explore my list of favorite VSCode extensions at the end of this article.)
To kickstart your C# development journey, I recommend installing the "Microsoft C# Dev Kit" extension. Please note that you'll need a Visual Studio account for this, and it's essential to check whether your license permits your intended usage.
This extension installation will include the C# extension, "IntelliCode" for C# development, and the "Runtime Install Tool."
The "C# Dev Kit" comes with a helpful tutorial to ensure you set up everything correctly. You can access this tutorial by opening a new VSCode window or using the command palette (shortcut: Cmd + Shift + P) and typing "Welcome: Open Walkthrough."
If you don't already have the .NET SDK installed you will also have to install this. I installed the .NET 7 SDK for MacOS.
Adding a Unit Test Project
In this tutorial, I'll demonstrate how to add a unit test project to your solution. I've chosen the "Heliocentric Kattis problem," a C# project already available on my GitHub account, as our example.
When you open the project with the C# Dev Kit enabled, you'll notice a new "Solution Explorer" window. This view closely resembles the one you'd find in Visual Studio. What's particularly convenient is the ability to right-click on a solution and access options for building, rebuilding, and cleaning.
To add a new project, click on the "+" icon next to your solution's name. For this example, I'm going to add an NUnit test project to the Heliocentric solution, although XUnit is also available by default.
You'll see the newly created NUnit unit test project in the solution, named "HeliocentricTests," complete with a placeholder unit test, much like what you'd find in Visual Studio.
领英推荐
The next step is to add a reference to the Heliocentric project, and you can do this through the Solution Explorer view.
With that in place, I drafted my unit test for the "AlignmentCalculator" in my Heliocentric project.
The "C# Dev Kit" extension provides two key tabs: "Run & Debug" and "Testing."
In the "Testing" tab, you'll have the ability to run your tests and view the results in the "Test Results" located at the bottom of the screen.
Considerations
At present, I've encountered a limitation with the C# Dev Kit extension, specifically, the absence of native support for adding a .runsettings configuration for test execution. In my current professional projects, the use of .runsettings is essential to configure runtime parameters for tests, and the lack of this support means I can't execute tests seamlessly. It's worth noting that the C# Dev Kit is continuously evolving, and I hope that future updates will include support for .runsettings, making it an even more powerful tool for C# developers.
Summary
This setup not only streamlines the process of writing and running tests in VSCode but also brings a sense of familiarity to those transitioning from Visual Studio. Enjoy your enhanced testing experience in the world of C# development with VSCode!
Bonus!
The VSCode extensions I am using regularly at the moment: