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
unit-testing visual-studio ms-office mstest rcw
namenlos
source share