Unit Tests for Custom Tasks

I'm developing some custom tasks and would like to write some Unit Test cases to make it easier to test them during development.  The only problem I've run into thus far is logging.  Because I'm not setting the BuildEngine property on my task before calling Execute(), all of the Log statements throw exceptions.  I haven't been able to locate any information on how to go about setting this property.  Any help would be appreciated.  Thanks.


Derek



Answer this question

Unit Tests for Custom Tasks

  • Jaimi

    You can't. Instead you need to use the assert methods on the MockEngine to verify that the log contains (or doesn't contain) the entries you want. Remember that unit tests should pass or fail in an automated fashion, and shouldn't require you to parse through their logged outputs manually.

  • Patrick Grimme

    Thanks.  This should prove to be useful.  BTW, this is a very basic question, but how do I see the console output of the MockLogger   It doesn't seem to be going to the Output window.  Thanks.


    Derek Greer

  • Ivan Starr

    Derek

    We frequently encounter the same situation when developing tasks, and our solution to it is to use a stubbed out class that implements IBuildEngine, and then set that as the BuildEngine on the task.

    If you are interested, one of the recent MSBuild Extras we put out there has the binaries for the MockEngine type that you can use, instead of building one from scratch on your own.  You can find the AssemblyInfo task here http://www.gotdotnet.com/codegallery/codegallery.aspx id=93d23e13-c653-4815-9e79-16107919f93e - and that has unit tests that show how this approach was used.

    Hope you find this helpful.

    Thanks.
    Faisal Mohamood


  • Unit Tests for Custom Tasks