Author Posts

August 19, 2016 at 5:59 am

I am having my .ps1 saved in directory1 and mt .tests.ps1 in directory2 with in the same folder.
Folder view
In general we use to write the script as follows when the test file and script file are in the same folder

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "$here\$sut"

But I am having them in different folder, so how can I load the path so that the script file should get loaded

August 19, 2016 at 7:47 am

Example of a solution for your question:

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace('.Tests.ps1','.ps1')
. "$(Join-Path -Path $here -ChildPath "..\Pester\$sut" -Resolve)"
  • This reply was modified 2 years, 1 month ago by  Daniel Krebs.

August 19, 2016 at 8:14 am

Getting the following error

Join-Path : Cannot find path 'D:\PowerShell\Pester\TestPester\Pester\PowerShellTest1.tests.ps1' because it does not exist.
At D:\PowerShell\Pester\TestPester\MyProject.Tests\PowerShellTest1.tests.ps1:3 char:6

August 19, 2016 at 8:30 am

I'm sure you can find out yourself. The code is not really complex. Most likely the folder layout is different from what you've posted earlier or you've removed the .. in my example.

Good luck.

August 19, 2016 at 8:34 am

I have written as per you posted, My .ps1 is in A folder and my test script in B folder, I believe the code you have given is searching in current directory B instead of A

August 19, 2016 at 12:54 pm

I have achieved as follows

$project = (Split-Path -Parent $MyInvocation.MyCommand.Path).Replace(".Tests", "")
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".tests.", ".")
. "$project\$sut"