Unit Testing VSTO projects

1.8k Views Asked by At

In most unit test tutorials I follow it has me create a second unit test project and then add a solution/project reference from the test project to the other project.

I have a VSTO addin, and when I go to add the reference I don't see anything listed under Solution > Project.

Why is this? How else can I add a reference to the addin project to test it?

2

There are 2 best solutions below

7
On BEST ANSWER

If this is your first time at unit testing, then VSTO might be a steep learning curve. As suggested by @Sam Holder, you might want to put some of your logic into a separate assembly to encourage you to separate your logic from your interactions with the office infrastructure.

That said, whilst you can't add the project using the normal approach of adding a reference to the project, you can add a reference to the output from that project using the Browse option:

  • Right click on the test project and select "Add Reference".
  • In the Box the appears, click on the "Browse" button at the bottom.
  • Navigate to the bin\debug folder of the VSTO project and select the projects dll.
  • Click on OK.

Obviously, you will have had to have built the addin for the above to work.

You're going to face some other challenges if you start trying to create objects that rely on the office infrastructure. So, at a minimum you might need to add references to Microsoft.Office.Tools, Microsoft.Office.Tools.Common into your test project.

0
On

I don't know why adding a reference doesn't work, but you might be able to solve it by adding a 3rd project. Place all your logic in this new project. Add a reference to this new project in your tests, and then add a reference to you new project in your VSTO addin project, where you can call the logic.