我正在尝试使用 Pesters TestDrive 创建自定义文件管理 powershell 函数的测试。但是,我没有以任何方式运行它,总是收到 TestDrive 不存在的错误。
即使使用文档中的示例: https: //pester.dev/docs/usage/testdrive
我创建了一个文件“pester.tests.ps1”,其中仅包含示例:
function Add-Footer($path, $footer) {
Add-Content $path -Value $footer
}
Describe "Add-Footer" {
$testPath = "TestDrive:\test.txt"
Set-Content $testPath -value "my test text."
Add-Footer $testPath "-Footer"
$result = Get-Content $testPath
It "adds a footer" {
(-join $result) | Should -Be "my test text.-Footer"
}
}
Run Code Online (Sandbox Code Playgroud)
出现的错误:
Starting discovery in 1 files. Set-Content : Cannot find drive. A drive with the name 'TestDrive' does not exist. At ...\pester.tests.ps1:7 char:5
+ Set-Content $testPath -value "my test text."
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (TestDrive:String) [Set-Content], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetContentCommand Add-Content : Cannot find drive. A drive with the name 'TestDrive' does not exist. At ...\pester.tests.ps1:2 char:5
+ Add-Content $path -Value $footer
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (TestDrive:String) [Add-Content], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.AddContentCommand Get-Content : Cannot find drive. A drive with the name 'TestDrive' does not exist. At ...\pester.tests.ps1:9 char:15
+ $result = Get-Content $testPath
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (TestDrive:String) [Get-Content], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Discovery finished in 46ms. [-] Add-Footer.adds a footer 10ms (8ms|2ms) Expected strings to be the same, but they were different. Expected length: 20 Actual length: 0 Strings differ at index 0. Expected: 'my test text.-Footer' But was: '' at (-join $result) | Should -Be "my test text.-Footer", ...\pester.tests.ps1:12 at <ScriptBlock>, ...\pester.tests.ps1:12 Tests completed in 152ms Tests Passed: 0, Failed: 1, Skipped: 0 NotRun: 0
Run Code Online (Sandbox Code Playgroud)
我是不是忘记了什么?还有其他先决条件吗?我已经更新了 Pester 和 Powershell。
Pester v5 最近发布了,它对 Pester 的运行方式进行了相当大的改变,提前解释了测试。因此,如何构建测试需要进行一些重大更改,其中之一是测试的设置需要通过 orbeforeall块完成beforeeach。
因此,您的示例的重写是有效的:
function Add-Footer($path, $footer) {
Add-Content $path -Value $footer
}
Describe "Add-Footer" {
BeforeAll {
$testPath = "TestDrive:\test.txt"
Set-Content $testPath -value "my test text."
}
It "adds a footer" {
Add-Footer $testPath "-Footer"
$result = Get-Content $testPath
(-join $result) | Should -Be "my test text.-Footer"
}
}
Run Code Online (Sandbox Code Playgroud)
关于 Pester v5 如何影响 TestDrive有一个悬而未决的问题,我刚刚向它添加了一条注释,以指出它的文档示例不再有效。
| 归档时间: |
|
| 查看次数: |
1081 次 |
| 最近记录: |