Import-csv and Pester

Welcome Forums Pester Import-csv and Pester

Viewing 1 reply thread
  • Author
    Posts
    • #111983
      Participant
      Topics: 1
      Replies: 2
      Points: 1
      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
      Senior Moderator
      Topics: 9
      Replies: 1265
      Points: 4,582
      Helping Hand
      Rank: Community Hero

      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
        Topics: 1
        Replies: 2
        Points: 1
        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 🙂

Viewing 1 reply thread
  • The topic ‘Import-csv and Pester’ is closed to new replies.