I've been rewriting all our tests that use Assert.Multiple to Assert.EnterMultipleScope but came across this issue that makes it currently impossible.
Given the following old code:
var restResponse = new RestResponse();
Assert.Multiple(() =>
{
Assert.That(restResponse.StatusCode, Is.EqualTo(HttpStatusCode.OK));
Assert.That(restResponse.Content, Is.Not.Null);
}
// restResponse.Content is definitely not null here
Assert.That(restResponse.Content.ToString(), Is.EqualTo("Test"));
And the code with EnterMultipleScope:
var restResponse = new RestResponse();
using (Assert.EnterMultipleScope())
{
Assert.That(restResponse.StatusCode, Is.EqualTo(HttpStatusCode.OK));
Assert.That(restResponse.Content, Is.Not.Null);
}
// restResponse.Content may be null here
Assert.That(restResponse.Content.ToString(), Is.EqualTo("Test"));
In the old version, the last assert does not produce a warning, because the analyzer knows that restResponse.Content is not null, or it would have failed earlier. In the newer version, it does produce a warning because it apparently does not know that restResponse.Content is definitely not null.
Both should work the same in detecting whether something can be null further on in the test.
I've been rewriting all our tests that use
Assert.MultipletoAssert.EnterMultipleScopebut came across this issue that makes it currently impossible.Given the following old code:
And the code with
EnterMultipleScope:In the old version, the last assert does not produce a warning, because the analyzer knows that
restResponse.Contentis not null, or it would have failed earlier. In the newer version, it does produce a warning because it apparently does not know thatrestResponse.Contentis definitely not null.Both should work the same in detecting whether something can be null further on in the test.