How to do TDD and unit testing in powershell?

后端 未结 6 528
遥遥无期
遥遥无期 2020-12-02 08:45

With MS ramming powershell into all new server products, I\'m starting to (reluctantly) think I need to take it seriously. Part of \"taking it seriously\" is TDD. Have you f

6条回答
  •  伪装坚强ぢ
    2020-12-02 09:03

    I am writing some basic PowerShell scripts with TDD like the following model:

    First, the spec in SUT_spec.ps1:

    Import-Module -Name .\my_SUT.ps1
    
    $case1_input=@{}
    $case1_output=@{}
    f1 $case1_input $case1_output
    
    $case1_result = $case1_output["Output"] -eq "expected"
    "f1 case1: $case1_result"
    
    # repeat for all test cases
    
    Remove-Module -Name my_SUT
    

    Second, my unit (SUT) as a function in my_SUT.ps1 file:

    function f1($the_input, $the_output)
    {
        #take input from $the_input hashtable, process it and put output into $the_output hashtable.
    
        $the_output["Output"]="results"
    }
    

    Third, the releasable main entry point as a separated file like SUT_spec.ps1 but with inputs from actual external sources.

提交回复
热议问题