Which is better? Unit-test project per solution or per project?

Assemblies are a packaging/deployment concern, so we usually split them out because we don’t want to deploy them with our product. Whether you split them out per library or per solution there are merits to both.

Ultimately, you want tests to be immediately available to all developers, so that developers know where to find them when needed. You also want an obstacle free environment with minimal overhead to writing new tests so that you aren’t arming the cynics who don’t want to write tests. Tests must also compile and execute quickly – project structure can play a part in all of this.

You may also want to consider that different levels of testing are possible, such as tests for unit, integration or UI automation. Segregating these types of tests is possible in some tools by using test categories, but sometimes it’s easier for execution or reporting if they are separate libraries. 

If you have special packaging considerations such as a modular application where modules should not be aware of one another, your test projects should also reflect this.

In small projects where there aren’t a lot of projects, a 1:1 ratio is usually the preferred approach. However, Visual Studio performance quickly degrades as the number of projects increases. Around the 40 project mark compilation becomes an obstacle to compiling and running the tests, so larger projects may benefit from consolidating test projects. 

I tend to prefer a pragmatic approach so that complexity is appropriate to the problem.  Typically, an application will be comprised of several layers where each layer may have multiple projects.  I like to start with a single test library per layer and I mimic the solution structure using folders. Divide when complexity warrants it. If you design your test projects for flexibility then changeover is usually painless.

Leave a Comment