Visual studio 在VisualStudio中运行Pester测试
安装Visual Studio 2015的Visual studio 在VisualStudio中运行Pester测试,visual-studio,powershell,powershell-tools,Visual Studio,Powershell,Powershell Tools,安装Visual Studio 2015的PowerShell工具后我创建了一个新的PowerShell模块项目,该项目创建了一个MyProject.psd1aMyProject.psm1和一个MyProject.tests.ps1文件 MyProject.tests.ps1文件如下所示 Describe "Connect-Database" { Context "When Connection To Database" { $result = Connect-Datab
PowerShell工具后
我创建了一个新的PowerShell模块项目
,该项目创建了一个MyProject.psd1
aMyProject.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
}
}
}
连接数据库是来自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工具附带了一个测试适配器,我的测试显示在测试资源管理器中
但是如果我执行它,它总是会失败,因为术语Connect Database不被识别为cmdlet函数脚本文件或可操作程序
甚至将
导入模块。\MyProject.psd1
添加到MyProject.tests.ps1
文件也没有帮助。在运行测试之前,您知道如何加载我的模块吗?我在MyProject.tests.ps1文件中添加了Get Location | Write Host
,并确定工作目录是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"))