Home > Back-end >  C# Unit Tests run in Azure DevOps but not in Visual Studio
C# Unit Tests run in Azure DevOps but not in Visual Studio

Time:12-23

I need to add a Unit Test in an existing Visual Studio solution with 100s of tests. I wrote a simple Unit Test only to check if it is working.

My test code is:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace Training.UnitTests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            Assert.AreEqual(1, 1); 
        }
    }
}

The projects used in the Visual Studio solution are of **SDK-Style**. Now when I try to run all the tests, I found that some of the tests run and some of test do not run as shown in the screenshot below (including my newly added sample test).

enter image description here

I do not understand why some tests are running and some tests do not run at all in Visual Studio's Test Explorer. All of these tests run fine in the Azure DevOps pipeline. I even tried to run my sample test individually in Visual Studio by pressing Ctrl R T but the test only builds not run. But if

Following are the sample project files of the UnitTest projects which run and those wo do not run.

.csproj of a running Project

<Project Sdk="Microsoft.NET.Sdk">
    <Import Project="..\..\..\..\_Solution\build.defaults.targets" />
    <PropertyGroup>
        <TargetFramework>net472</TargetFramework>
        <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
        <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
        <IsCodedUITest>False</IsCodedUITest>
        <TestProjectType>UnitTest</TestProjectType>
        <OutputPath>$(SolutionDir)SystemTests\$(Configuration)</OutputPath>
        <AssemblyTitle>SystemTest</AssemblyTitle>
        <DebugType>full</DebugType>
        <LangVersion>8</LangVersion>
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <Optimize>false</Optimize>
        <DebugSymbols>true</DebugSymbols>
        <AssemblyName>SystemTest.Bus</AssemblyName>
    </PropertyGroup>
    <Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
    
    <ItemGroup>
        <Reference Include="PresentationCore" />
    </ItemGroup>
    <ItemGroup>
      <PackageReference Include="MSTest.TestAdapter" Version="$(MSTestTestAdapterNugetVersion)" />
      <PackageReference Include="MSTest.TestFramework" Version="$(MSTestTestAdapterNugetVersion)" />
      <PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkNugetVersion)" />
        <PackageReference Include="FluentAssertions" Version="$(FluentAssertionsNugetVersion)" />
    </ItemGroup>
    
  <ItemGroup>
    <ProjectReference Include="..\..\..\UnitTestSetups.csproj" />
    <ProjectReference Include="..\..\Bus.Impl.csproj" />
  </ItemGroup>
  <Target Name="CopyCustomContent" AfterTargets="AfterBuild">
    <Copy SourceFiles="..\..\..\..\_Solution\automation.runsettings" DestinationFolder="$(OutDir)" />
  </Target>
</Project>

.csproj file of a NOT running project:

<Project Sdk="Microsoft.NET.Sdk">  
  <Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
  <Import Project="..\..\..\..\_Solution\build.defaults.targets" />

  <PropertyGroup>
    <TargetFramework>net472</TargetFramework>
    <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
    <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
    <IsCodedUITest>False</IsCodedUITest>
    <TestProjectType>UnitTest</TestProjectType>
    <OutputPath>$(SolutionDir)\Tests\$(Configuration)</OutputPath>
    <AssemblyTitle>Training.UnitTests</AssemblyTitle>
    <LangVersion>8</LangVersion>
  </PropertyGroup>
  
    <ItemGroup>
    <Reference Include="PresentationFramework" />
    <Reference Include="System" />
    <Reference Include="System.ComponentModel.Composition" />
    <Reference Include="System.Configuration" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="WindowsBase" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="FluentAssertions" Version="$(FluentAssertionsNugetVersion)" />
    <PackageReference Include="Moq" Version="$(MoqNugetVersion)" />
    <PackageReference Include="MSTest.TestAdapter" Version="$(MSTestTestAdapterNugetVersion)" />
    <PackageReference Include="MSTest.TestFramework" Version="$(MSTestTestAdapterNugetVersion)" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkNugetVersion)" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Caliburn.Micro" Version="$(VcSimNugetVersion)" />
  </ItemGroup>
  
  <ItemGroup>
    <ProjectReference Include="..\..\Training.csproj" />
  </ItemGroup>
</Project>

QUESTION: Why only some of the UnitTests are running in Visual Studio's Test Explorer? The test which do not run in VS run fine in Azure DevOps pipeline.

CodePudding user response:

Comparing both csprojs, I noticed the one which contains the tests that are not running contains the following line:

 <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

I've checked a previous answer and I believed the above line was either in the wrong place or not being used, reason why I've first recommended removing it to validate my assumption. It turns out it was not being used and after removing, it solved your problem.

  • Related