Test Your Code Quality with Pester and PSScriptAnalyzer - Leo Visser - PSConfEU 2023

  Рет қаралды 681

PowerShell Conference EU

Жыл бұрын

In this episode, I, Leo Visser, a Cloud Consultant at OGD ICT Diensten in the Netherlands, dive into the topic of code quality. I explain that code quality focuses on how well the code is written, including factors such as code formatting, unused code, and adherence to coding standards. To test code quality, I highlight the use of two tools: PS Script Analyzer and Pester.
PS Script Analyzer is a linting tool that provides a checklist of code issues and warnings. I demonstrate its usage on a sample script, showcasing potential problems like the use of "write-host" and trailing white space. Although these issues may not affect code functionality significantly, it's good practice to avoid them.
Moving on to Pester, it is a testing framework that allows us to execute tests on our code. Whether it's unit testing or integration testing, Pester provides a way to trigger commands or scripts, compare outputs, and generate test reports.
Incorporating PS Script Analyzer into our testing workflow ensures that any errors or warnings are identified before code is pushed to production. This can be done manually using the "Invoke-ScriptAnalyzer" command or through automated processes like VS Code extensions.
During the conversation, we discuss using Pester to create tests for script analyzer. We cover the naming convention for Pester tests and how to handle them in VS Code. We explain the structure of a Pester test using "describe" and "it" statements.
To make it easier to identify problematic files, I present an advanced version of the test. This version gathers all the rules and severities of script analyzer and creates separate tests for each rule. The "invoke script analyzer" command is used for each rule, checking for outcomes. If there are outcomes, they are appended to a string. Running the advanced Pester test provides detailed information on which rules failed and in which files, saving time in issue identification.
We also discuss filtering test results by severity using Pester tags. By using tags, we can choose to display only warnings or errors and exclude information outcomes.
Another approach we explore is working with exemptions when using Script Analyzer to test scripts. I demonstrate how specific errors can be suppressed using the "diagnostic code analyze suppressed message attribute" line of code. This allows us to accept certain errors for specific files, generating self-documenting code.
Integrating this testing process into CICD pipelines using Azure DevOps or GitHub Actions is the next step. I explain how to set up a pipeline that triggers the test file, showcasing the YAML file used for the pipeline. I mention the option to publish a test report in Azure DevOps, providing visual graphs of successful and failed tests.
Additionally, we discuss how the script analyzer tool integrates with Git systems like GitHub and Azure DevOps, enabling pull request validation. Setting up branch policies and build validation ensures that the pipeline runs on the code before proceeding, automatically checking for common issues. I touch on the possibility of sending pipeline output back to the test plan.
To enforce running tests before pushing to a remote repository, I suggest using Git hooks or setting up pipelines to trigger automatically and notify the developer if the pipeline fails.
In conclusion, I express gratitude to the audience for listening and encourage them to reach out with any further questions or concerns.
Chapters:
00:00:00 Test Your Code Quality with Pester and PSScriptAnalyzer - Leo Visser - PSConfEU 2023
00:00:07 Introduction and Acknowledgment of Sponsors
00:01:51 Definition of Code Quality and Introduction to PS Script Analyzer
00:03:19 PS Script Analyzer as a Linting Tool for PowerShell
00:04:29 Split in PowerShell Community on Using Right Host
00:06:02 Importance of Removing Trailing White Space in Code
00:06:24 Introduction to Pester Testing Framework
00:08:03 Incorporating PSScriptAnalyzer into …

Пікірлер
pumpkins #shorts
00:39
Mr DegrEE
Рет қаралды 111 МЛН
Don't look down on anyone#devil  #lilith  #funny  #shorts
00:12
Devil Lilith
Рет қаралды 46 МЛН
Я сделала самое маленькое в мире мороженое!
00:43
Кушать Хочу
Рет қаралды 4,4 МЛН
Wireless switch part 177
0:58
DailyTech
Рет қаралды 12 МЛН
Nokia 33-10 ☠️ #shorts
0:33
Apollon 🗿
Рет қаралды 462 М.
Пустая коробка вместо iPhone 16 pro
1:00
Кик Обзор
Рет қаралды 2,5 МЛН
Китайцы сделали самый ДЛИННЫЙ ноутбук: Это шутка
0:15
Собиратель новостей
Рет қаралды 1,7 МЛН