0

I'm writing a few UI automation test scripts using C#, NUnit and Selenium WebDriver. I would like to know if it's possible to restore the database at the end of each test (like via the TearDown method) and if so how I could achieve this.

I tried following this and this but they didn't seem to work. I'm guessing this is because I'm not executing any database related operation from the test script itself?

Community
  • 1
  • 1
Rusty Wizard
  • 531
  • 6
  • 20
  • Are you using transaction ? yiou probably should open a transction at the start of the test then rollback at the end. Other option would be to mock the database writing query to a log and replying with typical expected result from the DB. I don't think that there is a proper way to do it via web interface unless using a dummy DB – dvhh Mar 10 '15 at 06:47
  • Both the linked examples used SqlTransaction and so I used it too. – Rusty Wizard Mar 10 '15 at 08:21

2 Answers2

0

You can use TransactionScope

using (TransactionScope scope = new TransactionScope())
{
      //your code goes here
}

Keep in mind, that auto_increment counter values will not roll back.

nuclear sweet
  • 1,079
  • 10
  • 27
0

I would recommend creating Fakes at your DAL layer so that you are always using the same data for each run and you won't have to worry about having a live connection to a database. I wrote a bit about the general idea here http://blog.dmbcllc.com/automated-web-application-functional-testing/ if you need more detail.

Dave Bush
  • 2,382
  • 15
  • 12
  • I don't really have a DAL per se. This is UI automation after all and I don't have access to the application's source code. – Rusty Wizard Mar 11 '15 at 05:00