Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 99227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:20:50+00:00 2026-05-11T00:20:50+00:00

I want to write unit tests with NUnit that hit the database. I’d like

  • 0

I want to write unit tests with NUnit that hit the database. I’d like to have the database in a consistent state for each test. I thought transactions would allow me to ‘undo’ each test so I searched around and found several articles from 2004-05 on the topic:

  • http://weblogs.asp.net/rosherove/archive/2004/07/12/180189.aspx
  • http://weblogs.asp.net/rosherove/archive/2004/10/05/238201.aspx
  • http://davidhayden.com/blog/dave/archive/2004/07/12/365.aspx
  • http://haacked.com/archive/2005/12/28/11377.aspx

These seem to resolve around implementing a custom attribute for NUnit which builds in the ability to rollback DB operations after each test executes.

That’s great but…

  1. Does this functionality exists somewhere in NUnit natively?
  2. Has this technique been improved upon in the last 4 years?
  3. Is this still the best way to test database-related code?

Edit: it’s not that I want to test my DAL specifically, it’s more that I want to test pieces of my code that interact with the database. For these tests to be ‘no-touch’ and repeatable, it’d be awesome if I could reset the database after each one.

Further, I want to ease this into an existing project that has no testing place at the moment. For that reason, I can’t practically script up a database and data from scratch for each test.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:20:51+00:00Added an answer on May 11, 2026 at 12:20 am

    NUnit now has a [Rollback] attribute, but I prefer to do it a different way. I use the TransactionScope class. There are a couple of ways to use it.

    [Test] public void YourTest()  {     using (TransactionScope scope = new TransactionScope())     {         // your test code here     } } 

    Since you didn’t tell the TransactionScope to commit it will rollback automatically. It works even if an assertion fails or some other exception is thrown.

    The other way is to use the [SetUp] to create the TransactionScope and [TearDown] to call Dispose on it. It cuts out some code duplication, but accomplishes the same thing.

    [TestFixture] public class YourFixture {     private TransactionScope scope;      [SetUp]     public void SetUp()     {         scope = new TransactionScope();     }      [TearDown]     public void TearDown()     {         scope.Dispose();     }       [Test]     public void YourTest()      {         // your test code here     } } 

    This is as safe as the using statement in an individual test because NUnit will guarantee that TearDown is called.

    Having said all that I do think that tests that hit the database are not really unit tests. I still write them, but I think of them as integration tests. I still see them as providing value. One place I use them often is in testing LINQ to SQL code. I don’t use the designer. I hand write the DTO’s and attributes. I’ve been known to get it wrong. The integration tests help catch my mistake.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 83k
  • Answers 83k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What you are looking for is called Bandwidth throttling And… May 11, 2026 at 4:53 pm
  • Editorial Team
    Editorial Team added an answer Creating a custom result is the cleanest way to handle… May 11, 2026 at 4:53 pm
  • Editorial Team
    Editorial Team added an answer You can use the Emacs Desktop library: You can save… May 11, 2026 at 4:53 pm

Related Questions

I want to write unit tests with NUnit that hit the database. I'd like
I have a class that processes a 2 xml files and produces a text
We are developing applications for use within AutoCAD. Basically we create a Class Library
I'm not a newbie since I've been programming on and off since 1983, but
I am in a project where we are starting refactoring some massive code base.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.