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

awright18

项目结构

我无法加载获取Module.psm1到加载Module.tests.ps1

这是我的Module.psm1我添加了一个回车,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) ==========

有什么建议?

安斯加·威彻斯(Ansgar Wiechers)

我怀疑工作目录(.)与运行测试时文件所在的目录不同。您可以$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
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Elixir / mix项目中加载Erlang模块?

如何在Stackblitz项目中加载JSON?

如何在简单的Java项目中加载图像?

如何在Moqui项目中加载外部jar库?

如何在Babylonjs Reactjs项目中加载gltf模型

如何在适当的项目中的多个项目中加载资源路径?

如何在多模块项目中访问 gradle 中的测试 jar

如何在NodeJs项目中添加构建脚本和测试

模块无法在新的Django项目中加载

如何使用Qt Creator在多个项目中加载cmake脚本

如何在joomla 2.5中的组件中加载模块

如何在 Rails 中的视图中加载咖啡脚本?

如何在 Laravel 项目中加载另一个 Console/Kernel.php?

如何在Angular项目中加载图像(和其他资产)?

如何在React项目中加载Google Places JS API库?

如何在Hyperledger Composer项目中加载示例数据

如何在Spring项目中知道从中加载bean的资源。

如何在initrd中加载模块?

如何在Linux中加载tun模块?

如何在测试中加载表单类型

如何在puppeteer中加载脚本?

如何在React组件中加载脚本

Xamarin:如何从iOS库项目中加载图像

如何在ajax中加载多个项目?

如何在用Java编写的多模块播放项目中运行单元测试

脚本编译后如何在python中重新加载模块?

如何防止模块在Zend Framework 2 phpunit测试中加载

如何在maven多模块项目中访问其他模块pom中定义的属性

在移动浏览器中加载页面时,如何在所选项目中显示长字符串