My apologies in advance for a terrible title- suggestions welcome!
I've been reading about DI and AOP, and I think I grasp the basics; at least for the canonical example of adding logging.
I would like to apply this to the test cases we've been creating in NUnit e.g. be able to automatically add entry/exit logging for all test case methods and any 'helper methods' that they call. (And I'm not tied to NUnit- if it's easier in another framework, please let me know.)
NOTE- this is not about the subject under test; I want to apply these techniques to the testcases themselves.
It's pretty clear how to do this using PostSharp - it's their First example. However I don't want to add the handling of their licensing to our project for just this experiment.
All the other references I've found to AOP for C# are based around (dynamic) Interceptors provided by IoC container implementations such as CastleWindsor, Unity, Spring.Net, ... They all have a common problem in this case: you need a piece of setup-code that creates the proxy for the object you want to add interceptors to. (I did originally think this code would also have to create an IoC container, but I see I'm wrong there.)
But I can't see where this setup code would go for nUnit testcases.
The options I've come up with, and their problems:
- Have the testfixture classes constructor create a proxy to itself. Won't work, due to recursion (consumer asks for thing, thing tries to return proxy to thing, proxy tries to create thing... from reading this StackOverflow question)
- Roll my-own reflection based magic (which would be a big undertaking for me)
- have the constructor wrap all methods in the testfixture class and return this 'wrapped' object (not sure if this it's possible for a constructor to do this)
- use a static constructor on the testfixture to do this magic (assuming you can dynamically wrap the methods of the class in place.)
- do something at the module level using a module cctor (via Einar Egilsson's InjectModuleInitializer) and wrap all methods in all classes with logging.
- The simplest: some kind of factory for instantiating the testcases (NOT parameters for the tests), from which I could use one of the IoC Proxy Generators
- For nUnit: the only means I can find to do this is to create a custom AddIn. Advantage- may not break integration with ReSharper. Disadvantage- deploying to all devs machines, especially on updates to NUnit. Are there other ways of doing this for nUnit?
- For MbUnit: looks like it treats testcases as first class values, and this is straight-forward. Advantage: easy to deploy to all developers. Disadvantage: tests aren't going to show up in Resharper. Side note: how to handle setup and teardown.
Have I missed anything in my options and conclusions?
Are there any simpler means of doing this that I've missed?