How to perform transactional unit testing in nunit

Source: Internet
Author: User

From: http://www.cnblogs.com/ywqu/archive/2009/11/07/1598163.html

 

Unit Test requirements: the unit test method does not actually change the database, that is, the unit test does not rely on data in the database. So how can we solve the problem of not changing the data in the database after executing the unit test method?

There are two general solutions:

 

1. Create a unit test database and separate the development database from the unit test database. The unit test method is completely based on the unit test database.

This method has the following advantages: developers will not change the data in the unit test database during development, and will not affect the execution of the unit test method at any time.

Disadvantages: synchronization of Unit Test Databases and development databases, especially for iterative development projects, databases are constantly following up or changing as needed, and synchronization becomes a bottleneck for the normal operation of unit test.

 

2. Use transactions to roll back the execution of unit test methods.

Advantages of this method: the disadvantage of method 1 is solved, and the database structure is not synchronized.

Disadvantage: When performing the crud (Create/read/update/delete) operation, you need to perform some data insertion operations in the unit test method to ensure the independence of the Unit Test and the development database, this increases the unit test workload.

 

In actual projects, you can select a solution that suits your needs. If the database structure has been determined during the development phase of the project and will not change in the future, we recommend that you use the first solution, otherwise, the second solution is recommended. Currently, our project adopts the second plan.

I. nunit transactional Unit Testing

 

What if we use the nunit framework to ensure data will be rolled? Here we use COM + transactions.

That is, system. incluiseservices;

The details are as follows:

/// <Summary>

/// Unit test base class. All unit test classes must inherit this class.

/// </Summary>

[Testfixture]

[Transaction (transactionoption. Required)]

Public class databasefixture: servicedcomponent

{

Public databasefixture ()

{

//

// Todo: Add constructor logic here

//

}

[Teardown]

Public void transactionteardown ()

{

If (contextutil. isintransaction)

{

Contextutil. setabort ();

}

}

All unit test methods must inherit from this class. For example:

Public class addresssqldaotest: databasefixture

In this way, after the unit test method is executed, the transactionteardown () method in the databasefixture class will continue to be executed. As a result, the previous data operations will be rolled out, and the unit test method will not affect the development database. Similarly, the development database will not affect the execution of the unit test method, thus ensuring the independence of the Unit Test and the database data.

 

Ii. How to perform crud Unit Testing

1. Test addition method: Determine whether the returned primary key is greater than 0. If the primary key is greater than 0, the unit test method is successful. Otherwise, the system fails.

2. Test query method: first, insert data in the execution unit test class (not the insert method in the test class, but the insert method written in the unit test class, which must be separated ), then execute the query method.

3. Test the update method: first, insert the data method in the execution unit test class, and then execute the update method.

4. Test the deletion method: first, insert the data method in the execution unit test class, and then execute the deletion method.

 

Iii. unit test naming rules

To facilitate the maintenance of unit test methods in the future, we recommend that you name the unit test class and unit test method as follows.

Unit Test Class Name: tested class name + Test

Unit Test Method Name: tested method name + Test

Iv. Summary

Now, you can use nunit to perform transactional unit tests. I believe you have learned how to make unit tests independent of database data to perform unit tests more efficiently, it does not affect development.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.