Import-csv and Pester

Welcome Forums Pester Import-csv and Pester

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 month ago.

  • Author
    Posts
  • #111983

    Participant
    Points: 0
    Rank: Member

    Hello, can someone help me de-mystify the import-csv cmdlet used in Pester test scripts.

    I learned that standard built-in cmdlets like import-csv don't need to be tested by Pester, as they already are thoroughly tested. You just need to test your own functions.

    But don't you need to test if the csv file you provide has the correct information? Does it has the required number of columns, for example? Or is this something you'd rather test in your script itself, and not in the Pester script?

  • #112028

    Participant
    Points: 65
    Published
    Rank: Member

    Pester – is mainly used for unit testing scripts. When we say unit test, we should have developed the scripts in a way that it is testable by using functions. Here units are functions.

    Pester provides a framework for running Unit Tests to execute and validate PowerShell commands.

    What you need here is a kind of validation, and yes, you can leverage pester here as pester can always test against expected values. you should build the logic for that.

    You can achieve it via comparing as well,

    If your CSV is

    Column1 column2
    ——- ——-
    Raw11 Row12

    $CSV = Import-Csv c:\CsvToTest.csv
    $Refference = [pscustomobject]@{Column1='Raw11';column2='Row12'}
    
    Compare-Object -Referenceobject $Refference -DifferenceObject $CSV #output should be null if they are same.
    
    • #112121

      Participant
      Points: 0
      Rank: Member

      Hi, thanks a lot for the clarification and that example; I haven't really thought of using the compare-object cmdlet in this situation 🙂

You must be logged in to reply to this topic.