4

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 Fail2wasn'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 ?

Martin Ba
  • 37,187
  • 33
  • 183
  • 337
  • You could potentially use GetType().IsPublic somewhere in your base class, maybe check if all methods are public and anything else throw a warning message. – Watsyyy Oct 18 '19 at 08:55
  • If you run test after test method added, you quickly notice that 1. New test wasn't discovered by Test Explorer, 2. Test wasn't executed during. Even better if you write test before writing code, you should get failed test before. Which will indicated very very quickly that something wrong, because nothing failed. – Fabio Oct 18 '19 at 09:39

2 Answers2

2

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.

canton7
  • 37,633
  • 3
  • 64
  • 77
1

There is a really cool formatting tool, resharper where you can set different styling guides and might be able to make a rule for that but i'm not certain. Even if it doesn't work for this issue I would recommend it if you are working in visual studios.

jen
  • 195
  • 1
  • 7