К основному содержанию
правило CX Codex

pester — правило для Claude Code: тесты Pester

Правило задаёт соглашения для тестов PowerShell на Pester. Оно помогает Claude Code оформлять тестовые файлы и изолировать проверки по структуре репозитория.

MIT ★ 1 627 обновлён 2026-10-25

Установить в GitHub Copilot · Вручную: положить файлы Все способы и платформы ↓
.github/instructions/pester.instructions.md
Открыть на GitHub

Что делает pester

Правило применяется к файлам **/*.Tests.ps1 и описывает соглашения для Pester 5.x: имена и расположение тестов, подготовку BeforeAll, блоки Describe, Context и It, а также проверки с моками. Для скриптов предписан dot-source, для модулей — Import-Module, для классов — using module в начале файла.

Тесты должны соответствовать структуре каталогов репозитория; для самостоятельных пакетов правило допускает папку tests/ внутри пакета. Оно также описывает очистку модулей и временных файлов. Запускайте тесты через настроенный скрипт в package.json или напрямую командой Invoke-Pester; для проверки переменных окружения используется шаблон сохранения и восстановления из GitMocks.psm1.

Какие задачи решает

  • оформить имена тестов по файлам PowerShell
  • разложить тесты по структуре каталогов проекта
  • подготовить импорт проверяемого скрипта или модуля
  • изолировать тесты с временными файлами и моками

Примеры запросов агенту

  • Создай тесты для этого PowerShell-скрипта по правилам Pester в проекте.
  • Проверь файл SecurityHelpers.Tests.ps1 и приведи его к соглашениям Pester.
  • Добавь тесты с моками для этой функции и проверь вызовы через Should -Invoke.

Как установить pester

Вручную: положить файлы

.github/instructions/pester.instructions.md

Поле applyTo задаёт, к каким файлам применять

По шагам, с командами для каждой платформы: AGENTS.md и CLAUDE.md: что это, кто читает и какой выбрать

Вопросы о pester

Как установить pester в Claude Code?

Сохраните правило в файл .claude/rules/pester.md в проекте.

Что делает правило pester?

Оно задаёт соглашения для тестов PowerShell с Pester 5.x: структуру файлов, импорт проверяемого кода, моки, изоляцию и очистку.

Как запускать тесты по этому правилу?

Используйте тестовый скрипт из package.json, если он настроен, или запустите Invoke-Pester напрямую.

Исходный текст

pester.instructions.md · MIT
---
description: "Instructions for Pester testing conventions"
applyTo: '**/*.Tests.ps1'
---

# Pester Testing Instructions

Pester 5.x is the testing framework for all PowerShell code. Run tests through the repository's test runner (check `package.json` for a test script, or invoke `Invoke-Pester` directly if no runner is configured). Follow the repository's conventions for test execution.

## Test File Naming

Test files use a `.Tests.ps1` suffix matching the production file name:

| Production file              | Test file                          |
|------------------------------|------------------------------------|
| `Test-DependencyPinning.ps1` | `Test-DependencyPinning.Tests.ps1` |
| `SecurityHelpers.psm1`       | `SecurityHelpers.Tests.ps1`        |
| `SecurityClasses.psm1`       | `SecurityClasses.Tests.ps1`        |

## Test File Location

**Mirror directory pattern**: Place test files in a `tests/` directory that mirrors the production directory layout. Each production subdirectory has a corresponding test subdirectory. Discover the repository's test directory structure by examining existing test files.

**Co-located tests**: Self-contained packages (such as skills or plugins) place tests inside the package directory rather than the mirror tree:

```text
package-root/
├── scripts/
│   ├── action.ps1
│   └── helpers.psm1
└── tests/
    ├── action.Tests.ps1
    └── helpers.Tests.ps1
```

## Test File Header

Test files place `#Requires -Modules Pester` before the copyright header. This ordering differs from production scripts:

```powershell
#Requires -Modules Pester
# Copyright (c) 2026 Microsoft Corporation. All rights reserved.
# SPDX-License-Identifier: MIT
```

## SUT Import Patterns

Import the system under test in a file-level `BeforeAll` block. Use the pattern matching the production file type:

**Dot-source for scripts** (`.ps1`):

```powershell
BeforeAll {
    . (Join-Path $PSScriptRoot '../../security/Test-DependencyPinning.ps1')
}
```

**Import-Module for modules** (`.psm1`):

```powershell
BeforeAll {
    Import-Module (Join-Path $PSScriptRoot '../../security/Modules/SecurityHelpers.psm1') -Force
}
```

**using module for class modules** (parse-time type resolution):

```powershell
using module ..\..\security\Modules\SecurityClasses.psm1
```

The `using module` statement appears at the top of the file outside any block because PowerShell processes it at parse time.

## BeforeAll Setup

File-level `BeforeAll` initializes the test environment. Common activities include SUT import, mock module import, fixture path resolution, and output suppression:

```powershell
BeforeAll {
    . (Join-Path $PSScriptRoot '../../security/Test-DependencyPinning.ps1')
    Import-Module (Join-Path $PSScriptRoot '../../security/Modules/SecurityHelpers.psm1') -Force
    Import-Module (Join-Path $PSScriptRoot '../Mocks/GitMocks.psm1') -Force
    $script:FixtureRoot = Join-Path $PSScriptRoot '../fixtures/Security'
    Mock Write-Host {}
    Mock Write-CIAnnotation {} -ModuleName SecurityHelpers
}
```

## Describe, Context, and It Blocks

All `Describe` blocks require `-Tag 'Unit'`. The pester configuration excludes `Integration` and `Slow` tags by default:

```powershell
Describe 'FunctionName' -Tag 'Unit' {
    Context 'when input is valid' {
        It 'Returns expected output' {
            Get-Something -Path 'test' | Should -Be 'result'
        }
    }
}
```

`Context` groups related scenarios. Each `It` tests a single behavior with a descriptive sentence name.

## Data-Driven Tests

Use `-ForEach` on `It` blocks for parameterized testing:

```powershell
It 'Accepts valid type <Value>' -ForEach @(
    @{ Value = 'Unpinned' }
    @{ Value = 'Stale' }
    @{ Value = 'VersionMismatch' }
) {
    $v = [DependencyViolation]::new()
    $v.ViolationType = $Value
    $v.ViolationType | Should -Be $Value
}
```

## Mock Patterns

**Output suppression**: Empty scriptblock mocks prevent console noise:

```powershell
Mock Write-Host {}
```

**Module-scoped mocks**: `-ModuleName` injects mocks into modules under test:

```powershell
Mock Write-CIAnnotation {} -ModuleName SecurityHelpers
```

**Parameter-filtered mocks**: `-ParameterFilter` targets specific invocations:

```powershell
Mock git {
    $global:LASTEXITCODE = 0
    return 'abc123'
} -ModuleName LintingHelpers -ParameterFilter {
    $args[0] -eq 'merge-base'
}
```

## Mock Verification

Use `Should -Invoke` to verify mock calls:

```powershell
Should -Invoke Write-CIAnnotation -ModuleName SecurityHelpers -Times 1 -Exactly
Should -Invoke Write-CIAnnotation -ModuleName SecurityHelpers -ParameterFilter {
    $Level -eq 'Warning'
}
```

## Test Isolation

**`$TestDrive`**: Pester-managed temp directory, automatically cleaned per `Describe`:

```powershell
$testDir = Join-Path $TestDrive 'test-collection'
New-Item -ItemType Directory -Path $testDir -Force
```

**`New-TemporaryFile` with try/finally**: Manual temp file management when `$TestDrive` is insufficient:

```powershell
$tempFile = New-TemporaryFile
try {
    # test using $tempFile
}
finally {
    Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
}
```

**`$script:` scope**: Shares state across `It` blocks within a `Describe` or `Context`:

```powershell
BeforeAll {
    $script:result = Get-Something -Path 'test'
}
It 'Returns correct name' {
    $script:result.Name | Should -Be 'expected'
}
```

## Environment Save and Restore

Tests modifying environment variables use the `GitMocks.psm1` save/restore pattern:

```powershell
BeforeAll {
    Import-Module "$PSScriptRoot/../Mocks/GitMocks.psm1" -Force
}
BeforeEach {
    Save-CIEnvironment
    $script:MockFiles = Initialize-MockCIEnvironment
}
AfterEach {
    Remove-MockCIFiles -MockFiles $script:MockFiles
    Restore-CIEnvironment
}
```

## Cleanup

Remove imported modules in `AfterAll` to prevent state leakage between test files:

```powershell
AfterAll {
    Remove-Module SecurityHelpers -Force -ErrorAction SilentlyContinue
    Remove-Module GitMocks -Force -ErrorAction SilentlyContinue
}
```

## Assertion Reference

| Assertion                     | Usage                   |
|-------------------------------|-------------------------|
| `Should -Be`                  | Exact value equality    |
| `Should -BeExactly`           | Case-sensitive equality |
| `Should -BeTrue` / `-BeFalse` | Boolean checks          |
| `Should -BeNullOrEmpty`       | Null or empty string    |
| `Should -Not -BeNullOrEmpty`  | Non-null and non-empty  |
| `Should -Match`               | Regex matching          |
| `Should -BeLike`              | Wildcard matching       |
| `Should -Contain`             | Collection membership   |
| `Should -BeOfType`            | Type assertion          |
| `Should -HaveCount`           | Collection length       |
| `Should -Throw`               | Exception expected      |
| `Should -Not -Throw`          | No exception expected   |
| `Should -BeGreaterThan`       | Numeric comparison      |
| `Should -BeLessThan`          | Numeric comparison      |
| `Should -Invoke`              | Mock call verification  |

## Running Tests

Check `package.json` for a test runner script (common name: `test:ps`). If a runner is configured, use it to execute tests:

```bash
# Example: run all tests via the repository's test runner
npm run test:ps

# Example: run tests for a specific directory or file
npm run test:ps -- -TestPath "path/to/tests/"
```

If no runner is configured, invoke Pester directly:

```powershell
Invoke-Pester -Path './tests/' -Output Detailed
```

After execution, check the repository's log or output directory for structured test results (such as summary and failure JSON files) when available.

## Complete Test Example

<!-- <template-complete-test> -->

```powershell
#Requires -Modules Pester
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: MIT

BeforeAll {
    . (Join-Path $PSScriptRoot '../../linting/Invoke-Linter.ps1')
    Import-Module (Join-Path $PSScriptRoot '../Mocks/GitMocks.psm1') -Force
    $script:FixtureRoot = Join-Path $PSScriptRoot '../fixtures/Linting'
    Mock Write-Host {}
}

Describe 'Invoke-Linter' -Tag 'Unit' {
    Context 'when input file is valid' {
        BeforeAll {
            $script:result = Invoke-Linter -Path (Join-Path $script:FixtureRoot 'valid.md')
        }

        It 'Returns zero violations' {
            $script:result.Violations | Should -HaveCount 0
        }

        It 'Sets status to pass' {
            $script:result.Status | Should -Be 'Pass'
        }
    }

    Context 'when input file has errors' {
        BeforeAll {
            $script:result = Invoke-Linter -Path (Join-Path $script:FixtureRoot 'invalid.md')
        }

        It 'Returns violations' {
            $script:result.Violations | Should -Not -BeNullOrEmpty
        }

        It 'Includes file path in each violation' {
            $script:result.Violations | ForEach-Object {
                $_.File | Should -Not -BeNullOrEmpty
            }
        }
    }
}

AfterAll {
    Remove-Module GitMocks -Force -ErrorAction SilentlyContinue
}
```

<!-- </template-complete-test> -->

© microsoft · MIT · microsoft/hve-core

Похожие правила IDE

Все правила IDE по теме «Тесты и QA» →
powershell-pester-6 официальный github Правило powershell-pester-6 задаёт стандарты написания тестов на Pester v6 для PowerShell. Помогает генерировать надёжные спецификации в файлах *.Tests.ps1 без устаревших конструкций v5. CX Codex +1 ★ 41 430звёзд репо pester-migration официальный github Скилл pester-migration для GitHub Copilot переводит тесты Pester на следующую мажорную версию и возвращает зелёный прогон. После обновления модуля падают *.Tests.ps1 — скилл ведёт миграцию пошагово. CX Codex +1 25установок om-prepare-test-env open-mercato Скилл om-prepare-test-env разворачивает локальное окружение для интеграционных тестов и браузерного QA. Определяет стек проекта, создаёт скрипты запуска и сохраняет дескриптор среды. CX Codex +1 3 900установок game-automation rehan-remade Скилл game-automation запускает игры, снимает экран через GPU-захват и управляет окном через виртуальные клавиши и мышь. Нужен ИИ-агенту для автоматической проверки модов в реальной игре. CX Codex +1 2 000установок test-changes PinguApps Скилл test-changes для Claude Code готовит план ручной проверки изменений из текущего разговора. Он связывает каждое изменение с проверяемым результатом и даёт команды PowerShell, если они нужны. CX Codex +1 335установок erf-validate Nikolay Shirokov Скилл erf-validate проверяет структуру внешнего отчёта 1С в XML после создания или изменения. Помогает найти ошибки до сборки. CX Codex +1 50установок

Из того же набора148

Весь набор →
backlog-plan Скилл backlog-plan планирует задачи в Azure DevOps, GitHub и Jira без изменения данных в трекере. Разбирает очереди, готовит спринты и формирует локальные файлы планов. CX Codex +1 ★ 1 627звёзд репо review-artifacts Правило review-artifacts для Claude Code сохраняет результаты код-ревью в согласованной структуре. Оно применяется к файлам в .copilot-tracking/reviews/code-reviews/. CX Codex +1 ★ 1 627звёзд репо customer-card-render Скилл customer-card-render превращает канонические markdown-артефакты Design Thinking в YAML-определения слайдов PowerPoint. Дальше их собирает в готовую презентацию общий пайплайн скилла powerpoint. CX Codex +1 ★ 1 627звёзд репо evaluation-design Скилл evaluation-design помогает составить тестовые датасеты и сопроводительные руководства для диалоговых агентов и RAG-систем. Задаёт вопросы по одному и фиксирует требования. CX Codex +1 ★ 1 627звёзд репо typescript-doc-comments Правило typescript-doc-comments для написания документирующих комментариев в TypeScript по стандарту TSDoc. Помогает описать поведение и ограничения функций, типов и компонентов так, чтобы вызывающий код не ошибся. CX Codex +1 ★ 1 627звёзд репо engagement-reporting Скилл engagement-reporting формирует еженедельные отчёты о ходе проекта с опорой на первоисточники и создаёт черновики писем в Outlook. CX Codex +1 ★ 1 627звёзд репо hve-builder Скилл hve-builder помогает создавать и дорабатывать промпты, инструкции, агентов и другие скиллы. Он также проводит проверку качества или механическую валидацию без изменения файлов. CX Codex +1 ★ 1 627звёзд репо bash Правило задаёт единые конвенции для Bash-скриптов: strict mode, структуру с main(), форматирование, обработку ошибок и copyright-заголовки Microsoft. CX Codex +1 ★ 1 627звёзд репо

Спонсоры проекта

Компании, которые помогают держать аналитику открытой