Running Pester tests in Visual Studio 2019/2022 is really slow compared to VSCode

Tool: Visual Studio 2019/2022 - Free and Pro Trial
Version: 2019 and 2022

Using PowerShell Tools For Visual Studio, I have a solution with a project with psm1 files, and another project with ps1 files that I use to run my Pester unit tests.

Tests are discovered by the Test Explorer, and I can run them, but they are really slow to execute when I compare to the time they take to execute in a command line, or in VSCode.

I tried different things:

  • Updating Pester to the latest version in the default PowerShell
  • Installing the latest PowerShell Core, and using the pro version to use that version in the tools.

But it still takes the same amount of time to execute.

Is there a different way to speed up the execution of pester unit tests in the Visual Studio?

At the moment my tests are declared in multiple ps1 files with 1 Describe by files, and multiple Contexts in those Describe, 1 Context per Methods tested, the multiple It for each methods:

Describe "Assembly" {
    Context "GetAssemblyName" {
        It "Should find the assembly name in every project file." {
            CopyMasterFiles
            $sol = "$(MasterfileTemp)\TestSolution"
            foreach($projectFile in (MasterProjectFiles))
            {
                $projectFile = "$sol\$projectFile"
                $projectName = Split-Path $projectFile -Leaf
                $projectName = $projectName.Substring(0, $projectName.LastIndexOf('.'))
                "$projectFile $(GetAssemblyName $projectFile)" | Should -Be "$projectFile $projectName"
            }

            WereMasterfilesTempered | Should -Be $false
        }

        It "Should work when provided a System.IO.FileInfo." {
            CopyMasterFiles
            $sol = "$(MasterfileTemp)\TestSolution"
            foreach($projectFile in (MasterProjectFiles))
            {
                $projectFile = Get-Item "$sol\$projectFile"
                $projectName = Split-Path $projectFile -Leaf
                $projectName = $projectName.Substring(0, $projectName.LastIndexOf('.'))
                "$projectFile $(GetAssemblyName $projectFile)" | Should -Be "$projectFile $projectName"
            }

            WereMasterfilesTempered | Should -Be $false
        }
    }

Can you give me an idea of how large your test suite is? I can take a look.

I have 72 unique tests splitted in different files this way:
image
But even running 1 file only (for example Assembly.tests) takes 1.8min:

[+] Assembly.tests.ps1 26.71s (12.05s|2.57s)
Tests completed in 101.61s
Tests Passed: 11, Failed: 0, Skipped: 0 NotRun: 63

========== Test run finished: 11 Tests (11 Passed, 0 Failed, 0 Skipped) run in 1.8 min ==========

I suspect that some module loading takes place before the actual execution of the tests, but I cannot find any log to prove my point so far, and I wouldn’t have a solution either if I find such proof.

Interesting. Thanks for the info. Let me play around with this and see if I can reproduce the behavior.

1 Like