views:

226

answers:

1

My team has a set of unit test libraries that run against our application code - unfortunately they are throwing (unexpected) exceptions. The reason for this is that our logging code is being called and the objects aren't setup. The logging code is executed via a method attribute we have setup using PostSharp (which get called before and after the method body executes). The attribute should not be called.

Here's where PostSharp helps us out: it looks for a Conditional Compiler Directive (SKIPPOSTSHARP) which tells it not to execute. In other words, if we define the directive while the unit tests are being executed then we are able to test the desired code independently of the logging aspect - successfully avoiding unnecessary dependencies and complicated mocking code along the way.

This is Great! Except for one catch... A Conditional Compiler Directive defined for our test project is not going to be defined for the project that is being tested - meaning that our code under test still uses the attribute and our tests all fail.

Is it possible to have a compiler directive defined across a whole solution only when running unit tests? Even if we're not applying it to the whole solution, can a directive be applied to a specific project only when we're running unit tests (this would work equally as well)?

I'd also be interested in seeing opinions about this approach (we're trying to achieve seperation and scalability via the AOP code but there does appear to be a few extra hoops for us to jump through as we progress).

NOTE: I'm not talking integration testing here just unit testing. We only want to test the specific unit and we don't care whether the logging code works or not - that gets tested by other unit tests (which work fine).

+2  A: 

I would suggest creating a special build configuration for testing. There are maybe some ways to detect whether a project is built because of unit tests being executed (possibly some MSBuild property), but it would be unreliable in incremental build scenarios.

Gael Fraiteur
Thanks for the response Gael (I guess you're watching the PostSharp keywords - always nice to hear from the creator of a product!). I don't have very in-depth knowledge of MSBuild so I will need to investigate this and see what it is capable of. Are there any resources that you know of which would be able to point me in the right direction?
Zac
The trick is to enable diagnostic verbosity in Visual Studio Build Options, then to see if there is some interesting property being set in conditions of interest. For instance, Visual Studio executes the MSBuild project even when it does not build the project; in this case the property BuildingProject is set to false. You won't find this in documentation. Using the same approach, you could find something interesting to you.But I don't expect it to be robust; I would really create a new project configuration. As you have "Debug" and "Release", you will have "Test".
Gael Fraiteur