Skip to content

Commit bfb1520

Browse files
committed
Refactor unit tests guidelines to streamline instructions and remove outdated practices
1 parent 0302174 commit bfb1520

File tree

1 file changed

+1
-83
lines changed

1 file changed

+1
-83
lines changed

.github/instructions/dsc-community-style-guidelines-unit-tests.instructions.md

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3,87 +3,7 @@ description: Guidelines for writing and maintaining tests using Pester.
33
applyTo: "tests/**/*.[Tt]ests.ps1"
44
---
55

6-
# Tests Guidelines
7-
8-
All tests should use the Pester framework and use Pester v5.0 syntax.
9-
Parameter validation should never be tested.
10-
11-
Test code should never be added outside of the `Describe` block.
12-
13-
There should only be one Pester `Describe` block per test file, and the name of
14-
the `Describe` block should be the same as the name of the public command,
15-
private function, or class-based resource being tested. Each scenario or
16-
code path being tested should have its own Pester `Context` block that starts
17-
with the phrase 'When'. Use nested `Context` blocks to split up test cases
18-
and improve tests readability. Pester `It` block descriptions should start
19-
with the phrase 'Should'. `It` blocks must always call the command or function
20-
being tested and result and outcomes should be kept in the same `It` block.
21-
`BeforeAll` and `BeforeEach` blocks should never call the command or function
22-
being tested.
23-
24-
The `BeforeAll`, `BeforeEach`, `AfterAll` and `AfterEach` blocks should be
25-
used inside the `Context` block as near as possible to the `It` block that
26-
will use the test data, test setup and teardown. The `AfterAll` block can
27-
be used to clean up any test data. The `BeforeEach` and `AfterEach`
28-
blocks should be used sparingly. It is okay to duplicated code in `BeforeAll`
29-
and `BeforeEach` blocks that are used inside different `Context` blocks.
30-
The duplication helps with readability and understanding of the test cases,
31-
and to be able to keep the test setup and teardown as close to the test
32-
case (`It`-block) as possible.
33-
34-
To use `-ForEach` on `Context`- or `It`-blocks that use data driven tests the
35-
variables must be defined in a `BeforeDiscovery`-block for Pester to find in in the discovery phase.
36-
There can be several `BeforeDiscovery`-blocks in a test file, so we can keep the
37-
values for the particular test context separate.
38-
39-
- Always use the latest Pester v5 syntax and features in your tests.
40-
- Always prefer `-BeTrue` over `-Be $true`.
41-
- Always prefer `-BeFalse` over `-Not -Be $true` or `-Be $false`.
42-
- Do not use `-Not -Throw`, let `It`-block handle unexpected exceptions.
43-
44-
## Test Formatting Rules
45-
46-
- Use PascalCase for all Pester keywords: `Describe`, `Context`, `It`, `Should`
47-
- `It` block descriptions must start with "Should"
48-
- `Context` block descriptions must start with "When"
49-
- Use PascalCase for PowerShell commands in tests
50-
- Distinguish variable names used in test setup by using prefix 'mock'
51-
52-
Example:
53-
```powershell
54-
Describe 'Get-TargetResource' {
55-
Context 'When Get-TargetResource is called with default parameters' {
56-
It 'Should return something' {
57-
Get-TargetResource @testParameters | Should -Be 'something'
58-
}
59-
}
60-
}
61-
```
62-
63-
Never test, mock or use `Should -Invoke` for `Write-Verbose` and `Write-Debug`
64-
regardless of other instructions.
65-
66-
Never use `Should -Not -Throw` to prepare for Pester v6 where it has been
67-
removed. By default the `It` block will handle any unexpected exception.
68-
Instead of `{ Command } | Should -Not -Throw`, use `Command` directly.
69-
70-
Always make sure to pass mandatory parameters to the command being tested,
71-
to avoid tests making interactive prompts waiting for input.
72-
73-
Unit tests should be added for all public commands, private functions and
74-
class-based resources. The unit tests for class-based resources should be
75-
placed in the folder tests/Unit/Classes. The unit tests for public command
76-
should be placed in the folder tests/Unit/Public and the unit tests for
77-
private functions should be placed in the folder tests/Unit/Private. The
78-
unit tests should be named after the public command or private function
79-
they are testing, but should have the suffix .Tests.ps1. The unit tests
80-
should be written to cover all possible scenarios and code paths, ensuring
81-
that both edge cases and common use cases are tested.
82-
83-
Testing commands or functions, assign to $null when command return object that are not used in test.
84-
85-
Never use `InModuleScope` when testing public commands.
86-
Always use `InModuleScope` when testing private functions or class-based resources.
6+
# Unit Tests Guidelines
877

888
All public commands should always have a test to validate parameter sets
899
using this template. For commands with a single parameter set:
@@ -162,8 +82,6 @@ It 'Should accept ParameterName from pipeline' {
16282
}
16383
```
16484

165-
The `BeforeAll` block should be used to set up any necessary test data or mocking
166-
16785
Use localized strings in the tests only when necessary. You can assign the
16886
localized string to a mock variable by and get the localized string key
16987
from the $script:localizedData variable inside a `InModuleScope` block.

0 commit comments

Comments
 (0)