我想在Jenkins持續集成服務器上構建ASP.NET Core解決方案。如何在Jenkins上構建和測試ASP.NET Core解決方案
,我需要運行的步驟是:
- DOTNET恢復
- 構建解決方案:DOTNET編譯或msbuild14?
- 運行測試:測試DOTNET
- 測試覆蓋率
任何人知道或有腳本來完成點2至4?
我想在Jenkins持續集成服務器上構建ASP.NET Core解決方案。如何在Jenkins上構建和測試ASP.NET Core解決方案
,我需要運行的步驟是:
任何人知道或有腳本來完成點2至4?
你必須記住的是詹金斯從哪個目錄執行。 dotnet restore
可以在根目錄下運行,但dotnet build
和dotnet test
需要從與project.json
相同的目錄運行。
測試覆蓋率完全是一個單獨的主題 - 截至目前(2015年2月1日)Visual Studio Enterprise 2015中,代碼覆蓋率不起作用,至少在XUnit中,可能它與MSTest一起使用。 dotCover現在正在工作,但我不知道如何編寫腳本並獲得結果。
第一個構建操作應該是執行dotnet restore
。
powershell.exe -NoProfile -ExecutionPolicy Bypass ./TestsAndCoverage.ps1
我使用下面的腳本在Windows中詹金斯單元測試和代碼覆蓋率:
$testProjects = "Dangl.Calculator.Tests"
$testFrameworks = "net461", "net46", "netcoreapp1.0", "netcoreapp1.1"
# Get the most recent OpenCover NuGet package from the dotnet nuget packages
$nugetOpenCoverPackage = Join-Path -Path $env:USERPROFILE -ChildPath "\.nuget\packages\OpenCover"
$latestOpenCover = Join-Path -Path ((Get-ChildItem -Path $nugetOpenCoverPackage | Sort-Object Fullname -Descending)[0].FullName) -ChildPath "tools\OpenCover.Console.exe"
# Get the most recent OpenCoverToCoberturaConverter from the dotnet nuget packages
$nugetCoberturaConverterPackage = Join-Path -Path $env:USERPROFILE -ChildPath "\.nuget\packages\OpenCoverToCoberturaConverter"
$latestCoberturaConverter = Join-Path -Path (Get-ChildItem -Path $nugetCoberturaConverterPackage | Sort-Object Fullname -Descending)[0].FullName -ChildPath "tools\OpenCoverToCoberturaConverter.exe"
$testRuns = 1;
foreach ($testProject in $testProjects){
foreach ($testFramework in $testFrameworks) {
# Arguments for running dotnet
$dotnetArguments = "test", "-f $testFramework", "`"`"$PSScriptRoot\test\$testProject`"`"", "-xml result_$testRuns.testresults"
"Running tests with OpenCover"
& $latestOpenCover `
-register:user `
-target:dotnet.exe `
"-targetargs:$dotnetArguments" `
-returntargetcode `
-output:"$PSScriptRoot\OpenCover.coverageresults" `
-mergeoutput `
-excludebyattribute:System.CodeDom.Compiler.GeneratedCodeAttribute `
"-filter:+[Dangl.Calculator*]* -[*.Tests]* -[*.Tests.*]*"
$testRuns++
}
}
"Converting coverage reports to Cobertura format"
& $latestCoberturaConverter `
-input:"$PSScriptRoot\OpenCover.coverageresults" `
-output:"$PSScriptRoot\Cobertura.coverageresults" `
"-sources:$PSScriptRoot"
簡短的摘要:隨後PowerShell腳本通過常規的Windows批處理命令執行的$testProjects
變量定義(在本例中爲一個)要運行的測試項目,它們被假定爲相對於腳本位於./test
文件夾中。它爲所有指定的$testFrameworks
生成測試結果(即,如果我正在測試netstandard1.3
庫,我讓它針對netcoreapp
和net
都運行)。
在詹金斯,我選擇發佈的Cobertura覆蓋報告與**/*Cobertura.coverageresults
和發佈的xUnit淨測試結果與**/*.testresults
。您需要xUnit Plugin和Cobertura Plugin。
該腳本要求您的測試項目對(編譯時間)依賴於OpenCover
和OpenCoverToCoberturaConverter
NuGet包,以生成測試結果並將它們轉換爲Cobertura格式。
這是爲Visual Studio 2015s project.json格式
當你已經切換到的Visual Studio 2017年,您需要更改測試命令:運行dotnet xunit
代替dotnet test
。包括DOTNET的xUnit亞軍像這樣在你的csproj
文件:
<PackageReference Include="xunit" Version="2.3.0-beta2-build3683" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta2-build3683" />
檢查也是您的構建被配置爲輸出調試符號:
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DebugType>full</DebugType>
<DebugSymbols>True</DebugSymbols>
</PropertyGroup>
The project is on GitHub if you want to take a further look。
這隻在Windows上工作到目前爲止。在Linux上,除了代碼覆蓋之外的所有內容都可以工作
我有一個Jenkins作業,它使用Windows Powershell步驟在解決方案文件夾中調用標準名稱的PS腳本 - 此Powershell腳本獲取dotnetcore的PS安裝腳本並運行它。 然後我想用'dotnet恢復','dotnet build'等 事情是:我在本地運行PS很好,它的工作原理。我在Jenkins實例上運行腳本,並且 - 只要dotnetcore在路徑中 - 'dotnet restore'是一個有效的命令,但是我得到的返回碼爲0xC0000135(-1073741515),即「應用程序未能正確初始化(0xc0000135 )「... – Aidanapword
我們做'dotnet構建'和'dotnet測試',他們似乎工作正常。知道你的Jenkins是在Windows還是Unix上可能會有幫助。 – kiml42
@Aidanapword你可以粘貼你的ps腳本樣本? 你正在做單元測試?和jenkins的測試覆蓋率? 如何做到自動化的測試覆蓋率?聲納? –