Skip to content

Analyzer suggestion: Don't assert in catch blocks #6788

@Youssef1313

Description

@Youssef1313

I have seen developers doing this pattern a lot.

try
{
    // code that throws
}
catch
{
    // do some asserts.
}

The developer intent is usually "assert that the code throws, and assert some stuff later" (assertions may or may not use the exception object).

The reality is, this doesn't really assert that the code throws. If the code didn't throw at all, the test will still pass.

Such patterns make the tests very hard to read and understand, and can easily lead to bugs in production that are not caught by tests.

FYI @stan-sz :)

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions