如何在测试脚本中从项目加载模块?

时间:2015-08-01 13:57:17

标签: powershell plugins visual-studio-2015

Fiddle

我无法加载Get-Function加载Module.psm1

这是我的function Get-Function { return $true } 。我添加一个返回Module.psd1只是为了看它是否会被测试运行员选中。

这是FunctionsToExport = 'Get-Function'

Module.tests.ps1

Describe "Get-Function" { Context "Function Exists" { Import-Module .\Module.psm1 It "Should Return" { Get-Function | Should Be $true } } }

------ Run test started ------
Describing Get-Function
   Context Function Exists
    [-] Should Return 731ms
The term 'Get-Function' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
at line: 12 in C:\Users\Adam\Documents\code\Powershell\TestModuleProject\TestModuleProject\Module.tests.ps1
Tests completed in 731ms
Passed: 0 Failed: 1 Skipped: 0 Pending: 0 
========== Run test finished: 1 run (0:00:08.1834099) ==========

new_dict = {v: k for k,v in old_dict.items()}

SELECT `product_id` 
FROM `filter` 
WHERE `filter_id` IN (2,4)
GROUP BY `product_id`
HAVING COUNT(DISTINCT `filter_id`)=2

在我可以编写测试之前,是否必须构建模块并将其加载到我的模块路径中?或者有没有办法引用它相对于测试路径的位置?

我在输出窗口中看到的结果是:

product_id
----------
68

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

我怀疑工作目录(.)与运行测试时文件所在的目录不同。您可以使用$MyInvocation.MyCommand.Path来确定测试脚本的目录。

Pester单元测试(这是你的测试代码和输出的样子)通常包含这样一行:

$here = Split-Path -Parent $MyInvocation.MyCommand.Path

尝试更改测试代码,如下所示:

$here = Split-Path -Parent $MyInvocation.MyCommand.Path

Describe "Get-Function" {
    Context "Function Exists" {
        Import-Module "$here\Module.psm1"
        It "Should Return" {
            Get-Function | Should Be $true
        }
    }
}