Test Generation Research【免费下载链接】skillsRepository for skills to assist AI coding agents with .NET and C#项目地址: https://gitcode.com/GitHub_Trending/skills17/skillsProject OverviewPath: C:\work\contoso-billingLanguage: PowerShell 7.4Module:src/Contoso.Billing.psd1importsContoso.Billing.psm1Test Framework: Pester v5Coverage BaselineInitial Line Coverage: unknownStrategy: broadExisting Test Count: 0 tests across 0 filesBuild Test CommandsModule load:Import-Module ./src/Contoso.Billing.psd1 -Force -ErrorAction StopDiscovery:Invoke-Pester -Configuration { Run { Path ./Tests; PassThru $true; SkipRun $true } }Test:Invoke-Pester -Path ./Tests -Output DetailedFiles to TestHigh PriorityFileFunctionsTestabilityNotessrc/Contoso.Billing.psm1Get-InvoiceTotal, Get-InvoiceById, Set-InvoicePaidHighDependencies are scriptblocks, easy to fake; clock is injectableTesting PatternsNo existing patterns; recommend Pester v5Describe/Context/It,BeforeAllmodule import,-TestCasesfor total calculations, and scriptblock fakes for repository operations.值得注意的几个细节 1. **测试命令与发现命令分离**文档明确区分模块加载命令Import-Module ... -Force -ErrorAction Stop、纯发现命令SkipRun $true 只收集测试元数据不执行与实际测试命令。这与 [powershell.md](https://link.gitcode.com/i/21b60da366248b5f6eaf53c3cacb0ece) 的 Harness Discovery Check 一脉相承——CI/msbench/覆盖率工具通常从仓库根目录以默认发现方式调用 Pester测试若不在默认发现范围内即使 Invoke-Pester -Path 能跑过也毫无价值。 2. **可测性标注**High 的判定依据是依赖是 scriptblock容易伪造时钟可注入——这正是上一节源码分析得到的结论。 3. **策略建议**-TestCases 用于总额计算表格驱动scriptblock fake 用于仓储操作。 ## 四、Plan 阶段产出TESTAGENT_DIR/plan.md Research 之后code-testing-planner 基于 research.md 生成分阶段实施计划。示例文档中的计划产物如下 markdown # Test Implementation Plan ## Overview Generate Pester v5 tests for total calculation, repository lookup, and the paid-state transition. Single phase since there is one module file. ## Commands - **Import**: Import-Module ./src/Contoso.Billing.psd1 -Force -ErrorAction Stop - **Test**: Invoke-Pester -Path ./Tests/Contoso.Billing.Tests.ps1 -Output Detailed ## Phase 1: Contoso.Billing ### Files to Test - **Source**: src/Contoso.Billing.psm1 - **Test File**: Tests/Contoso.Billing.Tests.ps1 **Functions to Test**: 1. Get-InvoiceTotal — table-driven happy paths and empty-line-items error 2. Get-InvoiceById — existing invoice and missing invoice 3. Set-InvoicePaid — status/date update and persistence; already-paid error; missing invoice error计划的关键信息是覆盖矩阵而非测试数量三个函数各对应一组明确的行为/分支happy path、空数据报错、缺失报错、状态转换、幂等保护。这与 code-testing-agent/SKILL.md 的完成契约一致——每个显式需求都要能映射到一条具体测试plan.md就是这条映射的载体。由于只有一个模块文件这里采用单阶段Single phase即可对应生成器策略表中的Direct / Single pass判定见 code-testing-generator.agent.md。五、Implement 阶段产出生成的 Pester v5 测试文件code-testing-implementer按计划写出Tests/Contoso.Billing.Tests.ps1这是整个示例的核心交付物# Tests/Contoso.Billing.Tests.ps1 BeforeAll { Import-Module (Join-Path $PSScriptRoot .. src Contoso.Billing.psd1) -Force -ErrorAction Stop function New-TestInvoice { param( [int]$Id 1, [InvoiceStatus]$Status [InvoiceStatus]::Pending, [double]$TaxRate 0.10, [object[]]$LineItems ({ Quantity 1; UnitPrice 100.00 }) ) [pscustomobject]{ Id $Id Status $Status TaxRate $TaxRate LineItems $LineItems PaidDate $null } } } Describe Contoso.Billing invoice functions { Context Get-InvoiceTotal { It returns Expected for Name -TestCases ( { Name single item with tax; LineItems ({ Quantity 1; UnitPrice 100.00 }); TaxRate 0.10; Expected 110.00 } { Name multi quantity zero tax; LineItems ({ Quantity 3; UnitPrice 25.00 }); TaxRate 0.00; Expected 75.00 } { Name rounds to two decimals; LineItems ({ Quantity 2; UnitPrice 9.99 }); TaxRate 0.07; Expected 21.38 } ) { param($LineItems, $TaxRate, $Expected) $invoice New-TestInvoice -LineItems $LineItems -TaxRate $TaxRate Get-InvoiceTotal -Invoice $invoice | Should -BeExactly $Expected } It throws when the invoice has no line items { $invoice New-TestInvoice -LineItems () { Get-InvoiceTotal -Invoice $invoice } | Should -Throw *no line items* } } Context Get-InvoiceById { It returns an existing invoice { $expected New-TestInvoice -Id 42 $findInvoice { param($Id) if ($Id -eq 42) { $expected } } $result Get-InvoiceById -Id 42 -FindInvoice $findInvoice $result | Should -BeSame $expected } It throws when the invoice is missing { $findInvoice { $null } { Get-InvoiceById -Id 999 -FindInvoice $findInvoice } | Should -Throw *999* } } Context Set-InvoicePaid { It marks a pending invoice as paid and persists it { $invoice New-TestInvoice -Id 1 $script:updatedInvoice $null $fixedNow [datetime]2025-01-01T12:00:00Z $findInvoice { param($Id) if ($Id -eq 1) { $invoice } } $updateInvoice { param($Invoice) $script:updatedInvoice $Invoice } Set-InvoicePaid -Id 1 -FindInvoice $findInvoice -UpdateInvoice $updateInvoice -GetNow { $fixedNow } $invoice.Status | Should -Be ([InvoiceStatus]::Paid) $invoice.PaidDate | Should -Be $fixedNow $script:updatedInvoice | Should -BeSame $invoice } It throws and does not update an already-paid invoice { $invoice New-TestInvoice -Status ([InvoiceStatus]::Paid) $script:updatedInvoice $null $findInvoice { $invoice } $updateInvoice { param($Invoice) $script:updatedInvoice $Invoice } { Set-InvoicePaid -Id 1 -FindInvoice $findInvoice -UpdateInvoice $updateInvoice } | Should -Throw *already paid* $script:updatedInvoice | Should -BeNullOrEmpty } } }5.1 每条规则在示例中的落点这份测试文件几乎逐条呼应了 powershell.md 的基础规则所有导入都在BeforeAllImport-Module (Join-Path $PSScriptRoot .. src Contoso.Billing.psd1) -Force -ErrorAction Stop放在BeforeAll顶部而不是脚本顶层。原因是 Pester v5 分为 Discovery收集测试元数据与 Run执行测试两个阶段脚本顶层代码在 Discovery 阶段就会执行若在此阶段导入模块后续 Run 阶段才能看到导出的函数详见下文第六节。-Force保证每次运行都重新加载避免旧版本模块缓存。使用$PSScriptRoot而非$MyInvocation.MyCommand.Path后者在BeforeAll中返回空值Join-Path而不是字符串拼接\保证跨平台Linux/macOS 文件系统大小写敏感。helper 工厂函数New-TestInvoice收敛了测试数据的构造默认TaxRate 0.10、单个Quantity 1, UnitPrice 100.00的行项目各测试可覆盖部分参数。表格驱动-TestCases三条数据覆盖单行含税多数量零税四舍五入到两位小数配合It returns Expected for Name的占位符命名失败时能直接定位到具体场景。注意Expected与-BeExactly的配合——Should -Be无连字符是 v3 语法v5 必须用Should -BeExactly。异常断言传 scriptblock{ Get-InvoiceTotal -Invoice $invoice } | Should -Throw *no line items*——Should -Throw要求 scriptblock 而非直接调用且支持*通配符匹配消息子串。scriptblock fake 注入依赖$findInvoice { param($Id) if ($Id -eq 42) { $expected } }模拟仓储查找$updateInvoice把更新结果捕获到$script:updatedInvoice从而验证持久化确实发生了Should -BeSame $invoice验证引用相等。时钟注入-GetNow { $fixedNow }把当前时间固定为[datetime]2025-01-01T12:00:00Z随后断言PaidDate精确等于该值——这正是 powershell.md 建议可注入时钟的实践形态。状态转换与幂等保护Set-InvoicePaid的 happy path 断言Status变为Paid、PaidDate被写入、更新回调被调用已支付发票用例断言抛*already paid*且$script:updatedInvoice仍为$null——验证抛错且不更新防止测试只测了异常而漏掉副作用。5.2 示例之外的关键规则速查powershell.md 专门强调 Discovery 与 Run 的两阶段陷阱这是 Agent 错误的第一大来源所有 setup 代码必须放在BeforeAll/BeforeEach绝不能放在脚本顶层或Describe/Context内的游离位置直接写在Describe/Context里但在It/Before*/After*之外的代码会在 Discovery 阶段执行-ForEach/-TestCases的数据必须在BeforeDiscovery中准备BeforeAll在发现之后才运行-Skip:$condition在 Discovery 阶段求值来自BeforeAll的条件会是$null文件类测试优先用TestDrive:Pester 会自动清理。Mock 方面powershell.mdmock 放在BeforeAll共享或BeforeEach每个测试重置用-ModuleName指定在哪个模块作用域内生效选择性 mock 用-ParameterFilter调用验证用Should -Invoke默认只在It内统计该测试的调用。此外模块中的函数通过Export-ModuleMember只导出三个函数测试应走公开 APIGet-InvoiceById内部调用已被Set-InvoicePaid复用避免过度使用InModuleScope。六、修复循环Fix Cycle生成测试后code-testing-implementer运行测试遇到 Pester 发现或执行问题时code-testing-fixer负责诊断与修复。示例展示了一次非常典型的失败测试输出CommandNotFoundException: The term Get-InvoiceTotal is not recognizedFixer 诊断模块导入被放在了脚本顶层。应把模块导入移到BeforeAll中这样 Pester 的 Run 阶段才能看到导出的函数。应用的修复将Import-Module ... -Force移入BeforeAll即上文测试文件所示形态。重跑Invoke-Pester -Path ./Tests/Contoso.Billing.Tests.ps1 -Output Detailed→ SUCCESS这个案例对应 powershell.md 常见错误表中的经典条目错误修复CommandNotFoundExceptionfor Mock target函数必须已存在才能 mock——先在BeforeAll中导入模块变量在It块中是$null把赋值移入BeforeAll子It块无需$script:即可见-ForEach数据为空把数据准备从BeforeAll移到BeforeDiscoveryShould -Throw捕获不到 cmdlet 错误多数 cmdlet 错误是非终止错误用{ cmd -ErrorAction Stop }包裹或在BeforeEach设置$ErrorActionPreference Stop修复循环的整体执行契约在 code-testing-fixer.agent.md 中定义一次只修一个错误、只做必要的最小改动、保持现有代码风格、修测试期望而非生产代码。对于新生成的测试失败修正测试里的期望值以匹配真实生产行为是默认策略绝不能为了通过而给测试打[Ignore]/[Skip]。七、最终报告Final Report管道收尾时生成器汇总指标、产出文件与验证结果形成最终报告。示例文档给出的报告模板如下## Test Generation Report **Project**: contoso-billing (PowerShell) **Strategy**: Direct (single module in scope) ### Results | Metric | Value | |----------------|-------| | Tests created | 8 | | Tests passing | 8 | | Tests failing | 0 | | Files created | 1 | ### Files Created - Tests/Contoso.Billing.Tests.ps1 (8 Pester examples, 3 contenteditable="false">【免费下载链接】skillsRepository for skills to assist AI coding agents with .NET and C#项目地址: https://gitcode.com/GitHub_Trending/skills17/skills创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考