TeamCity 9.1.x 现在支持 NUnit 3.0,但是您必须安装运行程序并在步骤中指定 nunit3.console.exe 的路径。我的问题是我在哪里复制 nunit3-console.exe?我必须把这个放在所有的代理上吗?我是否将它放在我的 TeamCity 主服务器上的目录中,它会被代理共享或拉取?似乎没有很好的文档说明将这些文件复制到何处以便所有代理都可以使用它们。
8 回答
您应该在要运行 NUnit 测试的每个代理上都有 NUnit 控制台。
最好的选择是:
添加对 NuGet 包的引用 ( https://www.nuget.org/packages/NUnit.Runners/ )。
要恢复包,您可以使用“NuGet Installer”构建步骤,请参阅以下博客文章:https ://blog.jetbrains.com/teamcity/2013/08/nuget-package-restore-with-teamcity/
之后,您只需从还原的 NuGet 包中设置路径,如“packages\NUnit.Console.3.0.0\tools\nunit3-console.exe”。
基于@NikolayP 的回答:
- 添加对 NuGet 包的引用 ( https://www.nuget.org/packages/NUnit.Runners/ )。
- 要恢复包,您可以使用“NuGet Installer”构建步骤,请参阅以下博客文章:https ://blog.jetbrains.com/teamcity/2013/08/nuget-package-restore-with-teamcity/
- 之后,您只需从还原的 NuGet 包中设置路径,如“packages\NUnit.Console.3.0.0\tools\nunit3-console.exe”。
我编写了以下 PowerShell 脚本来确定正确的 NUnit.ConsoleRunner 包目录并在 NUnit 任务运行之前填充 TeamCity 变量。它使用最新版本的 NUnit.Console 包。
$SrcDirectory = "%src.directory%"
$PackagesDirectory = Join-Path $SrcDirectory packages
$NUnitConsoleRunnerPackageDirectory = Get-ChildItem (Join-Path $PackagesDirectory NUnit.ConsoleRunner.*) | %{
@{
Directory = $_.FullName
Version = [Version]::Parse(($_.Name -replace "NUnit.ConsoleRunner.",""))
}
} | Sort-Object Version -Descending | Select-Object -First 1 | %{ $_.Directory }
if (!$NUnitConsoleRunnerPackageDirectory) {
throw [IO.DirectoryNotFoundException] "NUnit console runner package directory not found"
}
Write-Output "##teamcity[setParameter name='nunit.consolerunner.directory' value='$NUnitConsoleRunnerPackageDirectory']"
请注意,您需要定义src.directory
变量以指向包含packages
构建代理上的目录的目录,或者提供必要的根目录以使 PowerShell 脚本正常工作。您还需要nunit.consolerunner.directory
使用默认值为空来定义变量。
如果由于某种原因找不到 NUnit.ConsoleRunner 目录,该脚本也会抛出异常。
Build is running on an agent, so you need to install NUnit3 on all of the agents where you want to run a build.
还建立在@NikolayP 的回答之上:
NuGet 当前支持操作的命令行-ExcludeVersion
参数install
。从文档:
将包安装到仅以包名称而不是版本号命名的文件夹中。
这导致在后续 NUnit 运行器构建步骤中相当容易使用的路径,并允许放弃 @NathanAldenSr 的巧妙解决方法。
从 TeamCity 2017.1.3(可能还有更早的版本)开始,此功能甚至作为NuGet 安装程序运行程序的参数公开(请参阅还原选项),但需要解决方案路径。下面的示例适用于 NUnit 的通用即时和临时安装。
为了方便复制和粘贴(根据您的要求调整 NUnit 版本):
- 可执行文件:
%teamcity.tool.NuGet.CommandLine.DEFAULT%\tools\nuget.exe
- 参数:
install NUnit.Console -Version 3.7.0 -ExcludeVersion -OutputDirectory %system.teamcity.build.tempDir%\NUnit
TeamCity 运行器有一些陷阱 - 具体来说,它的默认行为不是按照 NUnit2(和 NUnit3 Visual Studio 测试适配器)在他们自己的 AppDomains 中使用他们自己的基本目录运行规范。
TeamCity 9.x 构建系列中有一个(当前未记录的)配置属性,可让您更改此行为。我在这里写过。
尝试最新版本的脚本@NathanAldenSr
仍然需要变量http://teamcityserver/admin/editProject.html?projectId=yourId&tab=projectParams 将nunit.consolerunner.directory参数添加到配置参数
$SrcDirectory = "%teamcity.build.checkoutDir%"
$PackagesDirectory = Join-Path $SrcDirectory packages
Write-Output "PackagesDirectory" $PackagesDirectory
$NUnitConsoleRunnerPackageDirectory = Get-ChildItem (Join-Path $PackagesDirectory NUnit.ConsoleRunner.*) | %{
@{
Directory = $_.FullName
Version = [Version]::Parse(($_.Name -replace "NUnit.ConsoleRunner.",""))
}
} | Sort-Object Version -Descending | Select-Object -First 1 | %{ $_.Directory }
if (!$NUnitConsoleRunnerPackageDirectory) {
throw [IO.DirectoryNotFoundException] "NUnit console runner package directory not found"
}
$NUnitConsoleRunnerPackageDirectory = Join-Path $NUnitConsoleRunnerPackageDirectory tools
Write-Output "NUnitConsoleRunnerPackageDirectory" $NUnitConsoleRunnerPackageDirectory
Write-Output "##teamcity[setParameter name='nunit.consolerunner.directory' value='$NUnitConsoleRunnerPackageDirectory']"
我知道现在是 2018 年 7 月,但这些答案对我来说都不清楚。为什么我需要在每个代理上安装一个控制台。一定有更好的方法。当我为运行测试添加构建步骤时,我注意到 NUnit 控制台工具路径的输入下的文本显示“支持相对于检出目录的路径”。我所做的只是将 nuget 包添加到我的解决方案中的测试项目中。我添加了 NUnit 版本 v3.10.1,然后添加了 NUnit.Console v3.8.0。然后在 Team City 我简单地添加了相对路径“packages\NUnit.ConsoleRunner.3.8.0\tools\nunit3-console.exe”