Problems getting the SolidWorks app when running unit tests

‌ - Help!

You could see my question on Stack Overflow.

Here is my issue. When I get the currently running SolidWorks app in a class when Visual Studio is running in debug mode I get the app just fine. When I unit test I start the app using [TestInitialize] method. When that happens I CAN'T get the running Solidworks session in the class.

My code works gets the SolidWorks app when running in debug mode Visual Studio:

public SettingsManager(ref IModelDoc2 model)
{
if (solidWorksApp == null)
{
string progId = "SldWorks.Application";
solidWorksApp = System.Runtime.InteropServices.Marshal.GetActiveObject(progId) as ISldWorks;
}
if (solidWorksApp == null)
{
throw new Exception("Couldn't get a running SolidWorks session.");
}

When I run in a unit test and I start SolidWorks using TestInitialize:

[TestInitialize]
public void TestInitialize()
{
var progId = "SldWorks.Application";
var progType = System.Type.GetTypeFromProgID(progId);
app = System.Activator.CreateInstance(progType) as ISldWorks;
app.Visible = true;

DocumentSpecification documentSpecification;
documentSpecification = (DocumentSpecification)app.GetOpenDocSpec(fullFilePath);
documentSpecification.DocumentType = (int)swDocumentTypes_e.swDocPART;
var result = app.OpenDoc7(documentSpecification);
if (result == null)
{
throw new Exception("Couldn't load Solidworks test file.");
}
}

I then instantiate various classes that use my SettingsManager (first code above). When I do this SettingsManager cannot get the SolidWorks app that was started by the Unit Test in Visual Studio. I am guessing it is a permissions issue or an admin/non admin kind of thing.

By the way, I am running Visual Studio in admin mode while debugging. I am developing an AddIn using Artem's SwAddInEx frame work.

SolidworksApi/macros