user3111311
user3111311

Reputation: 8001

How to unit test an Excel Workbook project?

How to unit test an Excel Workbook project?

I use 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.

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. The excel workbook project will reference the dll project.

2) Then I can create unit test project and reference the dll project. I can create my own excel files and create unit tests for my excel reading functions, that are in the dll project.

Upvotes: 1

Views: 4159

Answers (1)

Raja Nadar
Raja Nadar

Reputation: 9499

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. 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.

now for #1, from the unit test:

  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.

for #2, from the unit test:

  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.

e.g. 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.
}

Upvotes: 2

Related Questions