Over on the Alt.Net mailing list (here) the topic of where to put your unit tests was the hot topic a few days ago. This is a topic that I have debated with various people over the past few years. With all the debates I have had, it is clear to me that there really is no right or wrong way do to this, but it is just a matter of philosophical opinion.
On this list there are various opinions (and supporting experiences to support those opinions) about which method is better or preferred. As this is a topic that many developers chat about, I thought I would put a list of the various advantages/disadvantages into location.
Whitebox Advantages
- Location of tests to their corresponding class under test.
- Very easy to test internal methods without the need of using the InternalVisibleTo attribute.
- Reduces the number of projects you have in your solution because you will not need a test project for each assembly.
Whitebox Disadvantages
- Have to add compiler switches to ensure the tests are NOT compiled into the release code. This is easily done, but is just something else that you have to setup and remember to do.
- Addition of various test assemblies in the release of your project. This includes stuff like the test framework assemblies, any mocking assemblies, etc.
- Slower compilation time.
Blackbox Advantages
- You test your assembly exactly like a calling application would use the assembly. This can be very beneficial as it can flush out implementation bugs very easily.
- If following true TDD, should create a simpler interface as you can only test the calls that are public and this is the way the user will use the assembly.
- No need to put any special compiler switches in the code in order to not release the tests.
- No worries about deploying test code as it is in a separate assembly.
Blackbox Disadvantages
- Additional projects in your solution.
- May need to add the InternalsVisibleTo to the various assemblies being tested if you need to tests internal methods (if this is the case, you may have a code smell on your hands).
As you can see from the list of pros/cons above, both whitebox and blackbox testing have upsides and downsides. At the end of the day this comes down to a personal/philosophical decision and both are better then the alternative....NO TESTS.
Till next time,
*** Note, some of the listed advantages/disadvantages above were taken from various postings on the Alt.Net mailing list. Credit goes to the various authors in these cases ***