|
1 | | -# test |
2 | | -Action that is used to test PowerShell modules |
| 1 | +# Test-PSModule |
| 2 | + |
| 3 | +Test PowerShell modules with Pester and PSScriptAnalyzer. |
| 4 | + |
| 5 | +This GitHub Action is a part of the [PSModule framework](https://github.com/PSModule). It is recommended to use the [Process-PSModule workflow](https://github.com/PSModule/Process-PSModule) to automate the whole process of managing the PowerShell module. |
| 6 | + |
| 7 | +## Specifications and practices |
| 8 | + |
| 9 | +Test-PSModule follows: |
| 10 | + |
| 11 | +- [Test-Driven Development](https://testdriven.io/test-driven-development/) using [Pester](https://pester.dev) and [PSScriptAnalyzer](https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/overview?view=ps-modules) |
| 12 | + |
| 13 | +## How it works |
| 14 | + |
| 15 | +The action runs the following the Pester test framework: |
| 16 | +- [PSScriptAnalyzer tests](https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/rules/readme?view=ps-modules) |
| 17 | +- [PSModule framework tests](#psmodule-tests) |
| 18 | +- If `RunModuleTests` is set to `true`: |
| 19 | + - Custom module tests from the `tests` directory in the module repository. |
| 20 | + - Module manifest tests using [Test-ModuleManifest](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/test-modulemanifest) |
| 21 | + |
| 22 | +The action fails if any of the tests fail or it fails to run the tests. |
| 23 | + |
| 24 | +## How to use it |
| 25 | + |
| 26 | +To use the action, create a new file in the `.github/workflows` directory of the module repository and add the following content. |
| 27 | +<details> |
| 28 | +<summary>Workflow suggestion - before module is built</summary> |
| 29 | + |
| 30 | +```yaml |
| 31 | +name: Test-PSModule |
| 32 | + |
| 33 | +on: [push] |
| 34 | + |
| 35 | +jobs: |
| 36 | + Test-PSModule: |
| 37 | + name: Test-PSModule |
| 38 | + runs-on: ubuntu-latest |
| 39 | + steps: |
| 40 | + - name: Checkout repo |
| 41 | + uses: actions/checkout@v4 |
| 42 | + |
| 43 | + - name: Initialize environment |
| 44 | + uses: PSModule/Initialize-PSModule@main |
| 45 | + |
| 46 | + - name: Test-PSModule |
| 47 | + uses: PSModule/Test-PSModule@main |
| 48 | + with: |
| 49 | + Name: PSModule # Needed if the repo is not named the same as the module |
| 50 | + Path: src |
| 51 | + RunModuleTests: false |
| 52 | + |
| 53 | +``` |
| 54 | +</details> |
| 55 | + |
| 56 | +<details> |
| 57 | +<summary>Workflow suggestion - after module is built</summary> |
| 58 | + |
| 59 | +```yaml |
| 60 | +name: Test-PSModule |
| 61 | + |
| 62 | +on: [push] |
| 63 | + |
| 64 | +jobs: |
| 65 | + Test-PSModule: |
| 66 | + name: Test-PSModule |
| 67 | + runs-on: ubuntu-latest |
| 68 | + steps: |
| 69 | + - name: Checkout repo |
| 70 | + uses: actions/checkout@v4 |
| 71 | + |
| 72 | + - name: Initialize environment |
| 73 | + uses: PSModule/Initialize-PSModule@main |
| 74 | + |
| 75 | + - name: Test-PSModule |
| 76 | + uses: PSModule/Test-PSModule@main |
| 77 | + with: |
| 78 | + Name: PSModule |
| 79 | + Path: outputs/modules |
| 80 | + |
| 81 | +``` |
| 82 | +</details> |
| 83 | + |
| 84 | +## Usage |
| 85 | + |
| 86 | +### Inputs |
| 87 | + |
| 88 | +| Name | Description | Required | Default | |
| 89 | +| ---- | ----------- | -------- | ------- | |
| 90 | +| `Name` | The name of the module to test. The name of the repository is used if not specified. | `false` | | |
| 91 | +| `Path` | The path to the module to test. | `true` | | |
| 92 | +| `RunModuleTests` | Run the module tests. | `false` | `true` | |
| 93 | + |
| 94 | +## PSModule tests |
| 95 | + |
| 96 | +The [PSModule framework tests](https://github.com/PSModule/Test-PSModule/blob/main/scripts/tests/PSModule/PSModule.Tests.ps1) verifies the following coding practices that the framework enforces: |
| 97 | + |
| 98 | +- Script filename and function/filter name should match. |
| 99 | + |
| 100 | +## Tools |
| 101 | + |
| 102 | +- Pester | [Docs](https://www.pester.dev) | [GitHub](https://github.com/Pester/Pester) | [PS Gallery](https://www.powershellgallery.com/packages/Pester/) |
| 103 | +- PSScriptAnalyzer [Docs](https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/overview?view=ps-modules) | [GitHub](https://github.com/PowerShell/PSScriptAnalyzer) | [PS Gallery](https://www.powershellgallery.com/packages/PSScriptAnalyzer/) |
| 104 | +- PSResourceGet | [Docs](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.psresourceget/?view=powershellget-3.x) | [GitHub](https://github.com/PowerShell/PSResourceGet) | [PS Gallery](https://www.powershellgallery.com/packages/Microsoft.PowerShell.PSResourceGet/) |
| 105 | +- [Test-ModuleManifest | Microsoft Learn](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/test-modulemanifest) |
| 106 | +- [PowerShellGet | Microsoft Learn](https://learn.microsoft.com/en-us/powershell/module/PowerShellGet/test-scriptfileinfo) |
0 commit comments