We had a case of a missed/skipped test case today:
The developer missed making the [Test]
public and so NUnit (v 3.9.0) didn't didn't run it.
[TestFixture]
public class StackTests
{
[Test]
public void Fail1()
{
Assert.Fail("Will be run");
}
[Test]
void Fail2()
{
Assert.Fail("will NOT be run - none the wiser");
}
}
When the developer noticed that Fail2
wasn't running, he spent another 20 minutes trying to figure out why it wasn't discovered, only to 🤦(U+1F926) when we noticed the missing public
.
It has been my experience that the missing public
on NUnit [Test]
methods is a repeated stumbling block and easily missed.
Is there any way to make NUnit or the compiler warn about non-public [Test]
methods?
Or are we stuck with the occasional 🤦🤦🤦 ?
There's an analyzers package for NUnit. It doesn't look like there's an analyzer for your case yet, but there is an open issue.
You could contribute an analyzer -- it looks like the discussion got stuck on what constructors a test fixture should have, and I suspect they would accept an analyzer which just checked that test methods were public, which should be fairly straightforward to write.
Another option is to adopt a coding standard of always specifying the access modifier -- so void Fail2()
would be banned, but public void Fail2()
and private void Fail2()
would be accepted. You can enforce this with the .editorconfig rule dotnet_style_require_accessibility_modifiers = always
.