Pester icon indicating copy to clipboard operation
Pester copied to clipboard

Feature Request: Expected, Actual, and Because as Test Property Fields

Open JustinGrote opened this issue 3 years ago • 8 comments

Summary of the feature request

The vscode proposed test UI has a very nice view window showing expected and actual results including a diff image

I would like to be able to display this similar information from Pester, but currently since the error is just a string, it would require a lot of parsing.

How should it work? (optional)

Ideally a failure would add 3 parameters either to the data of the exception or to the testresult itself: Example with Should -Be: Expected $false, but got $true

  1. Expected - $false
  2. Actual - $true

Example with Should -BeOfType: Expected the value to have type [int] or any of its subtypes, but got $true with type [bool].

  1. Expected: [int]
  2. Actual: [bool]

Also support Because, which would probably be shown as a comment to Expected

As a workaround a regex parsing of "Expected x, but y" would probably suffice for most scenarios

JustinGrote avatar Jun 15 '21 06:06 JustinGrote

Sounds good. We already transport some data on the exception (in the data dictionary of the exception), this would be adding more items. We should make sure we save the serialized version of the object, to avoid holding references to the real object.

https://github.com/pester/Pester/blob/main/src/functions/assertions/Should.ps1#L17-L18

nohwnd avatar Jun 15 '21 08:06 nohwnd

IIRC that function isn't called anymore. It's been converted to a c# method in Pester.Factory.

Concept probably still applies though.

fflaten avatar Jun 15 '21 10:06 fflaten

Yeah, the concept should be okay, the data are imho attached to the data dictionary on the exeception which is defined on the Exception base class so any .net exception has it.

nohwnd avatar Jun 15 '21 10:06 nohwnd

There is some information in targetObject but nothing specific to the error other than the message.

I think the most appropriate storage for this is $error.exception.data (currently blank), I'll go the parse route for now until this is available.

$x.errorrecord.targetobject

Key         Value
---         -----
Message     Expected $false, but got $true.
File        C:\Users\JGrote\Projects\vscode-pester-test-adapter\sample\Tests\Sample.tests.ps1
Line        5
LineText            It 'Context False' {$true | Should -be $false}
Terminating True

JustinGrote avatar Jun 15 '21 20:06 JustinGrote

I'd be happy to PR this @nohwnd @JustinGrote.

A lot of the Should functions return a PSCustomObject with Succeeded/FailureMessage properties. Thinking we can can include Expected/Actual/Because here and adding these to the error record with the CreateShouldErrorRecord call inside Invoke-Assertion. Would also need to modify CreateErrorRecord to include these new properties inside the targetObject dictionary.

This would actually be very useful for the work I'm doing. I am also just parsing the message with regex and being able to access these properties inside the exception would be great.

ArmaanMcleod avatar Jul 11 '21 14:07 ArmaanMcleod

Exception is a universal carrier of failure info. You can throw exceptions from your own assertions if you don't want to use Should. That is why I am suggesting to use Data on Exception object.

nohwnd avatar Jul 12 '21 08:07 nohwnd

6 month follow up :)

JustinGrote avatar Dec 14 '21 22:12 JustinGrote

@JustinGrote hello from 4 more months into the future :D Someone needs to just implement this. Maybe you yourself? 😉

nohwnd avatar Apr 22 '22 20:04 nohwnd