Skip to content

How to test a function within a script #1069

@DarkLite1

Description

@DarkLite1

This is more a question than it is an issue. Suppose I have a script file that contains some functions in the Begin clause. However, the script has also some actions defined in the Process clause and some mandatory parameters in the Parameters.

Under normal circumstances I would move the functions to a module and test the module for proper functionality of the containing functions. But in this case, that's not possible. Because we need these functions to be in the script file. The script file will later be called with Invoke-Command -FilePath Script.ps1 -ComputerName xxx to run on remote machines where there's no module folder available.

Consider the following example:

Param (
    [Parameter(Mandatory)]
    [String]$Name
)

Begin {
    Function Test-NumberIsFive {
        Param (
            [Parameter(Mandatory)]
            [Int]$Number
        )

        if ($Number -eq 5) {
            $true
        }
    }
}

Process {
    (0..10).ForEach({ Test-NumberIsFive -Number $_ })
}

What is the best way to test the function Test-NumberIsFive without invoking the script?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions