2015-06-19 53 views
0

我是MSBuild的新手,忙於自動化Visual Studio解決方案的測試。在MSBuild下仿效Devenv/Runexit

我以前曾使用命令行Devenv,它提供了一個方便的/Runexit操作模式。從手冊:

/Runexit (devenv.exe) 
Compiles and runs the specified solution, minimizes the IDE when the solution is run, 
and closes the IDE after the solution has finished running. 

這正是我需要的功能。我現在正在遷移到MSBuild。我發現解決方案中的項目文件可以直接用於構建,因爲默認目標是Build。

我能做些什麼來處理不同的目標,它會產生與/Runexit相同的效果?你能幫助我度過迷宮嗎?

+0

究竟有什麼不同的目標?假設你運行測試的目標是'Test',那麼你將運行'msbuild/t:Rebuild; Test'來首先重建然後測試.. – stijn

+0

@stijn我的問題正是如何創建一個目標來執行結果構建,即'Test.exe'。項目文件中默認沒有這樣的目標。 –

回答

2

這是運行一個項目的輸出文件的最基本目標:

<Target Name="RunTarget"> 
    <Exec Command="$(TargetPath)" /> 
</Target> 

對於C++單元測試我使用的是這樣的;這是一個屬性表,所以很容易添加到任何項目而無需手動修改它。它在構建之後自動運行輸出,因此不需要指定額外的目標,並且它對VS和命令行的作用相同。此外,在VS中,您將從錯誤列表中立即得到Unittest ++或Catch等框架中出現的unittest錯誤,因此您可以雙擊它們。此外,UnitTestExtraPath屬性可以在其他地方設置以防萬一(例如,在buildserver上,我們總是希望保持PATH乾淨,但有時我們確實需要修改它以運行構建的exes)。

<?xml version="1.0" encoding="utf-8"?> 
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <ImportGroup Label="PropertySheets" /> 
    <PropertyGroup Label="UserMacros" /> 
    <PropertyGroup /> 
    <ItemDefinitionGroup /> 
    <ItemGroup /> 
    <!--Used to be AfterTargets="AfterBuild", but that is unusable since a failing test marks the build as unsuccessful, 
     but in a way that VS will always try to build again. As a consequence debugging in VS is impossible since 
     VS will build the project before starting the debugger but building fails time and time again.--> 
    <Target Name="RunUnitTests" AfterTargets="FinalizeBuildStatus"> 
    <Exec Condition="$(UnitTestExtraPath)!=''" Command="(set PATH=&quot;%PATH%&quot;;$(UnitTestExtraPath)) &amp; $(TargetPath)" /> 
    <Exec Condition="$(UnitTestExtraPath)==''" Command="$(TargetPath)" /> 
    </Target> 
</Project> 
+1

現在就開始工作。你做了我的一天,謝謝。我必須添加'WorkingDirectory'屬性和拼寫完全像' ' –