2016-04-29 118 views
2

安装PowerShell Tools for Visual Studio 2015后,我创建了一个新的Powershell Module Project,它创建了一个MyProject.psd1和一个MyProject.tests.ps1文件。在Visual Studio中运行Pester测试

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的工具附带测试适配器和我的测试是在试验资源管理器。

,但如果我执行它,它总是失败,The term Connect-Database is not recognized as cmdlet function script file or operable program

即使添加Import-Module .\MyProject.psd1MyProject.tests.ps1文件没有帮助。任何想法如何在运行测试之前加载我的模块?

回答

4

我加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")) 
相关问题