Run tsSQLt Tests 任务中存在问题(可以重现此问题)。
源代码(可以在代理工作文件夹中找到,例如:_work/_task/RunDatabaseTestsxxx):
运行数据库测试.ps1:
[CmdletBinding(DefaultParameterSetName = 'None')]
param()
$global:ErrorActionPreference = 'Stop'
Import-Module -Name "$PSScriptRoot\ps_modules\TaskHelpers"
[string]$outputPath = Get-VstsInput -Name OutputPath
[string]$databaseServer = Get-VstsInput -Name DatabaseServer
[string]$databaseName = Get-VstsInput -Name DatabaseName
[bool]$useWindowsAuth = Get-VstsInput -Name UseWindowsAuth -AsBool
[string]$databaseUserName = Get-VstsInput -Name DatabaseUserName
[string]$databasePassword = Get-VstsInput -Name DatabasePassword
Write-VstsTaskVerbose -Message 'Ensuring the path to sqlcmd is present in env:PATH'
Initialize-SqlCmdInPathEnvironmentVariable
[string]$sourcesDirectory = Get-VstsTaskVariable -Name 'Build.SourcesDirectory'
if(!$sourcesDirectory)
{
Write-VstsTaskVerbose -Message 'Build.SourcesDirectory was not found as a VSTS Task variable'
Write-VstsTaskVerbose -Message 'Looking for Agent.ReleaseDirectory to use as source directory instead'
# For RM, look for the test assemblies under the release directory.
$sourcesDirectory = Get-VstsTaskVariable -Name 'Agent.ReleaseDirectory'
}
if(!$sourcesDirectory)
{
$msg = 'No source directory found'
Exit-WithError $msg
}
Write-VstsTaskVerbose -Message 'Source directory found'
Write-VstsTaskDebug -Message "buildSourcesDirectory = $buildSourcesDirectory"
if ([System.IO.Path]::IsPathRooted($outputPath) -eq $False)
{
$outputPath = Join-Path $sourcesDirectory $outputPath
}
Write-VstsTaskDebug -Message "OutputPath(absolute) = $outputPath"
if ($useWindowsAuth -eq $True) {
Write-VstsTaskVerbose -Message 'Starting sqlcmd with Windows Authentication'
Invoke-VstsTool -FileName 'sqlcmd.exe' -Arguments "-S $databaseServer -d $databaseName -i 'RunTests.sql'"
Invoke-VstsTool -FileName 'sqlcmd.exe' -Arguments "-S $databaseServer -d $databaseName -i 'OutputResults.sql' -o $outputPath"
}
else {
Write-VstsTaskVerbose -Message 'Starting sqlcmd with SQL Authentication'
Invoke-VstsTool -FileName 'sqlcmd.exe' -Arguments "-S $databaseServer -d $databaseName -U $databaseUserName -P $databasePassword -i 'RunTests.sql'"
Invoke-VstsTool -FileName 'sqlcmd.exe' -Arguments "-S $databaseServer -d $databaseName -U $databaseUserName -P $databasePassword -i 'OutputResults.sql' -o $outputPath"
}
# sqlcmd limits line length to 2034 characters and inserts new lines. Remove these.
$x = Get-Content $outputPath -Raw
$x.Replace("`r`n", "") > $outputPath
运行测试.sql:
EXEC [tSQLt].[RunAll];
输出结果.sql:
:XML ON
EXEC [tSQLt].[XmlResultFormatter];
我可以通过调用"sqlcmd.exe" -S v-tinmo-12r2 -d ReadyRollDemo2 -U starain -P User@123 -i RunTests.sql
命令来运行测试(从'RunTests.sql'中删除单引号,也可以将单引号替换为双引号))
因此,您可以通过命令行任务调用 sqlcmd.exe 来运行测试并获取报告,也可以参考 Run tSQLt Tests 任务的源代码来自定义构建/发布任务。