Search code examples
c#asp.net-mvcunit-testingnunitconventions

Unit Test Order Convention?


I am creating unit tests for an ASP.NET MVC application in C# using NUnit.

Short Version: Should I order my test methods alphabetically or by order of the methods belonging to the class being tested?

For Instance: Say I want to test the class:

public class MyClass{
     public void B(){}
     public void A(){}
     public void C(){}
}

Would it be more proper for me to structure my test class like so...

[TestFixture]
public class MyClassTests{
         [Test]public void Test_B(){}
         [Test]public void Test_A(){}
         [Test]public void Test_C(){}
    }

or like this...

[TestFixture]
public class MyClassTests{
         [Test]public void Test_A(){}
         [Test]public void Test_B(){}
         [Test]public void Test_C(){}
    }

A simple question I know, but I could not find any convention for this from my googling, just lots of conventions for naming test methods. Thanks for your help!


Solution

  • I'd do them the first way.

    I'd also remove Test_ from your test names, as it's redundant naming.

    The best naming convention I've seen for naming tests is:

    MethodName_ContextOfTest_ResultOfTest

    Example:

    A_WithNullString_ThrowsException

    Makes it very easy to write your tests to test specific things, and makes it much easier for others to understand what you're testing too.