# Pester Testing for Module

**URL:** <https://forums.powershell.org/t/pester-testing-for-module/22211>\
**Category:** Pester\
**Created:** [April 26, 2023, 1:16pm UTC](https://forums.powershell.org/t/pester-testing-for-module/22211 "2023-04-26T13:16:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [April 26, 2023, 1:16pm UTC](https://forums.powershell.org/t/pester-testing-for-module/22211/1 "2023-04-26T13:16:07Z")

</div>

Testing a large Powershell module with Pester 5.4.0, which is my first opportunity to leverage Pester. Many of the blogs were for older versions of Pester and version 5 is quite different in layout and scopes. This API has 300+ endpoints, so there are many tests. The tests are broken down by function (e.g. Tickets, Assets, Requesters, etc.), so there are Tickets.tests.ps1, Asset.tests.ps1 and Requesters.test.ps1 respectively.

Where do you Import the module? Where do you connect (sets global vars for the module)?

```auto
Import-Module Pester
Import-Module "$PSScriptRoot\..\PSFreshservice" -Force -ErrorAction Stop

Connect-Freshservice -Name ItsFine_Prod -NoBanner

Describe "SLA Policies" {

    InModuleScope PSFreshservice {
        BeforeDiscovery {
            $Script:guid = New-Guid
        }
        Context "View and List" {
            It "Get-FreshServiceSLAPolicy should return data" -Tag "SLA Policies" {
                $SLAs = Get-FreshServiceSLAPolicy
                $SLAs | Should -Not -BeNullOrEmpty
            }
        }
    }
}

```

I’ve had the first couple of lines in a BeforeAll{} block, but it doesn’t appear to execute or being scope for the Discovery as it tells me the module isn’t imported.

Also did it like this:

```auto
Describe "SLA Policies" {
    Import-Module Pester
    Import-Module "$PSScriptRoot/../PSFreshservice" -Force -ErrorAction Stop

    InModuleScope PSFreshservice {

        Connect-Freshservice -Name ItsFine_Prod
        BeforeDiscovery {
            $Script:guid = New-Guid
        }
        Context "View and List" {
            It "Get-FreshServiceSLAPolicy should return data" -Tag "SLA Policies" {
                $SLAs = Get-FreshServiceSLAPolicy
                $SLAs | Should -Not -BeNullOrEmpty
            }
        }
    }
}

```

Just getting some odd scoping issues where the Pester tests see a module or the variables aren’t populated. Just looking for guidance on how these should be setup. Logically, it would run those lines before Invoking Pester and then you aren’t doing it in every file. Thoughts? Guidance?

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 9:13pm UTC](https://forums.powershell.org/t/pester-testing-for-module/22211/2 "2024-05-16T21:13:24Z")

</div>


