locked
DTE and Unit Testing RRS feed

  • Question

  • I use VS 2008, and DTE object for extensibility.

    any good patterns and practices for use DTE and unit testing ?? I need test easy way my code that uses DTE (Solution2, Project, etc).

    Thx.


    Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackoverflow.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts
    Wednesday, December 1, 2010 10:42 PM

Answers

  • Hello,

    I don't think you can do useful add-in/package testing with mocking, you need the real VS IDE hosting your add-in to test something useful. If you want to use the MS testing tools you need a custom host adapter for the VS IDE. You have such a sample in the VS SDK but:

    - The sample provided in the VS 2005 SDK was not completed.

    - The sample provided in the VS 2008 SDK actually works (kind of)

    - There is no sample in the VS 2010 SDK if I remember well.

    The information is here about the VS IDE Host adapter:

    http://msdn.microsoft.com/en-us/library/bb286982%28v=VS.90%29.aspx

    That said, the VS IDE host adapter is terribly frustrating because the .NET Remoting that the agent side uses to communicate with the host side causes:

    - VS hangs when you try to evaluate a variable (in the Watch window, hovering over it with the mouse, etc.). Needless to say, this makes debugging near to impossible.

    - There are threading issues.

    - VS crashes frequently

    I will save you a lot of time and frustration: give up the VS IDE host adapter and use your own unit testing approach instead: when the add-in is loaded, using Reflection:

    - Get the types on your assembly that are unit test classes

    - Get the test methods in them

    - Execute them using the Invoke method

    - Show the failed results in a messagebox, log them to a file etc.

    That is, you create your own unit testing infrastructure. It seems more work but in the long run is better (I have followed both approaches). You can create a "UnitTesting" solution and project configuration with a conditional compilation constant to differentiate the normal behavior and the unit testing behavior.



    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/
    • Marked as answer by Victor_Chen Wednesday, December 8, 2010 8:55 AM
    Saturday, December 4, 2010 7:51 PM

All replies

  • Hi Alhambra,

     

    Thanks for your post.

    Could you please let me know which project you'd like to unit test?

    If it is a VSPackage, you can add the package unit test project from New project wizard. By that project, you can test any method or usecases using DTE.

    Else if it is an addin, you can right click a method and select "Create Unit Test", then a new unit test project created. In that project, two important interfaces you need to mock, they are DTE2 and AddIn. And also mock every child interface until reach to the one you expect to test.

    If my explanation is not clear, please provide some detail scenario and we can figure this out much easier.

     

    Best Regards,

    Ziwei Chen

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, December 3, 2010 7:54 AM
  • It is a addin.

    But how can I "mock" it ?? any sample code ??

     

    I use Solution2 and Project class of DTE, and in unit test , how can I get it from sln-csproj files in disk ??

     

    thanks


    Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackoverflow.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts
    Friday, December 3, 2010 9:16 AM
  • Hello,

    I don't think you can do useful add-in/package testing with mocking, you need the real VS IDE hosting your add-in to test something useful. If you want to use the MS testing tools you need a custom host adapter for the VS IDE. You have such a sample in the VS SDK but:

    - The sample provided in the VS 2005 SDK was not completed.

    - The sample provided in the VS 2008 SDK actually works (kind of)

    - There is no sample in the VS 2010 SDK if I remember well.

    The information is here about the VS IDE Host adapter:

    http://msdn.microsoft.com/en-us/library/bb286982%28v=VS.90%29.aspx

    That said, the VS IDE host adapter is terribly frustrating because the .NET Remoting that the agent side uses to communicate with the host side causes:

    - VS hangs when you try to evaluate a variable (in the Watch window, hovering over it with the mouse, etc.). Needless to say, this makes debugging near to impossible.

    - There are threading issues.

    - VS crashes frequently

    I will save you a lot of time and frustration: give up the VS IDE host adapter and use your own unit testing approach instead: when the add-in is loaded, using Reflection:

    - Get the types on your assembly that are unit test classes

    - Get the test methods in them

    - Execute them using the Invoke method

    - Show the failed results in a messagebox, log them to a file etc.

    That is, you create your own unit testing infrastructure. It seems more work but in the long run is better (I have followed both approaches). You can create a "UnitTesting" solution and project configuration with a conditional compilation constant to differentiate the normal behavior and the unit testing behavior.



    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/
    • Marked as answer by Victor_Chen Wednesday, December 8, 2010 8:55 AM
    Saturday, December 4, 2010 7:51 PM
  • Hi Carols,

     

     

     

    Thanks for the information.

     

    Best Regards,

    Ziwei Chen

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, December 8, 2010 8:55 AM
  • Thanks, very very gratefulness and useful !!!

    are there samples with code in msdn or codeplex about it ?? It seems working in ____ :-)


    Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackoverflow.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts
    Wednesday, December 8, 2010 11:31 AM
  • Hi 

    I have tried to create project using above your mention code snippet. But I get below mention errors randomly,

    1).System.Runtime.InteropServices.COMException (0x80080005): Retrieving the COM class factory for component with CLSID {656D8328-93F5-41A7-A48C-B42858161F25} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)). 

    2)."RPC server is unavailable.(Exception from HRESULT:0x800706BA)" 

    Can you please suggest how to resolve above mention issues ? "

    Best Regards,


    - Abbas K



    Thursday, January 5, 2017 9:14 AM
  • When automating DTE, the following approach must be used:

    Fixing 'Application is Busy' and 'Call was Rejected By Callee' Errors

    https://msdn.microsoft.com/en-us/library/ms228772(VS.80).aspx


    My portal and blog about VSX: http://www.visualstudioextensibility.com
    Twitter: https://twitter.com/VSExtensibility
    MZ-Tools productivity extension for Visual Studio: https://www.mztools.com

    Thursday, January 5, 2017 1:12 PM
  • Thanks for your response.

    I have already used IOleMessageFilter  error handlers.Sometimes IOleMessageFilter error handlers will be resolve run time exception, But I get exception like System.Runtime.InteropServices.COMException (0x80080005) randomly, When run unit test cases.

    I have get another issue in project create function. When create project DTE instance,one or more Visual Studio instance will be created. Please refer below screen for showing one or more Visual Studio instance in Task Manager.

    Can you please suggest any other solution to resolve above mention issues ? "

    Best Regards,


    - Abbas K

    Friday, January 6, 2017 5:24 AM
  • Hi,

    I don't know about the System.Runtime.InteropServices.COMException (0x80080005).

    Regarding the multiple instances, it could be that:

    - Each test creates its own instance of DTE. In this case, code your tests such as only one DTE instance is created at test suite level and that only one test is executed at the same time.

    - The DTE instance is not closed after a test session, so after multiple test sessions multiple DTE instances remain opened. In this case:

    1) Rather than calling dte.Solution.Open, use:

    dte =....

    solution = dte.Solution;

    solution.Open(...)

    2) Call Marshal.ReleaseComObject for each object:

    dte =....

    solution = dte.Solution;

    solution.Open(...)

    Marshal.ReleaseComObject(solution);

    solution = null;

    dte.Quit();

    Marshal.ReleaseComObject(dte)

    dte = null;

    For more info see:

    https://www.add-in-express.com/creating-addins-blog/2013/11/05/release-excel-com-objects/


    My portal and blog about VSX: <a href="http://www.visualstudioextensibility.com"> http://www.visualstudioextensibility.com</a> <br/> Twitter: <a href="https://twitter.com/VSExtensibility">https://twitter.com/VSExtensibility</a> <br/> MZ-Tools productivity extension for Visual Studio: <a href="https://www.mztools.com"> https://www.mztools.com</a>

    Friday, January 6, 2017 6:27 AM