简体   繁体   中英

Visual Studio 2010 Unit Testing

I am trying to find out in Visual Studio 2010 Unit Testing how to keep a transaction of the data I have either added, updated, or deleted during my tests so on my TestCleanup I can rollback their values.

What search terms should I be using to find more about this?

Cheers

Paul

Why do you need to rollback changes? Are your unit tests updating live data? If unit tests are written properly, you shouldn't have to clean up after what your tests changed because the data they're changing should be isolated to your test.

Edit:

It sounds like you've set up a data set for testing and want to make sure that data set is restored back to its original state. I prefer the practice of setting up the test data as part of the test, but I understand that can get difficult for complex tests.

If this in an ADO.NET data source, you could start a transaction then roll back that transaction at the end of your test. For example:

using (var transaction = db.BeginTransaction())
{
    // Do tests here
}
// The transaction is rolled back when disposed

Edit 2:

A third option, if you don't have transaction support, is to have a backup of your test data in a place where it won't get modified, then at the end of the test, restore that backup.

For unit testing you should probably try using mocks instead of accessing a test database. Unit tests should generally be completly self-contained (and not rely on external sources eg, databases).

If you are actually testing calls to the database then this is probably an integration test in which case you could:

  1. Create test data in setup
  2. Run code
  3. Assert test passes
  4. Remove test data inserted in step one

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM