user1434702
user1434702

Reputation: 829

Unit test with sqlite in vs2012 c#

Here is my code:

public class Program
{
   static void Main(string[] args)
   {
      exec1();
      Console.WriteLine("completed");
      Console.Read();
   }

   public static void exec1()
   {
      using (IDBConnection conn = new SQLiteConnection("Data Source=test.db"))
      {
         conn.Open();
         IDbCommand command = conn.CreateCommand();
         command.CommandText = "select * from user";
         IDataReader reader = command.ExecuteReader();
         while (reader.Read())
         {
            Console.WriteLine(reader[1]);
         }
      }
   }
}

Unit test:

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
       Program.exec1();
    }
}

It will run well with debug, and when i use test class to run it i get an error.

System.Data.SQLite.SQLiteException: SQL logic error or missing database no such table: user

Upvotes: 2

Views: 2309

Answers (2)

keyboardP
keyboardP

Reputation: 69372

Try using the DeploymentItemAttribute to ensure that the database file itself is deployed alongside the test.

[TestMethod]
[DeploymentItem("test.db")] 
public void TestMethod1()
{
   Program.exec1();
}

(Side note, please don't use images of your code as it makes it impossible to copy/paste when answering).

Upvotes: 1

Piotr Perak
Piotr Perak

Reputation: 11118

What unit testing framework are you using?

Probably test.db isn't copied to where you code is run under your testing framework.

Edit: Looking at attributes it's probably MSTest. I never used it but MSTest has something called deployment items. You have to define external files (here test.db) that are part of your test.

Upvotes: 0

Related Questions