跳到正文
Skillpedia
测试40k · #7319

pester-should-migration

它能做什么

这个工具可以帮你更新用 Pester 编写的代码测试。它会把旧的写法(`Should -Be`)改成新的写法(`Should-Be`),让错误信息更清晰。

适用场景

  • ✓当你想让测试升级到最新版本的 Pester 时。
  • ✓当你需要把旧测试转换为新格式时。
  • ✓当你希望获得更详细、更精确的错误信息时。

技能链接

https://github.com/github/awesome-copilot/blob/main/skills/pester-should-migration/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/github/awesome-copilot/blob/main/skills/pester-should-migration/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“把我代码中的 `Should -Be` 测试转换为 `Should-Be`。”

每周 5 个新技能,1 分钟读完。

工作原理

该工具会查找 `Should -Be` 或 `Should -Not -Be` 之类的旧写法,并替换为新的 `Should-Be` 或 `Should-NotBe`。有些转换需要额外留意,以确保测试的行为保持不变。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Pester Should -* → Should-* Migration

Convert classic Pester v5 assertions (Should -Be, space then parameter) to the new Pester v6 Should-* assertions (Should-Be, hyphen, no space).

Status: experimental / preview. Verified against Pester 6.0.0-rc2. The classic Should -Be style still works in v6, so migrate incrementally and keep the suite green.

Companion skill. This skill covers the optional move to the new Should-* operators. To upgrade a suite across major Pester versions (v3→v4→v5→v6 — the runtime, mocks, and config), use the separate pester-migration skill. In v6 the classic Should -Be keeps working, so adopting Should-* is independent of any version bump.

When to Use

  • Modernizing a Pester suite to the v6 Should-* assertions.
  • A user asks to migrate / convert / rewrite Should -... calls.
  • You want clearer, type-aware failure messages from the new assertions.

Know This First

  • Both syntaxes work side by side in Pester v6. Migration is optional and can be done one test (or one file) at a time. Nothing breaks if you leave some classic.
  • Requires Pester v6+. The Should-* commands do not exist in v5.
  • Negation is a separate command, not a -Not switch: Should -Not -Be → Should-NotBe. There is no -Not parameter on the new assertions.
  • The actual value still comes from the pipeline ($x | Should-Be 1) or from -Actual (Should-Be -Actual $x -Expected 1). -Because carries over unchanged.
  • Most renames are mechanical, but several have behavior changes you must check by hand — see Gotchas.

Procedure

Step 1 — Find the classic assertions

Search the target for the classic space-separated syntax (the tell is Should -, or Should followed by -Not):

Should -          # any classic operator
Should -Not -     # negated classic operator
Assert-MockCalled # also removed in v6 -> Should-Invoke

Limit the scope to PowerShell test files (*.Tests.ps1, *.ps1).

Step 2 — Apply the mapping

Most-used conversions (full list in references/assertion-map.md):

Classic (v5) New (v6)
$x | Should -Be 1 $x | Should-Be 1
$x | Should -Not -Be 1 $x | Should-NotBe 1
$x | Should -BeExactly 'A' $x | Should-BeString 'A' -CaseSensitive
$x | Should -BeGreaterOrEqual 2 $x | Should-BeGreaterThanOrEqual 2
$x | Should -BeLessOrEqual 2 $x | Should-BeLessThanOrEqual 2
$x | Should -BeLike 'a*' $x | Should-BeLikeString 'a*'
$x | Should -Match 're' $x | Should-MatchString 're'
$x | Should -BeOfType [int] $x | Should-HaveType ([int])
$x | Should -BeNullOrEmpty depends — see gotchas (no single equivalent)
$c | Should -HaveCount 3 $c | Should-BeCollection -Count 3
$c | Should -Contain 2 $c | Should-ContainCollection 2
{ ... } | Should -Throw 'msg' { ... } | Should-Throw -ExceptionMessage 'msg'
Should -Invoke Get-Thing Should-Invoke Get-Thing
Should -InvokeVerifiable Should-Invoke -Verifiable

Step 3 — Check the behavioral gotchas (do NOT skip)

These do not translate by a plain rename. Read each before converting:

  1. Case sensitivity. Classic Should -Be is case-insensitive on strings; so is Should-Be. But classic Should -BeExactly (case-sensitive) has no plain equivalent — use Should-BeString -CaseSensitive. (Should-Be is never case-sensitive.) Same pattern for BeLikeExactly → Should-BeLikeString -CaseSensitive and MatchExactly → Should-MatchString -CaseSensitive.
  2. Truthy vs. true. Classic Should -BeTrue / -BeFalse accept any truthy / falsy value (1, 'x', 0, '', $null, @()). The new Should-BeTrue / Should-BeFalse are strict (exactly $true / $false). To preserve the old loose behavior use Should-BeTruthy / Should-BeFalsy. Only use the strict ones when the value really is a boolean.
  3. BeNullOrEmpty has no single equivalent. Pick by intent: $null → Should-BeNull; empty string → Should-BeEmptyString; empty collection → Should-BeCollection -Count 0; broad "falsy" → Should-BeFalsy. The negation Should -Not -BeNullOrEmpty similarly splits into Should-NotBeNull / Should-NotBeEmptyString / Should-NotBeWhiteSpaceString.

…

常见问题

我需要一次性更新所有测试吗?

不需要,你可以一次只更新一个测试或一个文件。新版本的 Pester 仍然能识别旧写法,所以不必着急。

如果我不更新会怎样?

不会立即出问题。旧测试在 Pester 6 中仍然可以继续运行。更新只是为了使用新功能,是可选的。

更新后有些测试会失效吗?

有可能。有些转换比较复杂,比如 `Should -BeNullOrEmpty` 或集合比较,可能需要手动调整,以确保测试行为符合预期。转换之后最好检查一遍测试。

详情

作者的原始描述

Experimental (preview) Pester skill for migrating classic Should -Be (v5) assertion syntax to the new Should-* (v6) assertions (note the hyphen, no space), e.g. `Should -Be` -> `Should-Be`, `Should -Not -Be` -> `Should-NotBe`. Tracks Pester 6, which is still a release candidate, so this guidance may change; verified against Pester 6.0.0-rc2. Use when converting Pester v5 assertions to Pester v6 Sh

repo: github/awesome-copilot

热度

GitHub 星标
40k
排名
#7319
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能