简体   繁体   English

如何对Excel Workbook项目进行单元测试?

[英]How to unit test an Excel Workbook project?

How to unit test an Excel Workbook project? 如何对Excel Workbook项目进行单元测试?

I use Visual Studio 2012 C#. 我使用Visual Studio 2012 C#。

I have read several posts so far, and it seems I can't simply create a unit test project and then reference the main excel workbook project, because it depends on interop excel. 到目前为止,我已经阅读了几篇文章,似乎我不能简单地创建一个单元测试项目然后引用主要的excel工作簿项目,因为它依赖于interop excel。

Is this a valid solution, and if not can you please give suggestions: 这是有效的解决方案吗?如果不能,请提出建议:

1) I can separate my main project in two: excel workbook project + dll project that will contain the logic. 1)我可以将我的主项目分为两个:excel工作簿项目+包含逻辑的dll项目。 The excel workbook project will reference the dll project. excel工作簿项目将引用dll项目。

2) Then I can create unit test project and reference the dll project. 2)然后,我可以创建单元测试项目并引用dll项目。 I can create my own excel files and create unit tests for my excel reading functions, that are in the dll project. 我可以创建自己的excel文件,并为dll项目中的excel读取功能创建单元测试。

It depends if you want to test the : 这取决于您是否要测试:

  1. contents of a workbook that your main project created. 您的主项目创建的工作簿的内容 OR 要么
  2. logic to generate the right workbook. 生成正确工作簿的逻辑。

for both the cases, you don't need to separate out the project into Main & Library. 对于这两种情况,您都不需要将项目分成Main&Library。 A single project with rightly separated logic should be good enough. 具有正确分隔的逻辑的单个项目应该足够好。 And by rightly separated logic, i mean interface based classes to do Excel stuff that don't interfere with testing etc. 通过正确分离的逻辑,我的意思是说基于接口的类可以做不干扰测试等的Excel工作。

now for #1, from the unit test: 现在针对#1,来自单元测试:

  1. call into the main project's method to create the expected workbook in the specified path. 调用主项目的方法以在指定路径中创建预期的工作簿。
  2. continue the unit test by reading the excel file from this path and verifying the contents. 通过从此路径读取excel文件并验证内容来继续进行单元测试。

for #2, from the unit test: 对于#2,来自单元测试:

  1. call into the core logical classes/method to create a test specific workbook and then verify its contents. 调用核心逻辑类/方法来创建特定于测试的工作簿,然后验证其内容。

ensure the workbook is deleted in either cases. 确保在两种情况下都删除工作簿。

the only difference between #1 & #2 is that you unit test one more level of code. #1和#2之间的唯一区别是您可以对一个以上级别的代码进行单元测试。

eg You Main logic will be: 例如,您的主要逻辑将是:

public static void MainLogic()
{
 string workbookFileName = ""; // read workbook path from config etc.
 // validate anything   

 CoreClass.CreateWorkbook(workbookFileName);

 // you could do additional stuff here.
}

and your sample unit test is: 您的样本单元测试为:

[Test]
public void MainLogicTest()
{
 string workbookFileName = ""; // read workbook path from test config etc. but same path.
 MainLogic();

 // retrieve the workbook at workbookFileName  and verify.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM