我有一个用于一些 Playwright 测试的 NUnit 测试项目,我正在测试一个需要登录的网页。因为它需要登录,所以我需要使用一些用户名和密码。这些测试都应该作为发布管道的一部分运行,但也应该可以通过端点使用,以便它们可以作为 CRON 作业触发。我的整个解决方案如下所示:
像这样
appsettings.json
:
{
"TestUser": {
"Username": "",
"Password": ""
}
}
在测试类中
AdminWebTests.cs
我通过构造函数设置要在测试中使用的用户名和密码,方法是注入ConfigurationBuilder
并读取appsettings.json
文件:
public class AdminWebTests : PageTest
{
private readonly string _testUserName;
private readonly string _testUserPassword;
public AdminWebTests()
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(@"appsettings.json", false, false)
.Build();
_testUserName = configuration.GetSection("TestUser:Username").Value;
_testUserPassword = configuration.GetSection("TestUser:Password").Value;
}
}
这在本地工作得很好,但我也需要它在管道中工作。因此,在我的管道脚本中,我包含了变量组,其中包含要替换的变量和用于替换变量的 FileTransform 任务:
# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4
resources:
pipelines:
- pipeline: playwright
source: some-other-pipeline
branch: master
trigger:
branches:
- master
jobs:
- job: Delay
pool: Server
steps:
- task: Delay@1
inputs:
delayForMinutes: "1"
- job: Build
dependsOn: Delay
condition: succeeded()
pool:
vmImage: "windows-latest"
variables:
solution: "packages/tests/playwright/*.sln"
buildPlatform: "Any CPU"
buildConfiguration: "Release"
root_playwright: "packages/tests/playwright"
group: playwright-ci
steps:
- task: UseDotNet@2
displayName: "Set SDK version"
inputs:
packageType: "sdk"
version: "6.0.x"
includePreviewVersions: true
- task: NuGetToolInstaller@1
displayName: "Install nuget"
inputs:
checkLatest: true
- task: NuGetCommand@2
displayName: "Restore"
inputs:
command: "restore"
restoreSolution: $(solution)
- task: FileTransform@1
inputs:
folderPath: "$(root_playwright)/Playwright"
fileType: "json"
targetFiles: "appsettings.json"
- task: DotNetCoreCLI@2
displayName: "Build"
inputs:
command: "build"
projects: $(solution)
arguments: "--configuration $(buildconfiguration) --no-restore"
- task: PowerShell@2
displayName: Download Playwright Browsers
inputs:
targetType: inline
script: |
cd $(root_playwright)/Playwright
dotnet build
dotnet tool install --global Microsoft.Playwright.CLI
playwright install
- task: dotnetcorecli@2
displayName: "Test"
inputs:
command: "test"
projects: |
$(root_playwright)/Playwright/*.csproj
arguments: '--configuration $(buildconfiguration) --no-build --collect "code coverage"'
现在的问题是
appsettings.json
文件中的变量似乎没有被替换。事实上,配置似乎根本没有被读取。因为如果我尝试打印 _testUserName
和 _testUserPassword
设置的任何值,它们都是空白的。如果我在代码中设置它们并且不通过配置读取它们,那么这些值将作为实际的用户名和密码打印出来,这样我就知道日志记录/打印工作正常。无论我在代码中的 appsettings.json
文件中放入什么,在管道上运行时变量都会显示为空白,但在本地运行良好。 appsettings.json
被复制到输出目录,并且管道日志在 FileTransform 步骤期间指出 JSON variable substitution applied successfully.
。有什么我错过的吗?
更新:我尝试发布构建工件,发现变量
appsettings.json
文件实际上没有被替换,而是保持原样。我还尝试将 FileTransform 阶段放在构建阶段之后,并对 appsettings.json
目录中输出的 bin/Release/net6.0
运行转换,但没有帮助。
您可以使用市场上的替换代币任务。 https://marketplace.visualstudio.com/items?itemName=qetza.replacetokens
如果你使用 $() 作为变量,它看起来会更像这样:
- task: replacetokens@5
inputs:
targetFiles: '**/appsettings.json'
encoding: 'auto'
tokenPattern: 'azpipelines'
writeBOM: false
actionOnMissing: 'warn'
keepToken: false
actionOnNoFiles: 'continue'
enableTransforms: false
enableRecursion: false
useLegacyPattern: false
enableTelemetry: true
事实证明,我只是在管道脚本的
variables
部分中对变量组名称使用了错误的语法。正确的语法是:
variables:
- group: playwright-ci
不是
variables:
group: playwright-ci
同样重要的是,
group
变量是本节中的第一个。
# Run the integration tests
- task: DotNetCoreCLI@2
displayName: "Run Tests"
inputs:
command: test
projects: "$(Build.SourcesDirectory)/**/*Tests.csproj"
env:
# DOTNET_ENVIRONMENT determines which config files are used when running integration tests
DOTNET_ENVIRONMENT: ${{Environment}}
DOTNET_ENVIRONMENT 环境变量不是 DotNetCoreCLI 任务本身的功能,而是 .NET Core 应用程序中的常见做法,用于确定在运行时使用哪些配置文件。
在 .NET Core 中,您通常有针对不同环境(例如开发、登台、生产)的多个配置文件(例如 appsettings.json、appsettings.{Environment}.json)。 DOTNET_ENVIRONMENT 环境变量用于指定当前环境,应用程序使用该值来确定要加载哪些配置文件。
因此,只需分配 env 即可,而不是替换 appsettings 文件,以便管道将选择正确的 appsettings 文件来运行测试。