views:

46

answers:

1

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  A: 

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
Roman Kuzmin
thanks, that was driving me up the wall
Willbill