Unit test from command line fails

I'm currently struggling with some unit tests that work great with visual studio, but crash in Teamcity

I tracked the problem to mstests.exe

Let's say I do the following steps:

  • Create a new test project
  • Add a new Test class with the following test

    [TestMethod] public void TestCanCreateSqLiteConnection() { // Create the DbProviderFactory var factory = DbProviderFactories.GetFactory("System.Data.SQLite"); // Create the DbConnection. var connection = factory.CreateConnection(); // Assign connection string connection.ConnectionString = "Data Source=database.sqlite"; // check the result Assert.IsTrue(connection.GetType().Name.Equals("SQLiteConnection")); } 
  • Add the app.config file and add the following:

     <system.data> <DbProviderFactories> <remove invariant="System.Data.SQLite" /> <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" /> </DbProviderFactories> </system.data> 
  • Install "System.Data.SQLite (x86 / x64)" via nuget

  • Run the test from Visual Studio (2010). It should work fine:

Now I want to run the same test through mstest.exe so that I:

  • Open a command prompt in Visual Studio 2010

  • Go to bin \ debug folder

  • Run

     mstest.exe /testcontainer:TestProject1.dll /detail:errormessage 
  • The test ultimately fails with

     System.DllNotFoundException: Unable to load DLL 'SQLite.Interop.DLL': The specified module could not be found. (Exception from HRESULT:0x8007007E) 
  • Now, if I extend the call to mstest.exe using the test settings, the test works fine.

     mstest.exe /testcontainer:TestProject1.dll /detail:errormessage testsettings:..\..\..\Local.testsettings 

Local.testsettings does not contain anything special, even if I create a new testettings file and use it, the test passes.

  <?xml version="1.0" encoding="UTF-8"?> <TestSettings id="fc837936-41d1-4987-8526-34f9336569f5" name="TestSettings1" enableDefaultDataCollectors="false" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010"> <Description>default test run</Description> <Deployment enabled="false"/> </TestSettings> 

So, the main question: why does this affect my test run and how can I run my test commands from the command line without specifying a * .testsettings file.

+4
source share
4 answers

I again encountered a similar error message. If I remember correctly, the essence of the problem was as follows: (this may not be 100% relevant for the OP, but it may be useful for those who fall into this line).

The problem was that my module tested all the broken ones in Release mode, except for complaints about the availability (or absence) of SQLite.Interop.dll. I realized that when building in debug mode, the bin \ Debug folder had two subfolders (x64 and x86) with a copy of SQLite.Interop.dll, but these files / folders did not exist in release mode.

To solve this problem, I created the x64 and x86 folders in my project and added the corresponding version of SQLite.Interop.dll too, setting the Copy to ouput parameter Copy to ouput Copy if newer . (I originally used Copy Always, but it seems that the MS verification mechanism does not turn off when the test run is completed, which may lock the file. Since the dll should not be changed regularly, the Copy if newer was the appropriate approach).

This allowed my unit tests to go in release mode, but, unfortunately (as in the case of OP), they did not work when launched from the command line. I’m still trying to understand that one of them is, I think, because MSTest is 32-bit, and SQLite uses its own code, which is (possibly) 64-bit, but the finer details needed to eliminate it elude me at present time.

+1
source

Two years later, and it still hurts that SQLite worked in unit tests.

Yesterday, I included the current SQLite nuget package in the unit test project with <Deployment enabled="true"/> and was unable to access sqlite using the dbproviderfactories method.

I have included the interlude SQLite directories with

 <Deployment> <DeploymentItem filename="packages\System.Data.SQLite.Core.1.0.98.1\build\net40\" /> </Deployment> 

But that was not enough. Access to a supplier using

 DbProviderFactories.GetFactory("System.Data.SQLite"); 

still threw an error Failed to find or load the registered .Net Data Provider error , if I did not make this call var factory = new System.Data.SQLite.SQLiteFactory(); . After that, I was able to get SQLite with DbProviderFactories.

I included this in the ClassInitialize method, so it only executes once.

+1
source

I ran into a similar problem when tests run fine in Visual Studio 2013, but if they run directly with MSTest, many of them will fail. SQL Lite is not used!

In the end, I simply added the default .testsettings file to the MSTest call, and now the results are consistent.

0
source

You need to use DeploymentItem to ensure that the file will be copied to the deployment directory when testing through the command line. I created a base class for all my test classes, which are dependent on the SQLite database.

 [TestClass] [DeploymentItem("Resources\\empty-db.sqlite", "Resources")] [DeploymentItem("x64\\SQLite.Interop.dll", "x64")] [DeploymentItem("x86\\SQLite.Interop.dll", "x86")] public class SQLiteTest { [TestInitialize()] public void ClearDatabase() { File.Copy("Resources\\empty-db.sqlite", "test-db.sqlite", true); } } 
0
source

Source: https://habr.com/ru/post/1500285/


All Articles