title: C# NUnit Test Behaves Differently than when Run Directly
body: <p>So I have some code (I did not write) that works when run or debugged as a unit test, but when the <em>exact same</em> code is run normally with the program, it behaves differently. I'm using Visual Studio Ultimate 2013 with NUnit installed running on Windows 7 64-bit. To run as a unit test, I use the Visual Studio test explorer. I'm not that familiar with unit testing, so I'm really hoping that I just overlooked something simple.</p>
  
  <p>Here's what the code looks like:</p>
  
  <pre><code>//The code in question
  public static void TryThis()
  {
      USBHIDDRIVER.USBInterface usbIa = new USBInterface("0");
      String[] list = usbIa.getDeviceList(); //empty when calling from Main()
      Assert.IsNotEmpty(list);
  }
  
  //A unit test that calls this code
  [Test]
  public static void CallingFunction()
  {
      TryThis(); //list gets populated and assert passes
  }
  
  //Main function that calls this code when program running - doesn't 
  public static void Main()
  {
      TryThis(); //list remains empty and assert fails
  }
  </code></pre>
  
  <p>The code calls a DLL from System32 (setupapi.dll to be specific), and this function call is where the difference in behavior occurs, so I cannot step through to see exactly what causes the different behavior. There are no [SetUp] or [TearDown] attributes or anything like that in the entire solution. Only [TestFixture] and [Test].</p>
  
  <p>My question is: <strong>What could possibly be different when calling TryThis() from a unit test as opposed to Main()?</strong></p>
  
  <p>What I've tried:</p>
  
  <ul>
  <li>Running the test, as well as Main(), in both debug and release mode.</li>
  <li>Running the test, as well as Main(), in both x86 and x64 architecture.</li>
  <li>Specifying the full absolute path of the DLL to ensure that the same DLL is being loaded in both cases.</li>
  <li>Copying the code directly into Main() and CallingFunction() to avoid any function calls.</li>
  <li>Putting the [Test] attribute above Main() itself and running as a test (this works when run as a test, not when run normally).</li>
  <li>Debugging and stepping through the code one instruction at a time: between both cases (debugged as test and debugged normally), all variables (autos and locals) are identical right up to the point of the DLL function call.</li>
  <li>Compiling my code as its own DLL library and calling it from another application (list is still empty) to rule out issues with Visual Studio.</li>
  <li>Running Visual Studio as an administrator to rule out permissions issues.</li>
  <li>A bunch of other combinations of functions calling functions, defining functions as tests, etc. In all cases, the code only works when run as a test.</li>
  <li>Trying it on a different computer (same OS).</li>
  </ul>
  
  <p>I would greatly appreciate any feedback or ideas, no matter how crazy they are. Ultimately I just need to be be able to use this code in an application. Thanks!</p>