A test expects an exception with [ExpectedException], which passes when any line of the test throws it
What it finds
[ExpectedException] makes the test pass if the exception comes from anywhere in it, including the setup before the call under test. Assert.ThrowsException or Assert.Throws around the one call says exactly where it must come from.
From the rule's source, expected_exception_attribute.rs.
using System;using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass]public class Parser{ [TestMethod] [ExpectedException(typeof(FormatException))]tx0 reports: the exception may come from any line of the test public void Rejects() { }} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/csharp:expected-exception-attribute" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/csharp:expected-exception-attribute] reason="..."tx0 explain tx0/csharp:expected-exception-attribute shows the same in the terminal. Found a false positive? Report it.