.NET Conditional Compiler Symbols and Unit Test Libraries
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 uni开发者_C百科t 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).
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.
精彩评论