Martin Ba
Martin Ba

Reputation: 38941

How can we make sure that all NUnit [Test] methods are public?

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 🤦🤦🤦 ?

Upvotes: 4

Views: 320

Answers (2)

jen
jen

Reputation: 349

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.

Upvotes: 0

canton7
canton7

Reputation: 42330

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.

Upvotes: 2

Related Questions