MSTest unit test not working in VS Code: VS namespace doesn't exist?

Viewed 601

I am trying to write a .NET Core 3.1 console application in Visual Studio Code, and when I try to run the MSTest unit test I made for one part of the application, I get the following error: error CS0234: The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

I was following the instructions in this Microsoft tutorial.

Looking at an older question here hints that I might not be able to run that kind of unit test in VS Code:

The namespace Microsoft.VisualStudio... is defined in assemblies which are not part of the standard Installation of Visual Studio. It is available only if you installed the Visual Studio SDK.

I have tried running the tests on two different computers (a Windows machine and a Linux machine) and I have tried changing the project to .NET 5 instead of .NET Core 3.1. Neither worked, and I would like to change it back to .NET Core 3.1 if that won't break anything.

I have two csprog files. Project names have been redacted.

The main project's csprog file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <RootNamespace>redacted</RootNamespace>
  </PropertyGroup>

</Project>

And the test project's csprog file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
    <PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
    <PackageReference Include="coverlet.collector" Version="1.2.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\redacted.csproj" />
  </ItemGroup>

</Project>

Is it possible to install the Visual Studio SDK without installing Visual Studio itself? I'm trying to use VS Code whenever possible as opposed to full VS. If so, how would I do it, since the Microsoft site is no help? If not, is there another way to write the unit tests? I have accepted that a rewrite of the unit tests might be inevitable, but ideally I would like to not have to rewrite them.

1 Answers

Finally figured it out. I needed to create a solution file, add the test project to the solution, and open the solution (not just the folder) in VS Code. That last part is where I had a bit of a snag.

Related