Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio 在VisualStudio中运行Pester测试_Visual Studio_Powershell_Powershell Tools - Fatal编程技术网

Visual studio 在VisualStudio中运行Pester测试

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

安装Visual Studio 2015的
PowerShell工具后
我创建了一个新的
PowerShell模块项目
,该项目创建了一个
MyProject.psd1
a
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
        }
    }
}
连接数据库是来自
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"))