I am trying to do the following for UI test automation:
[SetUp]
public void TestSetUp()
{
_scope = new TransactionScope();
}
[TearDown]
public void TearDown()
{
_scope.Dispose();
}
[Test]
public void SomeTest()
{
Utilities.SomeDeleteTransaction(companyCode);
}
I am trying to execute one Update query and in [Test] and do some stuff with UI and rollback that transaction in [TearDown] which runs after the test. I am not sure if I am doing it right. But, I am trying to (probably commit) that transaction so that I can see it's effect on UI and rollback same transaction so my DB stays the same. Can I accomplish that with TransactionScope or some other class?
Edit
This question is mostly to handle the database known state for selenium testing. Since, my database is brought down from production monthly, I want to be able to execute some insert/update/delete sql script to modify db before tests and then do some UI testing with Selenium and then rollback in Teardown (tests are written using NUnit) to make sure db does not have any influence on tests and stays same after tests.
Database snapshots!
/* Create a database snapshot */
USE master;
CREATE DATABASE Your_Database_Snapshot ON
(
NAME = Your_Database,
FILENAME = 'C:\Snapshots\Your_Database_Snapshot.ss'
)
AS SNAPSHOT OF Your_Database;
GO
USE master;
RESTORE DATABASE Your_Database from
DATABASE_SNAPSHOT = 'Your_Database_Snapshot';
GO
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.IO;
using System.Data.SqlClient;
[SetUp]
public void TestSetUp()
{
string sqlConnectionString = @"server=test.database.com;uid=your_db_username;pwd=your_db_password;database=Your_Database;";
string script = File.ReadAllText(@"~/create_db_snapshot.sql");
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
}
[TearDown]
public void TearDown()
{
string sqlConnectionString = @"server=test.database.com;uid=your_db_username;pwd=your_db_password;database=Your_Database;";
string script = File.ReadAllText(@"~/restore_db_from_snapshot.sql");
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
}
Snapshot documentation: https://msdn.microsoft.com/en-us/library/ms175158.aspx
Code credit for executing .sql file: https://stackoverflow.com/a/1728859/3038677
/* Kill all current connections to Your_Database */
use master;
DECLARE @kill varchar(8000) = '';
SELECT @kill = @kill + 'kill ' + CONVERT(varchar(5), spid) + ';'
FROM master..sysprocesses
WHERE dbid = db_id('Your_Database')
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments