Pester Check help

ZM 206 Reputation points
2022-07-04T14:38:20.487+00:00

Hi, i need help on a pester check for my script. I seem to be doing it wrong

So I want to check a specified template file exists in my directory and do a test-path

Deploy\Az\Deployment\nework

If I could get some suggestion or tips on how the pester check should be

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 32,911 Reputation points
    2022-07-04T15:19:35.767+00:00

    From a pure Powershell perspective, you would test based on relative path or full path to the file.

    PS C:\temp> get-location  
      
    Path  
    ----  
    C:\temp  
      
    PS C:\temp> Get-ChildItem .\test\Bkup\Ver3\  
      
        Directory: C:\temp\test\Bkup\Ver3  
      
    Mode                 LastWriteTime         Length Name  
    ----                 -------------         ------ ----  
    -a----          6/3/2022   1:35 PM              8 xxx.txt  
      
      
    PS C:\temp> Test-Path .\test\Bkup\Ver3\xxx.txt  
    True  
    PS C:\temp> Test-Path c:\temp\test\Bkup\Ver3\xxx.txt  
    True  
    PS C:\temp> if (Test-Path c:\temp\test\Bkup\Ver3\xxx.txt) { Write-Host "It's there"}  
    It's there  
    PS C:\temp>  
    

  2. Rich Matheisen 45,906 Reputation points
    2022-07-04T21:23:16.167+00:00

    I've recommended this book before: pesterbook

    It's not the most up-to-date, but it does bring all the essential elements together in one place. You can also find Pester posts/videos, but they tend to deal only with specific areas and leave you to look for the rest of the material. The book starts with the basics and builds from there.

    I'm not clear on what you want to do, though. Pester is geared toward unit testing. Your script (the one you want to test) should be doing the checking for the presence of the file, the integrity of the file, and the valid contents of the file. If there's a problem, your script should be the one to detect it and return some sort of recognizable message (e.g., throw an exception, return a string, return a numeric value, etc.) signifying the problem.

    Pester can build a directory/file to provide (i.e., call) your script with data for each test case (e.g., missing directory, missing file, file in the wrong format, file contains the wrong data, directory/file/data all there, etc.) and then wait for a return value and check it for the value you expect your script to return when presented with the erroneous data. Pester will then report either a failure (i.e., the script didn't return the expected value), or success (i.e., the script detected the error being tested for and returned the expected value) . . . or, when presented with a correct set of data, the script produced the expected output.

    0 comments No comments