Skip to content

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

Closed

Description

@JustinGrote

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

Activity

  1. nohwnd commented on Jun 15, 2021

    @nohwnd
    Member

    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

  2. fflaten commented on Jun 15, 2021

    @fflaten
    Contributor

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

    Concept probably still applies though.

  3. nohwnd commented on Jun 15, 2021

    @nohwnd
    Member

    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.

  4. JustinGrote commented on Jun 15, 2021

    @JustinGrote
    ContributorAuthor

    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
    
  5. added this to the 5.4 milestone on Jun 16, 2021
  6. ArmaanMcleod commented on Jul 11, 2021

    @ArmaanMcleod
    Contributor

    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.

  7. nohwnd commented on Jul 12, 2021

    @nohwnd
    Member

    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.

  8. JustinGrote commented on Dec 14, 2021

    @JustinGrote
    ContributorAuthor

    6 month follow up :)

  9. nohwnd commented on Apr 22, 2022

    @nohwnd
    Member

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

  10. added a commit that references this issue on Jul 19, 2023
    f9d930d
  11. added a commit that references this issue on Apr 16, 2024
    fa6f0b2
  12. JustinGrote commented on Apr 16, 2024

    @JustinGrote
    ContributorAuthor

    @nohwnd thank you!

  13. nohwnd commented on Apr 25, 2024

    @nohwnd
    Member

    Thank you :)

  14. nohwnd commented on Apr 25, 2024

    @nohwnd
    Member

    It is now released in beta if you want to promote or try it further.

  15. fflaten commented on Apr 26, 2024

    @fflaten
    Contributor

    @nohwnd The github release tag + notes are missing this + 3 other commits.
    image

    The published build is fine.

  16. nohwnd commented on Apr 26, 2024

    @nohwnd
    Member
  17. nohwnd commented on Apr 26, 2024

    @nohwnd
    Member

    I've found only 2 commits missing, the tag was on wrong commit. not sure what other 2 I a missing, maybe those are the two for dropping powershell support and revert of that , which are linked at the bottom of the release notes?

  18. fflaten commented on Apr 26, 2024

    @fflaten
    Contributor

    I think it was the azure-pipelines commits. But all good now, thanks 🙂

  19. nohwnd commented on Apr 26, 2024

    @nohwnd
    Member

    Yeah I think I made a commit locally into main, and then pulled because there was extra work, but instead of forwarding it merged, and I did not want that, so I reset to the remote state, cherry-pick the changes and push.

    But before this I've tagged the latest commit, but that commit did not end up on main, because I cherry picked it.

    And I do recall that I've reapplied the tag, but I probably did it wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions