VS2008 UnitTesting - a separate RCW with Office Application objects (PowerPoint, etc.)

BACKGROUND

  • I can automate PowerPoint 2007 through C #
  • I am writing unittests using Visual Studio's built-in unit testing (Microsoft.VisualStudio.TestTools.UnitTesting) for my code.
  • I am good at Office 2007 application automation

MY PROBLEM

  • When I run my unit tests, the first unit test method works fine, everyone after that has an error regarding disconnected RCW
  • I am creating a static PowerPoint instance for testing methods for sharing, but it looks like the RCW application is shutting down after starting the first testing method.

SOURCE CODE

using System; using System.Text; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace TestDemo { [TestClass] public class UnitTest1 { private static Microsoft.Office.Interop.PowerPoint.ApplicationClass g_app = new Microsoft.Office.Interop.PowerPoint.ApplicationClass(); private TestContext testContextInstance; public TestContext TestContext { get { return testContextInstance; } set { testContextInstance = value; } } [TestMethod] public void Test01() { g_app.Visible = Microsoft.Office.Core.MsoTriState.msoCTrue; } [TestMethod] public void Test02() { g_app.Visible = Microsoft.Office.Core.MsoTriState.msoCTrue; } } } 

ERROR MESSAGE

 Test method TestDemo.UnitTest1.Test02 threw exception: System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.. 

This message appears in the line where the PowerPoint instance is used (when I set the Visible property)

WHAT I ARTED

  • Unittests order does not change behavior
  • The same thing happens with Word 2007, Visio 2007, etc.
  • When writing test files using NUNIT, I do not get this problem - obviously, something is different from the way the visual studio runs unit tests (not assuming that VS is incorrect, just indicating that it is different from NUNIT).
  • This has nothing to do with the Visible property - any use of a method or property will cause this problem.
  • I tried using the AssemblyInitialize and ClassInitialize attributes to instantiate, but nothing worked
  • Googled and Binged - no clear answer that helps me.

COMMENTS

  • I could switch to NUNIT, but would prefer to continue using the unit test environment for Visual Studio

MY QUESTION

  • How can I successfully create one PowerPoint 2007 instance that will be used by all TestMethods tests
  • If you can make it clear why this is happening, I would be grateful.

SOLVED (THANKS WITH ALCONJA)

  • I followed his advice to change .testrunconfig and it worked.

LINKS

+7
unit-testing visual-studio ms-office mstest rcw
source share
1 answer

It seems that the problem is that MS Unit tests run on multiple threads, while NUnit tests run on a single thread. Thus, a static link to PowerPoint when working in your MS tests is divided between threads , which COM does not like, because by default its STA (single thread). You can switch the MS test to use MTA (multithreading for COM) by adding:

 <ExecutionThread apartmentState="MTA" /> 

to the * .testrunconfig file (open the file as XML and run the above line somewhere in the main TestRunConfiguration node ).

Not sure how well PowerPoint (and your specific tests) will be considered multithreaded, but your trivial example above goes with MTA enabled. If you run into problems with threads, you can try setting up unit tests and see if this fixes the problem.

+7
source share

All Articles