powershell - Pester 5.0.2 BeforeAll 块代码未显示在描述块中
问题描述
我最近切换到 Pester 5.0.2 开始对我编写的 Powershell 脚本进行一些测试。我能够闯入 BeforeAll 块,并且按照我的预期填充路径,但是当我到达 Describe 块中的 Get-ChildItem 区域时,来自 BeforeAll 的变量为 $null。
BeforeAll {
$testDir = Split-Path $PSCommandPath -Parent
$prodDir = Split-Path $testDir -Parent
}
Describe "Tests - All Files in prod" {
Get-ChildItem $prodDir -File | ForEach-Object {
$fileName = (Split-Path $_ -leaf).PadRight(20, ' ')
$filePath = $_.PSPath
It "Vars not declared or changed between function definitions" {
[System.Collections.ArrayList]$preSource = (Get-Variable).Name
. "$filePath" -Sourcing
[System.Collections.ArrayList]$postSource = (Get-Variable).Name
$postSource.Remove('preSource')
$postSource.Remove('UnderTest')
[array]$diff = Compare-Object $preSource $postSource -PassThru
$diff | Should -BeNullOrEmpty
}
It "$fileName all #*, #?, #TODO tags are removed" {
$_ | Should -Not -FileContentMatch $([regex]::escape('#TODO'))
$_ | Should -Not -FileContentMatch $([regex]::escape('#*'))
$_ | Should -Not -FileContentMatch $([regex]::escape('#?'))
}
}
}
解决方案
我找到了答案,但在文档中很难找到。由于新的“发现”功能,这不再起作用。这在所有 Describe、It、Context、BeforeAll、AfterAll 等块之前运行。然后丢弃此处运行的代码。
我最终使用 -TestCase 参数使用测试用例来解决问题。
编辑:
下面的版本带有关于它为什么不起作用的评论,然后是一个关于如何设置它的示例。
BeforeAll {
# This block will run AFTER discovery. Anything you need to be evaluated in front of the tests should
# be placed here
$testDir = Split-Path $PSCommandPath -Parent
$prodDir = Split-Path $testDir -Parent
}
$iWillDisappearAfterDiscovery = 'Byebye'
Describe "Describing your tests" {
# Code placed here will be run DURING discovery, and will not be availble at runtime. Placing the code from
# BeforeAll here instead would not work as $testDir and $prodDir would be empty when the tests ran.
#
# The apparent exception to this rule is that the tests are done DURING discovery and then stashed for when
# the tests run. That means your test cases can be set up here if not done directly inside the -TestCases parameter
#
Get-ChildItem $prodDir -File | ForEach-Object {
$fileName = (Split-Path $_ -leaf).PadRight(20, ' ')
$filePath = $_.PSPath
It "Vars not declared or changed between function definitions" {
[System.Collections.ArrayList]$preSource = (Get-Variable).Name
. "$filePath" -Sourcing
[System.Collections.ArrayList]$postSource = (Get-Variable).Name
$postSource.Remove('preSource')
$postSource.Remove('UnderTest')
[array]$diff = Compare-Object $preSource $postSource -PassThru
$diff | Should -BeNullOrEmpty
}
It "$fileName all #*, #?, #TODO tags are removed" {
$_ | Should -Not -FileContentMatch $([regex]::escape('#TODO'))
$_ | Should -Not -FileContentMatch $([regex]::escape('#*'))
$_ | Should -Not -FileContentMatch $([regex]::escape('#?'))
}
}
}
# Here's a correct example:
BeforeAll {
$testDir = Split-Path $PSCommandPath -Parent
$prodDir = Split-Path $testDir -Parent
}
Describe "Describing your tests" {
# You can still set them up dynamically as test cases
[System.Collections.ArrayList]$testCases = @()
Get-ChildItem $prodDir -File | ForEach-Object {
$fileName = (Split-Path $_ -leaf).PadRight(20, ' ')
$filePath = $_.PSPath
$testCases.Add({fileName = $fileName; filePath = $filePath})
}
It "<fileName> Exists" -TestCases $testCases {
#using <varName> will dynamically name your tests with data
# from your test cases
$filePath | Should -Exist
}
It "<fileName> all #*, #?, #TODO tags are removed" {
$filePath | Should -Not -FileContentMatch $([regex]::escape('#TODO'))
$filePath | Should -Not -FileContentMatch $([regex]::escape('#*'))
$filePath | Should -Not -FileContentMatch $([regex]::escape('#?'))
}
Context "Different Context, Run different tests" {
$testCases = @{filename = 'file4'.PadRight(20, ' '); filepath = '/path/to/file4.ps1' },
@{filename = 'file5'.PadRight(20, ' '); filepath = '/path/to/file5.ps1' }
It "Exists" {
$filePath | Should -Exist
}
}
Context "You can also set them up inline like this" -TestCases @(
$testCases = @{filename = 'file1'.PadRight(20, ' '); filepath = '/path/to/file1.ps1' },
@{filename = 'file2'.PadRight(20, ' '); filepath = '/path/to/file2.ps1' },
@{filename = 'file3'.PadRight(20, ' '); filepath = '/path/to/file3.ps1' }
) {
It "Run some tests" {
$fileName | Should -BeOfType [string]
}
}
}
推荐阅读
- c# - 将 Null 传递给 ActionResult 会导致错误,但逻辑似乎是正确的
- javascript - 如何排除对象的某些属性
- php - 保存时间戳和 IP 地址
- android - 如何在 Android 9 中检测长按电源按钮
- c# - 使用数据注释从双精度类型转换为货币
- javascript - Visual Studio Code 打破了自闭标签
- ios - Swift 4 - 无法调用委托方法
- dialogflow-es - Dialogflow 日志在 stackdriver 中给了我一个没有逗号的 JSON
- reactjs - 如何从 .JSON 文件导入数据并在 React 中对其进行迭代?
- python - 使用张量流回归