views:

117

answers:

4

I would like to optimize my unit test assemblies. Specifically, I would like to make sure that multiple developers can work concurrently without stepping on each others' toes. What is the best practice when creating unit tests? One unit test class per real class, unit tests broken up by concern rather than by class, one large unit test class for all classes (i doubt it), etc.??

+1  A: 

I like the one test class for a class convention from java. This makes sure that you always know where to find a test and the test units are small enough to have many people working on them without having to much merging to do because they are all in one file.

It also enables you to run only the tests for one class alone if you have a rather larger test suite and you are working closely on this one class.

Janusz
+1  A: 

There are tree possible approach to organize unit tests:

  1. TestMethod in test class per testing class. The most popular one.
  2. TestMethod in test class per test fixture. Allow to minimize text fixture/tear down code.
  3. TestMethod in test class per system under test(subsystem, module, couple of classes).

Usually there are needs to keep several automated test assemblies:

  • Unit test Assembly - with pure unit test, that is testing each unit using mocks, test dumps.
  • Integration/Consumer test assembly - intended to check integration of system units, checks that consumer critical functionality work properly.
klashar
A: 

I think the best way is to differentiate on behaviour. One behavior for each testclass. Which means severaltest classes for each class. A related question with example can be found here.

Cellfish
A: 

The approach that I have gravitated toward is a context specification style of unit testing that breaks your unit tests into groupings of behavior; for example If I am working on a new ASP.NET web page for a web site called the job status page I would have a directory structure like this:

unit-tests -> spec_for_job_status_page

where spec_for_job_status_page is the folder that contains the solution file, csproj file and related classes.

As far as the structure of the unit test classes are concerned I like to use a naming convention that follows the context specification style such as:

namespace spec_for_job_status_page
{
    [TestFixture]
    public class when_the_job_status_page_is_loaded
    {
        [SetUp]
        public void context()
        {
            //write set-up code here for your test
        }

        [Test]
        public void should_show_the_job_number()
        {
            //write the assertion code here.  Should be a single line
        }
    }
}
Michael Mann