Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert.Multiple and Assert.EnterMultipleScope don't make sense for test code using the try-parse pattern #835

Open
Bartleby2718 opened this issue Jan 19, 2025 · 2 comments

Comments

@Bartleby2718
Copy link
Contributor

Bartleby2718 commented Jan 19, 2025

I think NUnit.Analyzers is too aggressive in recommending Assert.Multiple and Assert.EnterMultipleScope for code following the Try-Parse pattern.

It doesn't make too much sense to group the following assertions into a single Assert.Multiple scope

Assert.That(TryConstructSomething(x, y), Is.True);
Assert.That(y, Is.EqualTo(z));

bool TryConstructSomething(object x, [NotNullWhen(returnValue: true) out object y);

because there's no point in moving on to the second assertion when the first assertion is true.

To reduce (since we probably can't eliminate) a false positive like this, perhaps we can check if any the actual parameters in the assertions came from the out parameter of a previous assertion.

@manfred-brands
Copy link
Member

Note that in this case y is not an out parameter. The attribute only indicates it is not null.
Did you forget the out keyword?

But yes, we could try to detect a Try pattern as being a method having a bool return and an out parameter with a NotNullWhen attribute.

@Bartleby2718
Copy link
Contributor Author

Oops, yeah I missed the out keyword. Just updated the description!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants