安装 PowerShell Tools for Visual Studio 2015
后,我创建了一个新的 Powershell 模块项目
,它创建了一个 MyProject.psd1
和一个 MyProject .psm1
和一个 MyProject.tests.ps1
文件。
MyProject.tests.ps1 文件如下所示
Describe "Connect-Database" {
Context "When Connection To Database" {
$result = Connect-Database -host localhost -user admin -pass pass
It "Should Return True" {
$result | Should Be $True
}
}
}
Connect-Database 是 MyProject.psm1
中的一个函数,通过 MyProject.psd1
导出
# Functions to export from this module
FunctionsToExport = 'Connect-Database'
运行一个 powershell 控制台并执行
Import-Module .\MyProject.psd1
Invoke-Pester
效果很好,有返回
Describing Connect-Database
Context When Connection To Database
[+] Should Return True 627ms
Tests completed in 627ms
Passed: 1 Failed: 0 Skipped: 0 Pending: 0
我的问题来了:PowerShell 工具带有一个测试适配器,我的测试显示在测试资源管理器中。
但是如果我执行它,它总是失败并显示The term Connect-Database is not recognized as cmdlet function script file or operable program
即使将 Import-Module .\MyProject.psd1
添加到 MyProject.tests.ps1
文件也无济于事。关于如何在运行测试之前加载我的模块有什么想法吗?
最佳答案
我添加了 Get-Location | Write-Host
到 MyProject.tests.ps1 文件,发现工作目录是 C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE
我一开始没有检查,因为我相信测试适配器只会在工作目录中执行 Invoke-Pester
最终我用
解决了这个问题Split-Path $PSCommandPath | Set-Location
Import-Module (".\" + (Split-Path -Leaf $PSCommandPath).Replace(".tests.ps1", ".psd1"))
关于visual-studio - 在 Visual Studio 中运行 Pester 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36938108/