user1051434
user1051434

Reputation: 167

Setup Database before run ui coded tests on Visual Studio 2010

I'm automating UI tests to my Silverlight App and I'm using Visual Studio 2010 for it. Some tests required a setup to my Oracle Database.

Things i've done:

1 - A setup.sql file where I connect to my Database and perform actions on it. I had this file as an existing item to my project and I add as a Deployment on TestSettings.

Code:

CONNECT USERNAME@DATABASE,
CREATE TABLE TABLE_NAME,
EXIT

2 - A set.bat file where I call the setup.sql file. I had the path of this file on Setup and Cleanup tab on TestSetings.

Code:

sqlcmd -S MARIALISBOA -i setup.sql

3 - I wrote a TestInitilize method on my TestClass.

Code:

[TestInitialize()]
   public void Initialize()
   {
     System.Diagnostics.Process.Start("setup.bat");
   }

4 - I connected do my Database throw Visual Studio (Data -> Add New Data Source).

I run a test on Visual Studio but the class isn't created on my database.

Could anyone help me? I'm trying to solve this problem since Monday and I starting to lose my mind

Upvotes: 1

Views: 1617

Answers (1)

Kinexus
Kinexus

Reputation: 12904

While it does not solve your initial problem, a solution would be to use something similiar to this;

  1. Do not create the table within your tests. this should be created on install of the Test Environment
  2. Clear down the table for each test you want to do using a Helper Method within the test.

For example (Please note that this is SQL Server, use OLE DB connection or similiar);

   internal static object FireSqlStatement(string sqlStatement)
        {
            object result = null;
            using (var cn = new SqlConnection(ConfigurationManager.ConnectionStrings[connectionString].ConnectionString))
            {
                cn.Open();

                var cmd = new SqlCommand
                {

                    CommandText = sqlStatement,
                    CommandType = CommandType.Text,
                    Connection = cn
                };
                result = cmd.ExecuteScalar();
                cmd.Dispose();

                cn.Close();
            }
            return result;
        }

An example of how I use this within my test;

 [Test]
        public void GetUserTriggers()
        {
            //Insert Test Record
            Helper.FireSqlStatement("INSERT INTO [Users].[Triggers] (Id) VALUES (1)");

            var request = new GetTriggersRequest()
            {
                TriggerType = TriggerType.UserTrigger
            };

            var response = ServiceInvoker.Invoke<IReports, 
                      GetTriggersRequest, GetTriggersResponse>(
                      "Reports",
                      request,
                      (proxy, req) => proxy.GetTriggers(req));

            foreach (var t in response.Triggers)
            {
                Console.WriteLine(t.Id.ToString());
            }

            Assert.NotNull(response);
            Assert.NotNull(response.Triggers);
            Assert.Greater(response.Triggers.Count, 0);
        }

In your case, you could call;

 Helper.FireSqlStatement("TRUNCATE TABLE tableName");

Any good?

Upvotes: 1

Related Questions