I want to run unit tests using MSBuild. Here is how I invoke msbuild today:
msbuild MySolution.sln
Instead, I want to use an MSBuild project file called "MyBuild.proj" like this:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5" DefaultTargets="Build">
<Target Name="Build">
<ItemGroup>
<SolutionToBuild Include="MySolution.sln" />
<TestContainer Include="..\Output\bin\Debug\*unittests.dll"/>
</ItemGroup>
</Target>
</Project>
And then call this command line:
msbuild MyBuild.proj
For some reason, when I do that the command succeeds immediately and the build doesn't even happen. I fear I must be missing something very obvious as I am new to MSBuild.
I suppose I really have 2 questions:
Why doesn't this even build my solution
Is the "TestContainer" element correct for executing my tests
Thanks!
You havent supplied any task to actually do anything,
inside your build target you need a call to an msbuild task, your example becomes:
<Target Name="Build">
<ItemGroup>
<SolutionToBuild Include="MySolution.sln" />
<TestContainer Include="..\Output\bin\Debug\*unittests.dll"/>
</ItemGroup>
<MSBuild Projects="#(SolutionToBuild)"/>
</Target>
this specifies what projects you actually want msbuild to build.
See:http://msdn.microsoft.com/en-us/library/vstudio/z7f65y0d.aspx for more details and the parameters it takes.
Thats part one.
As for part 2? what testing framework are you using? If using mstest id try wrapping the commandline mstest.exe in an msbuild exec statement to get it to run and execute the tests. See an example here:http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/cb87a184-6589-454b-bf1c-2e82771fc3aa
Related
After some research, to get VisualStudio to build a project's object files in parallel, one must ensure that project properties MinimalRebuild is off and MultiProcessorCompilation is on. I can configure this in the project file, but is there a way to override these project settings on the command line using msbuild?
In other words, using msbuild from the command line, how would I override MinimalRebuild (/Gm-) to be false and MultiProcessorCompilation (/MP) to be true? The following doesn't seem to work
msbuild /m:7 /p:CL_MPCount=7 /p:MinimalRebuild=false /p:MultiProcessorCompilation=true
As an aside, is /p:CL_MPCOUNT redundant with the MultiProcessorCompilation property? Overall, I'm having trouble finding documentation on what fits the /p option.
In other words, using msbuild from the command line, how would I
override MinimalRebuild (/Gm-) to be false and
MultiProcessorCompilation (/MP) to be true?
CL_MPCount can be overrided by /p:xxx=xxx but MultiProcessorCompilation and MinimalRebuild are not. Just to be clear, It can only be overridden by /p:xxx=xxx(p means property) if it is a property of MSBuild. And the Property usually can be called by $. So I do some tests:
Edit
Test
1)CL_MPCount
This value means Maximum Number of concurrent C++ compilations in VS IDE.You can refer to this.
So I have assigned a value by VS IDE by(Debug->Options->Projects and Solutions->VC++ Project Solutions and set the Maximum Number of concurrent C++ compilations to 2.)
write a custom target like
<Target Name ="Test" AfterTargets="Build">
<Message Importance="high" Text="CL_MPCount is= $(CL_MPCount)">
</Message>
</Target>
Then Build and it shows;
From this we can see that this is a property that belongs to MSBuild.
2)MinimalRebuild
Usually, it means /GM and it usually set in the Properties in a c++ project. l have set it to True by (Right-click on the project-->Properties-->C/C++-->Code Generation-->set Enable Minimal Rebuild to True).
Custom target like
<Target Name ="Test" AfterTargets="Build">
<Message Importance="high" Text="MinimalRebuild is= $(MinimalRebuild)">
</Message>
</Target>
The result is like
From it, the value which set in the VS IDE cannot be available by $(MinimalRebuild) which means it is not a property of MSBuild.
3) MultiProcessorCompilation
It also means /MP and from the document we will find that it is an option of the the compiler not a property of MSBuild. Besides, l did the same as the MinimalRebuild and it is not being output. So it is also not a property of MSBuild.
In addition, l thought MinimalRebuild and MultiProcessorCompilation are the options of MSBuild before. So I type MSBuild -help in Command Line and found that it did not list these parameters at all. So I think these two parameters have nothing to do with MSBuild at all, but are just some of the compiler's options.
Hope it could help you.
I found a way to override both properties at User level. Anyway you may find them helpful (for example for Build host where incremental build is not meaningful at all).
Create these two properties files:
%USERPROFILE%\AppData\Local\Microsoft\MSBuild\v4.0\Microsoft.Cpp.Win32.user.props
%USERPROFILE%\AppData\Local\Microsoft\MSBuild\v4.0\Microsoft.Cpp.x64.user.props
with following content:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup>
<ClCompile>
<MinimalRebuild>false</MinimalRebuild>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<ProcessorNumber>2</ProcessorNumber>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup />
</Project>
Feel free to customize ProcessorNumber to suit your hardware.
Now MSBUild will use above values for all projects.
I am trying to call the Publish target every time I build my WPF app. I have tweaked the .csproj file to include this:
<Target Name="AfterBuild">
<Message Text="Running AfterBuild..." />
<MSBuild Projects="$(MSBuildProjectFullPath)" Properties="Configuration=$(Configuration); PublishDependsOn=" Targets="Publish" />
</Target>
When I run this from the command line, I see the message that it is 'Running AfterBuild...' but nothing happens. If I remove the '; PublishDependsOn=' from the Properties of the MSBuild task, I get a circular reference error.
What magic am I missing here?
OK, I figured out how to do what I want to do. Instead of trying to explicitly call Publish in AfterBuild, I just added it to the DefaultTargets of the project. Now it calls Build then Publish.
<Project ToolsVersion="4.0" DefaultTargets="Build;Publish" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
I've been looking through CruiseControl documentation and I found tag and
for running scripts. But when I am trying to run exe-file from that tags it does not work as well as it described in documantation.
I also tried to put call of the exe in batch file and execute it from CruiseControl but also did not work as I expected. So how can I run exe-file from CC? I also need to be able to include output of this file work in my email notification is it possible at all?
E.g. I have file UnitTests.exe which prints something like this:
Unit tests are passed.
47 Tests was successful
How can I do this? Or how can I at least get an returning code from that executable file?
Run the exec in ant.
In cruisecontrol:
<schedule>
<ant anthome="/usr/apache-ant-1.8.2" buildfile="/usr/ant-build-files/my-ant-build-file.build" target="do-task" uselogger="true">
</ant>
</schedule>
In /usr/ant-build-files/my-ant-build-file.build
...
<target name="do-task">
<exec executable="/<path to dir containing exe>/UnitTests.exe" failonerror="true">
<arg line="<args to UnitTests.exe>"/>
</exec>
There is option to execute .bat or .exe files using the following tag.
<exec executable="c:/something.exe" />
You can place the above line in any target of the xml files that your build script is going to call.
<target name="target-to-call-an-exe">
<exec executable="c:/cygwin/bin/bash.exe" />
</target>
Hope this helps, Thanks.
I'm trying to set up an ANT build script which compile code, compile tests, run unittests and then build. These are all done through separate targets with dependencies i.e.
<target name="compile">
<javac>...
</target>
<target name="compile-tests" depends="compile">
<javac>...
</target>
<target name="unittest" depends="compile-tests">
<junit...
<test ...
<fail if="tests.failed" ..
</target>
<target name="build" depends="compile, unittest">
</target>
Each 'test' inside the 'junit' task focuses on one part of the application, (typically package by package) and points to a Junit TestSuite. This set up allows for all tests to be run when a build is called but this isn't ideal for day-to-day development.
I would like to be able to do 2 things:
Run all the tests in a build (like the setup shown above)
Run tests individually from ant
My solution for (2) was to use multiple antcall tasks which isn't really best practice. During these calls different properties were set to run all the tests as they each required a different property:
<!-- test package p2 with ant unittest -Dtest.p2=true -->
<target name="unittest" depends="compile-tests">
<junit...
<test if="test.p1" ...
<test if="test.p2"
<fail if="tests.failed" ..
</target>
<target name="unittestall">
<property name="test.p1" value="true"/>
...
</target>
<target name="build" depends="compile, unittest">
<antcall target="unittestall" />
<antcall target="clean" />
<antcall target="compile" />
</target>
This gave the granularity I required but meant alot of work was duplicated and ant's dependency features weren't being used to their full.
So my question is:
How can I best set up ANT and Junit so that all tests can be run as part of a build AND so that individual tests can be run?
Thankyou :)
from Joshua England
p.s. ANT 1.8 and Junit 4.10 :)
Something like this?
<target name="unittest-p1"></target>
<target name="unittest-p2"></target>
<target name="unittest-p3"></target>
<target name="unittest" depends="unittest-p1, unittest-p2, unittest-p3/>
You could then run all the tests by passing the unittest target:
ant unittest
(or any target which depends on unittest)
And you could run any individual test of set of tests by invoking the appropriate target, e.g.
ant unittest-p1
If you would end up with a lot of duplication in multiple junit targets, you could tidy that up by putting all the common stuff into a macrodef.
There are plenty of guides out there which help you mimic VS2008's "Custom Build Step" in VS2010 with MSBuild. However, I'd like my build to be smarter and make use of MSBuild. I've written a little MSBuild task which invokes the ANTLR parser generator. That build task works flawlessly when I run it in a simple test MSBuild file. However, when I try to add my task to a C++ project, I run into problems. Essentially I've added this to the top of my project file (Right after the <project> element):
<UsingTask TaskName="ANTLR.MSBuild.AntlrGrammar"
AssemblyName = "ANTLR.MSBuild, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d50cc80512acc876" />
<Target Name="BeforeBuild"
Inputs="ConfigurationParser.g"
Outputs="ConfigurationParserParser.h;ConfigurationParserParser.cpp;ConfigurationParserLexer.h;ConfigurationParserLexer.cpp">
<AntlrGrammar
AntlrLocation="$(MSBuildProjectDirectory)Antlr.jar"
Grammar="ConfigurationParser.g"
RenameToCpp="true" />
</Target>
However, my target is not being called before build.
How can I add my task to a C++ build?
Before reading this answer, you'll probably want to see:
General .vcxproj File Reference
The New Way of doing Build Extensibility in .NET 4
The old way of extending MSBuild, and the one mentioned by the reference book I have, essentially is based on overriding default-empty targets supplied by Microsoft. The new way, as specified in the second link above, is to define your own arbitrary target, and use the "BeforeTargets" and "AfterTargets" properties to force your target to run before or after your intended target.
In my specific case, I needed the ANTLR Grammars task to run before the CLCompile target, which actually builds the C++ files, because the ANTLR Grammars task builds .cpp files. Therefore, the XML looks like this:
<Project ...
<!-- Other things put in by VS2010 ... this is the bottom of the file -->
<UsingTask TaskName="ANTLR.MSBuild.AntlrGrammar"
AssemblyName = "ANTLR.MSBuild, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d50cc80512acc876" />
<Target Name="AntlrGrammars"
Inputs="Configuration.g"
Outputs="ConfigurationParser.h;ConfigurationParser.cpp;ConfigurationLexer.h;ConfigurationLexer.cpp"
BeforeTargets="ClCompile">
<AntlrGrammar
AntlrLocation="$(MSBuildProjectDirectory)\Antlr.jar"
Grammar="Configuration.g"
RenameToCpp="true" />
</Target>
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
</Project>
As for why this is superior to a PreBuildEvent and/or PostBuildEvent; this is smart enough to not rebuild the .cpps when the grammar itself is not updated. You'll get something like:
1>AntlrGrammars:
1>Skipping target "AntlrGrammars" because all output files are up-to-date with respect to the input files.
1>ClCompile:
1> All outputs are up-to-date.
1> All outputs are up-to-date.
This also silences Visual Studio's incessant complaining every time you run the program that it needs to rebuild things, like it does with plain pre- and post- build steps.
Hope this helps someone -- took me frickin forever to figure out.