Создайте новый .NETCore MSTest проект и получите сообщение об ошибке, указывающее, что ‘VisualStudio’ не существует в пространстве имен ‘Microsoft’

#c# #.net-core #mstest

#c# #.net-core #mstest

Вопрос:

Я пытаюсь создать новый проект MSTest V2. Я создаю новый проект MSTest .NETCore, и до того, как я внес какие-либо изменения, я уже получаю эту ошибку

Тип или имя пространства имен ‘VisualStudio’ не существует в пространстве имен ‘Microsoft’ (вам не хватает ссылки на сборку?) в file .nugetpackages microsoft.net.test.sdk 16.5.0build netcoreapp2.1 Microsoft.NET.Test.Sdk.Program.cs(3,12,3,24)

Я использую VS2019 16.6.30204.135 Я установил Microsoft .Net Core SDK 3.1.102 и 2.1.402

UnitTest1.cs:

 using Microsoft.VisualStudio.TestTools.UnitTesting;

    namespace UnitTestProject2
    {
        [TestClass]
        public class UnitTest1
        {
            [TestMethod]
            public void TestMethod1()
            {
            }
        }
    }
 

файл csproj:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</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>

</Project>
 

вывод сборки:

 1>------ Build started: Project: MyProject, Configuration: Debug Any CPU ------
1>MyProjectUnitTest1.cs(1,17,1,29): error CS0234: The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
1>.nugetpackagesmicrosoft.net.test.sdk16.5.0buildnetcoreapp2.1Microsoft.NET.Test.Sdk.Program.cs(3,12,3,24): error CS0234: The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
1>MyProjectUnitTest1.cs(5,6,5,15): error CS0246: The type or namespace name 'TestClassAttribute' could not be found (are you missing a using directive or an assembly reference?)
1>MyProjectUnitTest1.cs(5,6,5,15): error CS0246: The type or namespace name 'TestClass' could not be found (are you missing a using directive or an assembly reference?)
1>MyProjectUnitTest1.cs(8,10,8,20): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?)
1>MyProjectUnitTest1.cs(8,10,8,20): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?)
1>Done building project "MyProject.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
 

Ответ №1:

Для решения вашей проблемы вы можете переустановить пакет MSTest или обновить VS до последней версии.

У меня была такая же проблема с пакетом Morelinq, переустановите его и повторно запустите мой Vs.

Ответ №2:

Обновление до Visual Studio 16.8.2 исправило это.