0

I am using MSbuild to build and deploy .NET 4.6.2 project. I have Main.proj file with the following configuration

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">  
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
    <PublishProfileName>$(Configuration).pubxml</PublishProfileName>
    <IntegrationDomainOutput>$(MSBuildThisFileDirectory)BuildOutput\</IntegrationDomainOutput>
    <VisualStudioVersion>14.0</VisualStudioVersion>
    <Framework40Dir>C:\Windows\Microsoft.NET\Framework64\v4.0.30319</Framework40Dir>
  </PropertyGroup>


  <Target Name="Main" DependsOnTargets="Clean">
    <!--<Error Condition="$(SchedulerServiceDestinationFolder)==''" Text="'SchedulerServiceDestinationFolder' property is missing." />-->   
    <CallTarget Targets="BuildAndDeployApi;" />
  </Target>  

  <Target Name="Clean">
    <RemoveDir Directories="$(IntegrationDomainOutput)"/>
  </Target>

  <Target Name="BuildAndDeployApi">
    <MSBuild Projects="$(MSBuildThisFileDirectory)IntegrationDomain.sln"
             Properties="Configuration=$(Configuration);
                         DeployOnBuild=true;
                         PublishProfile=$(PublishProfileName);
                         OutDir=$(IntegrationDomainOutput);
                         VisualStudioVersion=$(VisualStudioVersion);
                         Framework40Dir=$(Framework40Dir)"
             Targets="Build"/>
  </Target>
</Project>

I also have corresponding .pubxml file for each environment.

In Jenkins then i simply run windows command msbuild Main.proj /p:Configuration=development which builds and deploys the project.

Now i want to execute unit tests after the build but before deployment. When Main.proj builds, the unit test project file Domain.UnitTests.dll get copied to $(ItegrationDomainOutput) path.

The corresponding mstest commmand to execute unit tests is

mstest /testcontainer:$(ItegrationDomainOutput)\Domain.UnitTests.dll /resultsfile:TestResults.trx

However i am not sure how do execute this command after build but before the deployment

UPDATE 1
There is AfterBuild and AfterCompile predefined targets which we can override. So as per the documentaion i have updated my proj file. I have override AfterCompile target and then Updated Build target's DependsOnTargets attribute.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">  

  ... This part is same as original
  ... Removed for Brevity purpose 

  <Target Name="Build" DependsOnTargets="AfterCompile"/>

  <Target Name="BuildAndDeployApi">
    <MSBuild Projects="$(MSBuildThisFileDirectory)IntegrationDomain.sln"
             Properties="Configuration=$(Configuration);
                         DeployOnBuild=true;
                         PublishProfile=$(PublishProfileName);
                         OutDir=$(IntegrationDomainOutput);
                         VisualStudioVersion=$(VisualStudioVersion);
                         Framework40Dir=$(Framework40Dir)"
             Targets="Build"/>
  </Target>  

  <Target Name="AfterCompile">
    <!--Run MSTest.exe-->  
      <Exec Command="C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\mstest.exe /testcontainer:$(IntegrationDomainOutput)\Domain.UnitTests.dll" >       
      </Exec> 
  </Target>

</Project>

However, AfterCompile target never get executed

LP13
  • 30,567
  • 53
  • 217
  • 400
  • Since you run build and deploy a project on Jenkins, make sense to add a build step to run tests between build and deploy, IMO – Pavel Anikhouski Jan 23 '20 at 08:04
  • issue is in `.proj` file the `BuildAndDeployApi` target builds and deploy. I Dont know how to separate those two steps – LP13 Jan 23 '20 at 16:13

0 Answers0