testfx icon indicating copy to clipboard operation
testfx copied to clipboard

Openness for Caller Argument Expression in Assert failure messages

Open johnthcall opened this issue 3 years ago • 8 comments

Description

With .Net 6 Caller Argument Expression was added, this could be used in testfx to provide the expression which failed the check. Doing this could provider consumers more out of the box detail about what caused the assert to fail without having to manually provide messages.

Sample Code change

#if !NET6_0_OR_GREATER
        public static void IsNull(object value)
        {
            IsNull(value, string.Empty, null);
        }
#endif

        public static void IsNull(
            object value,
#if !NET6_0_OR_GREATER
            string message)
#else
            [CallerArgumentExpression("value")]string message = "")
#endif
        {
            IsNull(value, message, null);
        }

Work required

  • Add .Net 6 as a built/packaged runtime
  • Modify Assert code to make the flows without a message removed in .Net 6 so that code like Assert.IsNull(myParam) execute passing myParam as the message.

Change in behavior

Currently a failing call to Assert.IsNull without a message will just return Assert.IsNull failed. This would change to Assert.IsNull failed. myParam which could be considered a breaking change and a result a no-go.

AB#1588958

johnthcall avatar Jul 18 '22 17:07 johnthcall

Fixed by #1154

Evangelink avatar Aug 25 '22 15:08 Evangelink

Fixed by #1172

johnthcall avatar Aug 25 '22 17:08 johnthcall

Hey @johnthcall, thanks! And sorry for the typo :)

Evangelink avatar Aug 26 '22 07:08 Evangelink

Reopening the issue as changes are reverted. Long story short we misidentified the impact and as it is introducing some breaking changes we will postpone its fix in v4 of MSTest.

Evangelink avatar Sep 14 '22 13:09 Evangelink

Just to confirm, is [CallerArgumentExpression] not supported at all currently by MSTest? Meaning no methods using the attribute would be able to be unit tested using MSTest?

CameronBellNZX avatar Nov 28 '22 18:11 CameronBellNZX

If your method has some argument marked with the CallerArgumentExpression attribute there is no problem for the method to be tested. The goal of this ticket is to rewrite MSTest assertions methods to be using this attribute so that if the user provides no custom message we can provide a better default message (being the caller expression).

Evangelink avatar Nov 29 '22 08:11 Evangelink

Ahh I see, thanks for the clarification

CameronBellNZX avatar Nov 29 '22 20:11 CameronBellNZX