5
Assert.NotNull(res);
Assert.Equal(1, res.Foo); // CS8602 warning here

I'm using xunit.assert 2.4.1, .NET 6.0, VS 2022.

When "Ctrl+Click" navigating to the Assert.NotNull() source code I can see it defined as

public static void NotNull(object @object)

while I was actually expecting to see

public static void NotNull([NotNull] object? @object)

From the xunit source code one can see that nullable flavour of methods are used only when XUNIT_NULLABLE conditional compilation variable is enabled. Can it be that nuget downloaded "non-nullable" version of the xunit.assert package? How can we force a "nullable" version (built with the XUNIT_NULLABLE defined)?

3
  • It’s nothing to do with XUnit. The C# compiler is telling you that at this point the value can still be null. Commented Apr 11, 2022 at 16:28
  • 1
    @scottdavidwalker, I think you misunderstood my question. For sure C# compiler generates the warning, but it does this because Assert.NotNull() does not prevent the compiler from telling the warning. You can create a custom NotNull() method private void NotNull([NotNull] object? @object) {} and use it to silence the compiler, so why cannot NUnit Assert.NotNull() do the same? And actually it can, please follow the link I've shared in the question and maybe you'll get my question.
    – Roman
    Commented Apr 12, 2022 at 18:27
  • 2
    It's coming as a part of v3. github.com/xunit/xunit/issues/2011 I presume that the nuget package is compiled without the flag set so we don't have that ability yet. Commented Apr 14, 2022 at 8:59

1 Answer 1

5

Finally, xunit 2.4.2 has been released and Assert.NotNull() plays nicely with C# nullable types!!

The following code is the implementation of NotNull method in xunit.assert 2.4.2.

        /// <summary>
        /// Verifies that an object reference is not null.
        /// </summary>
        /// <param name="object">The object to be validated</param>
        /// <exception cref="NotNullException">Thrown when the object reference is null</exception>
#if XUNIT_NULLABLE
        public static void NotNull([NotNull] object? @object)
#else
        public static void NotNull(object @object)
#endif
        {
            if (@object == null)
                throw new NotNullException();
        }
4
  • I still get a warning on subsequent usages of of the object passed to Assert.NotNull Commented Nov 15, 2022 at 0:49
  • @PaulWheeler what version of xunit NuGet package do you use?
    – Yas Ikeda
    Commented Nov 15, 2022 at 3:37
  • 2.4.2 with .Net 6.0 and C# 10. I have a nullable dynamic variable dynamic? test = ... and I pass it to Assert.NotNull(test) but I still get warnings on subsequent lines like Assert.NotNull(test.member). I don't think it's an xunit issue per se because the same happens when I write my own version of Assert.NotNull the only way I can get it to work is with a generic function that returns the same type as it's argument and is decorated with [return: NotNull] in that case test = MyAssertNotNull(test); resolves the warnings. Commented Nov 15, 2022 at 4:11
  • My issue seems to be specific to using dynamic? It works fine for Object? thanks. Commented Nov 15, 2022 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.