Using specific Visual Studio Project Build configuration for running unit tests - visual-studio-2010

My company already has a Team Foundation Server as a Continuous Integration platform. However, what I am looking to setup is a build configuration that a developer can run on their own development machine.
Let's say I have a Visual Studio solution that contains a .NET C# Class Library project (I'll call this the Library Project). It also contains another project containing the Unit Testing classes for Library Project (I'll call this the Testing Project).
I have the normal Debug and Release build configurations for each project and at the solution level. For both of these configurations, I have set it to only build the Library Project (so Testing Project does not get built).
What I would like to do is set up 2 new build configurations called Debug With Testing and Release With Testing. They will each be the same as the Debug and Release, respectively but I need them to have the following extra features:
Builds the Testing Project.
Run all test cases in the Testing Project.
Run Code Analysis on Library Project.
Generate report for testing and code analysis.
Save report in a specific location.
Doing item 1 is easy. However, I can't figure out how to do items 2 to 5. Can anyone point me in the right direction?
Any help will be greatly appreciated. TIA

You will need to write custom MS build code, I already do some similar task as the following:
Get the latest change from TFS
Build the solution including all projects
Deploy the Main Database locally
Deploy the Test Database locally which hold the test data used in the
data driven test
Run the sanity test or BVT (Build Verification Test) which has
belong to category 1 (Test the integration between DB and code)
Check-in the pending change
And hear the code of this tasks
<Target Name="GetLatestFromTFS2010" AfterTargets="build" >
<Message Importance="high" Text ="start GetLatest for the project "></Message>
<Exec Command='"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\TF.exe" get $/AutoDBand/AutomateDatabaseAndTest/AutomateDatabaseAndTest /recursive /login:YourUsername,YourPassword' ContinueOnError='false'/>
</Target>
<!--===========Deploy Database============-->
<Target Name="DeployDatabase" AfterTargets="GetLatestFromTFS2010" Condition="'$(Configuration)' == 'DebugForCheck-in'">
<Message Importance="high" Text="-------------------------------- Deploying Database according to the connection string -------------------------------- " />
<Message Importance="high" Text=" "/>
<MSBuild Projects="..\DB\DB.dbproj" Targets="Build;Deploy" />
</Target>
<!--============Run the Test==================-->
<Target Name="UnitTests" AfterTargets="DeployDatabase" Condition="'$(Configuration)' == 'DebugForCheck-in'">
<Message Importance="high" Text="-------------------------------- Running Unit Tests for category 1 only--------------------------------" />
<Message Importance="high" Text=" "/>
<Exec Command='"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\mstest.exe" /testcontainer:"..\BLTest\bin\Debug\BLTest.dll" /category:cat1' />
</Target>
<Target Name="Chekin-pendingChange" AfterTargets="UnitTests" >
<Message Importance="high" Text ="-------------------------------- start Check-in process-------------------------------- "></Message>
<Message Importance="high" Text=" "/>
<Exec Command='"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\TF.exe" checkin $/AutoDBand/AutomateDatabaseAndTest/AutomateDatabaseAndTest /recursive /login:YourUsername,YourPassword' ContinueOnError='false'/>
</Target>
For more information you can see this article with source code
http://mohamedradwan.wordpress.com/2010/11/13/automate-the-best-practice-for-check-in-including-get-latest-deploy-db-run-test-check-in/

Have a look at something like:
TeamCity
Jenkins
Team Foundation Server
all are Continous Integration Servers, which are good in doing the jobs you like to have done.

Related

Get list of projects in solution with MSBuild

I have a Visual Studio solution with a number of C++ projects (.vcxproj). There is one utility project with a custom build step. On this build step I would like to get a list of projects in solution and pass it to an external tool. Is there a way to have such a list? Something like $(ProjectsInSolution)?
See also https://social.msdn.microsoft.com/Forums/vstudio/en-US/51254ee1-abaf-496a-89f9-cf87fc2ae1e8/list-project-from-solution-sln-file?forum=msbuild
Get list of projects in solution with MSBuild
You could use MSBuild Community Tasks's GetSolutionProjects for this question.
To accomplish this, create a new project in that solution, like GetProjectsPath. You should add MSBuildTasks to your test project. After that, you will find following scripts in your project file(If not, add it manually):
<Import Project="..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.targets" Condition="Exists('..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.targets')" />
Then unload your project. Then at the very end of the project, just before the end-tag </Project>, place below scripts:
<Target Name="GetProjectsPath" AfterTargets="Build">
<GetSolutionProjects Solution="..\GetProjectsPath.sln">
<Output ItemName="ProjectFiles" TaskParameter="Output" />
</GetSolutionProjects>
<Message Text="Get Projects Path in the solution!" />
<Message Text="Relative project paths:" />
<Message Text="%(ProjectFiles.ProjectPath)" />
<Message Text="Full paths to project files:" />
<Message Text="%(ProjectFiles.FullPath)" />
</Target>
When you build this project, you will get the all projects path in the solution:
Hope this helps.

Running test case files(tst) as part of MSbuild

I am using TFS 2010 and Visual Studio 2010. I have a build definition which points to my solution. The build runs overnight
I have a set of test case files(*.tst) and i would like my current build to include these as part of the build and to execute them overnight. The test case files are in source control
I read that i have to use MsTest.exe but unsure how to get started?
Can anyone point me please how i can get started on running the test case files as part of the build? Any examples please?
Thanks in advance,
There are several ways you can have your test cases run. One way is to add an invoke process or Exec Command to your build project file or workflow.
Place the call to mstest in the AfterBuild target or workflow step. The other way would be to separate out the build and test cases into different builds.
Here are a couple of links to get you started:
How to: Configure and Run Scheduled Tests After Building Your Application
Example of MSBuild with MSTest
*Based on your comments here is an update of something you can do to get more information about the error or to continue if you encounter an error. You do not need to check for the error code if you just want to stop on any error but if you do want to check the error code then you would do something like this:
<Target Name="AfterBuild">
<Message Text="Running tests..." />
<PropertyGroup Label="TestSuccessOrNot">
<TestSuccessOrNot>5</TestSuccessOrNot>
</PropertyGroup>
<!-- Run MSTest exe-->
<Exec Command="cd ." IgnoreExitCode="False" ContinueOnError="ErrorAndContinue">
<Output TaskParameter="ExitCode" PropertyName="TestSuccessOrNot" />
</Exec>
<Message Text="ExitCode = $(TestSuccessOrNot)" />
<Error Condition="'$(TestSuccessOrNot)' != '0'" Text="Unit tests fail!" /> </Target>

VS 2010 Solution Configuration build for a project context

I have a Visual Studio solution containing 30+ projects. There are 2 build configurations, Debug and Release. Ten of the project files (.csproj) are generated reasonably frequently using an external tool and the templates for that generation include the Debug and Release configurations. Modifying the templates to include additional configurations is not really an option.
So the problem I have is that I have a new project that is hosted in AppFabric/IIS. I'm using Web.Config transformations to update the Web.Config for deployment to 4 different environments: Development, Test, Staging, Production. I've add those contexts to the specific project using the Configuration Manager. That works so I can update the Configuration Manager for e.g. the Release build to use the Production context for the project. Right-clicking and building a deployment package for the project results in the appropriate transformation being applied to the Web.Config.
Now I want to automate the process so I have an MSBuild script:
<ItemGroup>
<BuildMode Include="Dev"/>
<BuildMode Include="Test"/>
<BuildMode Include="Staging"/>
<BuildMode Include="Prod"/>
</ItemGroup>
<Target Name="Build" DependsOnTargets="Package"></Target>
<!--
Build deployment package for each target environment
-->
<Target Name="Package" Outputs="%(BuildMode.Identity)">
<Message Text="Building %(BuildMode.Identity)"/>
<MSBuild Projects="..\SynchWorkflow\SynchWorkflow.csproj"
Targets="Package"
Properties="Platform=AnyCPU;Configuration=%(BuildMode.Identity);"/>
</Target>
Unfortunately this errors because it is trying to build e.g. a Prod configuration when it doesn't exist - Prod is only a context for the SynchWorkflow project. What I want to do is have the Prod context package generated using the Release configuration. Is that possible using the MSBuild task? Is there an extra setting I can provide to the MSBuild task in the Properties attribute that would allow this?
I added new solution configurations for each environment (without adding new project configurations) and then used Configuration Manager to set the contexts to Release for the dependent projects. Unfortunately this didn't work because the MSBuild task was building against the project file and not the solution. Resolved using Julien Hoarau's SO answer. Adding the new solution configurations was the correct approach but the linked answer closed the loop for me. The build script has been updated to the following:
<ItemGroup>
<BuildMode Include="Development"/>
<BuildMode Include="Test"/>
<BuildMode Include="Staging"/>
<BuildMode Include="Production"/>
</ItemGroup>
<PropertyGroup>
<PackageLocation>$(MSBuildProjectDirectory)</PackageLocation>
</PropertyGroup>
<Target Name="Build" DependsOnTargets="Package"></Target>
<!--
Build deployment package for each target environment
-->
<Target Name="Package" Outputs="%(BuildMode.Identity)">
<Message Text="Building %(BuildMode.Identity)"/>
<MSBuild Projects="..\SynchWorkflow.sln"
Properties="Platform=Any CPU;
Configuration=%(BuildMode.Identity);
DeployOnBuild=true;
DeployTarget=Package;
PackageLocation=$(PackageLocation)\SynchWorkflow.%(BuildMode.Identity)Package.zip;"/>
</Target>
The script builds against the solution file and generates the correct package for each target environment. For the purposes of the example script I'm creating the packages in the MSBuildProjectDirectory which is a bit suboptimal.

TeamCity & MSpec with sln2008 runner?

I'm currently using the sln2008 runner. Is there a way to configure TeamCity to execute MSpec tests without switching to a NAnt or MSBuild runner?
I've never done it, but you could probably add a post build Exec task that just shelled out to mspec.exe. Just throw the code from my answer linked to above (How to integrate MSpec with MS Build?) in your specs csproj and add DependsOnTargets="RunSpecs" to your AfterBuild target:
<Target Name="RunSpecs">
<PropertyGroup>
<MSpecCommand>
lib\machine\specifications\Machine.Specifications.ConsoleRunner.exe $(AdditionalSettings) path\to\your\project\bin\Debug\Your.Project.Specs.dll path\to\your\other\project\bin\Debug\Your.Other.Project.dll
</MSpecCommand>
</PropertyGroup>
<Message Importance="high" Text="Running Specs with this command: $(MSpecCommand)"/>
<Exec Command="$(MSpecCommand)" />
</Target>
<Target Name="AfterBuild" DependsOnTargets="RunSpecs">
</Target>
You may use msbuild runner. Please see How to integrate MSpec with MS Build? for description on how to integrate msbuild and mspec

How can we display a "step" inside Visual Studio build process?

When you are monitoring the TFS build from Visual Studio (2008 or 2005), you can see where it is up to.
The issue is that I have some Post-Build custom steps I would like the developer to be able to see directly throught the UI. Those steps take some times and we can also get a "timing" of the build step.
Any idea how to have it displayed?
This is the pattern that I normally use for adding steps to the build report in TFS 2008. (See http://code.msdn.microsoft.com/buildwallboard/ for the full example that I usually use in my Team Build talks)
Basically, the magic is that there is a custom task provided for you in TFS2008 called "BuildStep". Here is the section where I generate and MSI installer and build the appropriate build steps in the report:
<Target Name="PackageBinaries">
<!-- create the build step -->
<BuildStep TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
BuildUri="$(BuildUri)"
Message="Creating Installer"
Condition=" '$(IsDesktopBuild)' != 'true' " >
<Output TaskParameter="Id"
PropertyName="InstallerStepId" />
</BuildStep>
<!-- Create the MSI file using WiX -->
<MSBuild Projects="$(SolutionRoot)\SetupProject\wallboard.wixproj"
Properties="BinariesSource=$(OutDir);PublishDir=$(BinariesRoot);Configuration=%(ConfigurationToBuild.FlavourToBuild)" >
</MSBuild>
<!-- If we sucessfully built the installer, tell TFS -->
<BuildStep TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
BuildUri="$(BuildUri)"
Id="$(InstallerStepId)"
Status="Succeeded"
Condition=" '$(IsDesktopBuild)' != 'true' " />
<!-- Note that the condition above means that we do not talk to TFS when doing a Desktop Build -->
<!-- If we error during this step, then tell TFS we failed-->
<OnError ExecuteTargets="MarkInstallerFailed" />
</Target>
<Target Name="MarkInstallerFailed">
<!-- Called by the PackageBinaries method if creating the installer fails -->
<BuildStep TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
BuildUri="$(BuildUri)"
Id="$(InstallerStepId)"
Status="Failed"
Condition=" '$(IsDesktopBuild)' != 'true' " />
</Target>
So initially, I create the build step and save the Id of the step in a propery called InstallerStepId. After I have performed my task, I set the status of that step to Succeeded. If any errors occur during the step then I set the status of that step to Failed.
Good luck,
Martin.
Note that in #Martin Woodward's great example, PackageBinaries is one of the existing TFS build targets. If you want to use your own targets, you can use the CallTarget task to call them from one of the known targets, e.g.,
<Target Name="AfterDropBuild">
<CallTarget Targets="CreateDelivery"/>
<CallTarget Targets="CreateInventory"/>
</Target>
Then in your targets (e.g., CreateDelivery) use the BuildStep task as per Martin's example.

Resources