简体   繁体   English

Powershell Core + Pester - 将测试与 src 分开

[英]Powershell Core + Pester - Separating tests from src

Question:问题:

What would be the best way to import functions to tests that don't reside in the same directory?将函数导入不在同一目录中的测试的最佳方法是什么?

Example例子

📁 src
      📄 Get-Emoji.ps1
📁 test
      📄 Get-Emoji.Tests.ps1

Inb4 Inb4

  • Pester documentation[1] suggests test files are placed in the same directory as the code that they test. Pester 文档[1] 建议将测试文件放在与他们测试的代码相同的目录中。 No examples of alternatives provided.没有提供替代品的例子。
  • Pester documentation[2] suggests dot-sourcing to import files. Pester 文档[2] 建议使用 dot-sourcing 来导入文件。 Only with examples from within same directory仅来自同一目录中的示例
  • Whether breaking out tests from the src is good practice, is to be discussed elsewhere从 src 中拆分测试是否是好的做法,将在别处讨论
  • Using Powershell Core for cross platform support on different os filesystems (forward- vs backward slash)使用 Powershell 内核在不同的操作系统文件系统上提供跨平台支持(正斜杠与反斜杠)

[1] File placement and naming convention [1] 文件放置和命名约定

Pester considers all files named.Tests.ps1 to be test files. Pester 认为所有名为.Tests.ps1 的文件都是测试文件。 This is the default naming convention that is used by almost all projects.这是几乎所有项目都使用的默认命名约定。

Test files are placed in the same directory as the code that they test.测试文件与它们测试的代码放置在同一目录中。 Each file is called as the function it tests.每个文件都称为它测试的 function。 This means that for a function Get-Emoji we would have Get-Emoji.Tests.ps1 and Get-Emoji.ps1 in the same directory.这意味着对于 function Get-Emoji,我们将在同一目录中拥有 Get-Emoji.Tests.ps1 和 Get-Emoji.ps1。 What would be the best way to referencing tests to functions in Pester.将测试引用到 Pester 中的函数的最佳方法是什么。

[2] Importing the tested functions [2] 导入测试函数

Pester tests are placed in.Tests.ps1 file, for example Get-Emoji.Tests.ps1. Pester 测试放在 .Tests.ps1 文件中,例如 Get-Emoji.Tests.ps1。 The code is placed in Get-Emoji.ps1.代码放在 Get-Emoji.ps1 中。

To make the tested code available to the test we need to import the code file.为了使测试代码可用于测试,我们需要导入代码文件。 This is done by dot-sourcing the file into the current scope like this:这是通过将文件点源到当前的 scope 来完成的,如下所示:

Example 1示例 1

 # at the top of Get-Emoji.Tests.ps1 BeforeAll {. $PSScriptRoot/Get-Emoji.ps1 }

Example 2示例 2

 # at the top of Get-Emoji.Tests.ps1 BeforeAll {. $PSCommandPath.Replace('.Tests.ps1','.ps1') }

I tend to keep my tests together in a single folder that is one or two parent levels away from where the script is (which is usually under a named module directory and then within a folder named either Private or Public).我倾向于将我的测试放在一个文件夹中,该文件夹与脚本所在的位置相距一两个父级别(通常位于命名模块目录下,然后位于名为 Private 或 Public 的文件夹中)。 I just dot source my script or module and use .. to reference the parent path with $PSScriptRoot (the current scripts path) as a point of reference.我只是点源我的脚本或模块,并使用..$PSScriptRoot (当前脚本路径)作为参考点来引用父路径。 For example:例如:

  • Script in \SomeModule\Public\get-something.ps1 \SomeModule\Public\get-something.ps1中的脚本
  • Tests in \Tests\get-something.tests.ps1 \Tests\get-something.tests.ps1中的测试
BeforeAll { 
    . $PSScriptRoot\..\SomeModule\Public\get-something.ps1    
}

Use forward slashes if cross platform compatibility is a concern, Windows doesn't mind if path separators are forward or backslashes.如果需要考虑跨平台兼容性,请使用正斜杠,Windows 不介意路径分隔符是正斜杠还是反斜杠。 You could also run this path through Resolve-Path first if you wanted to be certain a valid full path is used, but I don't generally find that necessary.如果您想确定使用了有效的完整路径,您也可以首先通过Resolve-Path运行此路径,但我通常认为没有必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM