我有一个使用 Office PIA 的 Excel 包装类。我还有一个之前编写的在 NUnit 上运行的有限测试套件。我们正在工作中迁移到 TFS2010,因此我也将 NUnit 测试迁移到 MSTest。
测试套件在我的开发计算机上运行良好,并且如果在运行构建代理的计算机上使用 MSTest 命令行实用程序手动执行。但是,当通过 Team Build 执行时,所有与磁盘 I/O(打开、保存等)相关的测试都会失败。我的构建代理在域帐户上运行,并且该域帐户也是同一台计算机上的本地管理员。几个不执行任何磁盘 I/O 的测试运行良好,因此我知道 Excel 正在被触发并且可用。似乎只是权限问题或 Team Build 流程的限制。
这是一个示例函数。这让我认为这是 Excel I/O 问题。 File.Exists 检查顺利通过。我在测试运行中没有收到 FileNotFoundException,而是直接从互操作层收到 COMException。
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);
}
异常(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
最佳答案
我遇到了类似的问题,这个解决方案对我有用: http://blogs.msdn.com/b/sqlserverfaq/archive/2010/04/30/unable-to-open-excel-files-using-a-cscript-in-sql-server-jobs.aspx
“systemprofile”文件夹中似乎需要一个“Desktop”文件夹。
在位置 C:\Windows\SysWOW64\config\systemprofile 下创建 Windows 2008 Server (x64) 的“桌面”文件夹
对于 32 位 Windows 2008 Server,在位置 C:\Windows\System32\config\systemprofile 下创建“Desktop”文件夹
关于excel - 在 Team Build 2010 下针对 Excel 运行单元测试时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5642351/