visual-studio - 在 Visual Studio 中运行 Pester 测试

标签 visual-studio powershell

安装 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/

相关文章:

asp.net - 如何使用 PowerShell 了解托管在 IIS 中的特定网站的事件 session 计数

c++ - 为什么Visual Studio将i++帖子增量作为自动完成循环的默认值?

c# - 如何使用 Visual C# 从 SQL Server 表中获取 ID 号

visual-studio - 根据智能感知在同一个项目中找不到命名空间,但项目构建

c++ - 在 Visual Studio 中调试 64 位转储

asp.net - MS Exchange powershell命令创建了错误的电子邮件ID

powershell - 为什么 “where”不能按我期望的在这里选择某些结果?