msbuild unit test - how to run separately? - msbuild

Currently, I have this msbuild script that scans Test DLLs and run once using msbuild /test
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="RunMSTest">
<ItemGroup>
<TestAssemblies Include="**\bin\*Test*.dll"/>
</ItemGroup>
<Target Name="RunMSTest">
<Exec Condition=" '#(TestAssemblies)' != ''"
Command="Mstest.exe /nologo /resultsfile:TestResults.trx #
(TestAssemblies ->'/testcontainer:"%(RecursiveDir)%(Filename)%(Extension)"', ' ')"
/>
</Target>
</Project>
Ideally, I want each Test DLL a separate mstest.exe and the file name saved to TestResults%(FileName).trx

Separation of Microsoft Unit Tests to Reduce the Load of CI Server
https://helloacm.com/separation-of-microsoft-unit-tests-to-reduce-the-load-of-ci-server/

Related

MSBuild project to test a C++ program

I have a .vcxproj file that compiles a C++ program. I would like to create a second MSBuild project file that tests the program by running it, but only if the program has been rebuilt since the last successful test. How can I access the "TargetPath" of the program from the second project file?
If I could access TargetPath as an "item" from the .vcxproj file, then the the tester project file will look like this:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Build" Inputs="#(TargetPath)" Outputs="#(TargetPath->'%(filename).test-passed)'">
<Exec Command="#(TargetPath)" />
<Touch Files="#(TargetPath->'%(filename).test-passed)'" />
</Target>
</Project>
I would like to execute the test using a separate project file from the compilation of the program, to make it easier to choose between build-and-test or build-and-debug within Visual Studio, without multiplying the build configurations.
It is possible to run a native program compiled by a separate .vcxproj using the MSBuild task. Use the <Output> element to create an Item with the "TargetOutputs" from the C++ application build. However, if you are building a "native" program, "TargetOutputs" is normally blank. In this case, use the "GetNativeTargetPath" target to get the output path. The following project .vcxproj file works with Visual Studio. It builds test_build.vcxproj. The test_build.exe file is run, if it has changed since the last successful run.
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{80DB0D71-72E0-4FB1-B53F-EFB858A1D5A8}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>nordic_test_run</RootNamespace>
</PropertyGroup>
<PropertyGroup>
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ItemGroup>
<ProjectReference Include="test_build.vcxproj" />
</ItemGroup>
<Target Name="BuildExecutable">
<MSBuild Projects="#(ProjectReference)" Targets="Build" BuildInParallel="True" />
<MSBuild Projects="#(ProjectReference)" Targets="GetNativeTargetPath" BuildInParallel="True">
<Output TaskParameter="TargetOutputs" ItemName="NativeTests" />
</MSBuild>
</Target>
<Target Name="Build" DependsOnTargets="BuildExecutable" Inputs="#(NativeTests)" Outputs="#(NativeTests->'%(filename).test-passed')">
<Exec Command="#(NativeTests)" />
<Touch Files="#(TestTargets->'%(filename).test-passed')" />
</Target>
</Project>

Change output directory where test results file is generated

I am using MSTest to execute my unit tests against a web service on a remote server.
Is there way to change the path to where the results file(.trx) is generated. Currently my test results is generated in the directory which the Exec command is invoked from:
<Target Name="ExecuteTheTests" AfterTargets="StartService" Condition="'$(ServiceStarted)' == 0 And '$(Configuration)' == 'Release'">
<Message Text="Executing the Unit Tests" Importance="high" />
<PropertyGroup>
<TestSuccessOrNot>0</TestSuccessOrNot>
</PropertyGroup>
<Exec Command=""C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\Common7\\IDE\\MSTest.exe" /testcontainer:..\..\..\..\\_MyOutput\\UnitTests.dll /detail:testname ">
<Output TaskParameter="ExitCode" PropertyName="TestSuccessOrNot" />
</Exec>
<Error Condition="$(TestSuccessOrNot) == 1" Text="Unit tests fail!" />
Thanks,
Many Bothans died* to bring you this message:
mstest.exe /resultsfile:c:\BadPlaceForTestResults.trx
j/k, many Bothans didn't die, they just typed "MsTest.exe /?"

Log4Net configuration error causing MSBuild to fail

I'm trying to set up a CI environment at a new client site using Team City and MSbuild and the MS build community extensions. Compiling the code seems to work fine. However, when I run my unit tests I get the following error coming from the NUnit task:
log4net : error XmlConfigurator: Failed to find configuration section 'log4net' in the application's .config file.
I've identified the two test projects that are causing this issue. However, I've ran the tests directly from nunit-console, and the resharper nunit test runner and though I see the warning the tests don't fail. I don't want to do anything with the Log4net configuration file or the assembly.cs in any project. All I want to do is make the MSBuild script behave like Visual Studio which doesn't consider the log4net error as a failure.
Here's the build file
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0" DefaultTargets="Compile">
<Import Project=".\MSBuild.Community.Tasks.Targets"/>
<PropertyGroup>
<Configuration Condition="'$(Configuration)' == ''"> Debug</Configuration>
</PropertyGroup>
<ItemGroup>
<BuildArtifacts Include=".\build_artifacts\"/>
<SolutionFile Include ="..\Core.Services.sln"/>
<NUnitPath Include="..\Packages\NUnit.2.5.10.11092\tools"/>
</ItemGroup>
<Target Name="Clean">
<RemoveDir Directories="#(BuildArtifacts)"/>
</Target>
<Target Name="Init" DependsOnTargets="Clean">
<MakeDir Directories="#(BuildArtifacts)"/>
</Target>
<Target Name="Compile" DependsOnTargets="Init">
<MSBuild
Projects="#(SolutionFile)"
Targets="Rebuild"
Properties="OutDir=%(BuildArtifacts.FullPath)">
</MSBuild>
</Target>
<Target Name="DevelopmentBuild" DependsOnTargets="Compile">
<Message Text="Running Unit Tests from %(BuildArtifacts.FullPath)...." ContinueOnError="true"></Message>
<CreateItem Include="%(BuildArtifacts.FullPath)*.Tests.dll">
<Output TaskParameter="Include" ItemName="TestAssembly" />
</CreateItem>
<NUnit Assemblies="#(TestAssembly)"
ToolPath="#(NUnitPath)\"
ContinueOnError="false"
OutputXmlFile="%(BuildArtifacts.FullPath)test-results.xml"
DisableShadowCopy="true"/>
</Target>
</Project>

MSBUILD AdditionalProperties not respected in teamcity

I have the following piece of master msbuild script which triggers child scripts with the respective properties.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="3.5">
<PropertyGroup>
<BuildLabel>0.8.1.2</BuildLabel>
</PropertyGroup>
<Target Name="Build" >
<CallTarget Targets="BuildApplication"/>
</Target>
<Target Name="BuildApplication" >
<ItemGroup Condition="'$(Configuration)'==''">
<ProjectToBuild Include="./Application/Application.msbuild">
<AdditionalProperties>Configuration=Publish - Beta</AdditionalProperties>
</ProjectToBuild>
<ProjectToBuild Include="./Application/Application.msbuild">
<AdditionalProperties>Configuration=Publish - Production</AdditionalProperties>
</ProjectToBuild>
</ItemGroup>
<MSBuild Projects="#(ProjectToBuild)" Properties="BuildLabel=$(BuildLabel);Platform=Any CPU" />
</Target>
</Project>
While this script works fine on my local as well as the build server, it does NOT work( $(Configuration) is not available to child as shown by teamcity logs) when the same build server checks out the code and runs the script.
what could be the problem?
The tools version in teamCity was set to default due to which the child scripts were being called using MSbuild 2.0.Changing it to 4.0 fixed it

How to run MSUnit tests with MSBuild

I have a MSBuild script which looks similar to:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Build">
<MSBuild Projects="MyProject.UnitTest.sln" Targets="Build" />
</Target>
</Project>
How do I run the MSTest unit tests which are within the solution?
I am looking for something similar to what can be used in TFS Build of:
<TestContainerInOutput Include="%2a%2a\%2aTest.dll" />