Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1017803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:45:35+00:00 2026-05-16T10:45:35+00:00

I am writing a simple unit test harness in powershell I designed the harness

  • 0

I am writing a simple unit test harness in powershell

I designed the harness such that its assert functions takes a script-block as a parameter to allow the harness to run the code from within the assert function and treat any exceptions are thrown as a test failure.

If the tests fail I want to return line in the unit test in which the test fails. My plan was to do this by taking a stacktrace (Get-PSCallStack) at the start of each assert method and use the information for the second stack-frame which I assume should correspond to the line where the assert function was called.

In practice, I found that the information that powershell gave back seemed wrong. The second stack-frame refers to the the correct file as expected but always gives the line number at which I called Get-PSCallStack in the assert method. Sometimes this number might be even higher than the number of lines in the file given (i.e. Location is given as “ScriptFile.ps1 line 88” but the file only has 20 lines).

Is there a problem with the stack trace in powershell or is there something I am not understanding here?

Edit

As requested I am posting an example which should produce the same results

Tester.ps1

#File 1 (Tester.ps1)
#Creates the tester object

$tester = (New-Object PSObject);

$tester | Add-Member -MemberType ScriptMethod -Name AssertTrue -Value {
    param($expression);

    $stackFrame = (GEt-PSCallStack)[1];

    try{
        $result = &$expression;
        if($result -eq $true){
            $this.LogPass();
        }else{
            $this.LogFailure("Evaluation Failed expected ""$true"" got ""$false""", $stackFrame);
        }
    }catch [Exception]{
        $this.LogFailure("Unexpected exception encountered", $stackFrame);
    }
}

$tester | Add-Member -MemberType ScriptMethod -Name LogPass -Value {
    #Do nothing
};

$tester | Add-Member -MemberType ScriptMethod -Name LogFailure -Value {
    param($message, $stackFrame);
    "Failure Encounterd";
    "Command: $($stackFrame.Command)"
    "Location: $($stackFrame.Location)";
    "Message: $message";
}

return $tester;

TestCase.ps1

#File 2 (TestCase.ps1)
#Runs the tests using the tester object

$tester = &(Resolve-Path "Tester.ps1");

function TestFailure($tester){
    $expression = {$false};
    $tester.AssertTrue($expression);
}

TestFailure($tester);

The assert is called on Line 7 of TestCase.ps1 and the call stack is captured on line 9 of Tester.ps1

This prints

Failure Encounterd
Command: TestFailure
Location: Tester.ps1: Line 9
Message: Evaluation Failed expected "True" got "False"

The command is correct but both the file and the line are wrong

The next frame of the stack trace correctly describes where TestFailure() is called with its location as “TestCase.ps1: Line 11”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:45:36+00:00Added an answer on May 16, 2026 at 10:45 am

    It is not an assert function that you use, it is an assert script block used as a “member function”. But it is still a script block.

    According to this reported issue:
    https://connect.microsoft.com/PowerShell/feedback/details/531086/depending-on-how-you-invoke-a-script-block-the-invocation-details-may-not-be-available-from-inside-the-script-block#

    there is something wrong with calling Get-PSCallStack from script blocks. So, the answer to your question is probably: yes, it is a PowerShell issue.

    Well, I would recommend you to use functions. I re-factored your scripts to use functions (dirty version, bad names, etc.) and they work as expected:

    #File 1 (Tester.ps1)
    #Creates the tester object
    
    function AssertTrue {
        param($expression);
    
        $stackFrame = (Get-PSCallStack)[1]
    
        try{
            $result = . $expression;
            if($result -eq $true){
                LogPass
            }else{
                LogFailure ("Evaluation Failed expected ""$true"" got ""$false""") $stackFrame
            }
        }catch [Exception]{
            LogFailure "Unexpected exception encountered" $stackFrame
        }
    }
    
    function LogPass {
        #Do nothing
    }
    
    function LogFailure {
        param($message, $stackFrame);
        "Failure Encounterd";
        "Command: $($stackFrame.Command)"
        "Location: $($stackFrame.Location)";
        "Message: $message";
    }
    

    And

    #File 2 (TestCase.ps1)
    #Runs the tests using the tester object
    
    . (Resolve-Path "Tester.ps1");
    
    function TestFailure {
        $expression = {$false};
        AssertTrue $expression
    }
    
    TestFailure
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.