harlam357
harlam357

Reputation: 1491

Exceptions running unit tests against Excel under Team Build 2010

I have an Excel wrapper class using the Office PIAs. I also have a limited test suite that was previously written to run on NUnit. We are migrating to TFS2010 at work so I'm migrating the NUnit test overs to MSTest as well.

The test suite runs fine on my dev machine and if executed manually with MSTest command line utility on the machine that runs the build agent. However, when executed through Team Build all the tests that have anything to do with disk I/O (Open, Save, etc) fail. My build agent is running on a domain account and that domain account is also a local admin on the same machine. The few tests that do not do any disk I/O run fine, so I know Excel is being fired and available. Just seems like a permissions issue or a limitation of the Team Build process.

So here's an example function. This is what makes me think it's an Excel I/O issue. The File.Exists check passes fine. I'm not receiving a FileNotFoundException in my test run, instead I'm receiving a COMException directly from the interop layer.

public void OpenXLS(string workbookFilePath) 
{
   // Make sure given file path exists
   if (!File.Exists(workbookFilePath))
   {
      throw new FileNotFoundException(String.Format(CultureInfo.CurrentCulture,
         "File '{0}' cannot be found.", workbookFilePath));
   }

   // Open the Workbook
   _xlsWorkbook = _xlsWorkbooks.Open(workbookFilePath, 0, false, Missing.Value,
      "", Missing.Value, true, Missing.Value, Missing.Value, true, false,
      Missing.Value, Missing.Value, Missing.Value, Missing.Value);
}

The Exception:

System.Runtime.InteropServices.COMException: Microsoft Excel cannot access the file 
'C:\BuildPath\TestResults\TestRun\Out\TestBook.xls'. There are several possible reasons:

• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open 

Upvotes: 2

Views: 318

Answers (2)

Christophe Cadilhac
Christophe Cadilhac

Reputation: 135

Harlam357, you made my day !

However, it seems that, even on a 64 bits machine, you need to create the "Desktop" folder in C:\Windows\System32\config\systemprofile. I've created it in both directories to make sure. And don't forget to give Full Control to the Service Account, maybe it's needed.

Upvotes: 0

agnes
agnes

Reputation: 56

I had similar problem and this solution worked for me: http://blogs.msdn.com/b/sqlserverfaq/archive/2010/04/30/unable-to-open-excel-files-using-a-cscript-in-sql-server-jobs.aspx

A “Desktop” folder seems to be necessary in the “systemprofile” folder.

  • Create the “Desktop” folder for Windows 2008 Server (x64) under the location C:\Windows\SysWOW64\config\systemprofile

  • And for a 32 bit Windows 2008 Server create the “Desktop” folder under the location C:\Windows\System32\config\systemprofile

Upvotes: 4

Related Questions