NUnit - cleanup after test failure

后端 未结 11 951
盖世英雄少女心
盖世英雄少女心 2020-12-24 05:09

We have some NUnit tests that access the database. When one of them fails it can leave database in inconsistent state - which is not an issue, since we rebuild database for

11条回答
  •  一整个雨季
    2020-12-24 05:55

    One option not mentioned so far is to wrap the test up in a TransactionScope object, so it doesn't matter what happens as the test never commits anything to the DB.

    Here's some details on the technique. You can probably find more if you do a search on unit testing and transactionscope (though you are really doing integration testing if you hit a DB). I've used it successfully in the past.

    This approach is simple, does not require any cleanup and ensures that tests are isolated.

    Edit- I've just noticed Ray Hayes answer is also similar to mine.

提交回复
热议问题