Nunit unit test practices

Source: Internet
Author: User

This problem is often encountered in projects. The database field is modified and the logic is also changed due to the change in requirements.CodeAfter modification, run the interface to start testing. However, it is impossible to write all the logic at one time, or to spell out all the characters. Debugging a function, especially for B/S Systems, usually takes a lot of time. In addition, if there are changes, you have to repeat them again. If there are some associations, debugging and testing will be more complicated. In addition, after N modules are implemented in the project, another function is modified. To be honest, Tian knows whether other logics have produced bugs.

Therefore, it is necessary to introduce unit tests and use them properly to improve the code quality. It seems that the Code time has been extended, but in fact the debug time has been greatly shortened, and the marginal influence of the modification function on other methods is greatly reduced.

What if it is a good practice?

Generally, we use nunit. this is not difficult. You can find a lot of entry points in the garden or on the Internet.ArticleAnd I will not repeat it. Let's only talk about the precautions.

1. testing only requires testing of logical operations and testing of crud data. The collection and display of front-end UI data is not done by nunit. Our task is to ensure that the data transmitted to the UI is correct.

In the standard demo, I only tested BLL and Dal layers.

 

2. it is best to separate the test database from the database used in peacetime, because the test data does not solve the problem of automated testing well, such as using the app. config configures the database separately (the demo uses the same database)

3. Construct and clean test data as needed

Sometimes the data needs to be cleared, but sometimes it is not required. For classification testing, it is easier to retain some data. Therefore, try to use [test, category ("XX")] for classification.

4. test with the data module to improve efficiency and test results

The classification test makes it easier to see the problems of a module. Sometimes, the data for testing a method alone does not indicate any problems.

5. combined with testdriven.net, you get twice the result with half the effort. Combined with the add-in tool of Vs, the test becomes more convenient. nunit becomes a decoration. Basically, it is opened to see a green progress bar.

6. Test the void method. In my opinion, test whether an exception is thrown. For example, test the following add method:

 

Code
Public   Void Add (loginuser Model)
{
Try
{
Validateinputmodel (model );
Loginmanager LM =   New Loginmanager ();
Lm. Add (model );
}
Catch (Exception ex)
{
Throw Ex;
}

}

 

Then I can test it like this.

Code


[Testfixture]
Public   Class Testloinbll: basefixture
{
[Test, expectedexception ( Typeof (Exception)]
Public   Void Testaddofnousername ()
{
Loginbll lb = New Loginbll ();
Model. loginuser Model = New Loginuser ();
Model. ID =- 1 ;
Model. Username =   "" ;
Model. Password = " Dd " ;
LB. Add (model );
}

[Test, expectedexception ( Typeof (Exception)]
Public   Void Testaddofnopasswd ()
{
Loginbll lb =   New Loginbll ();
Model. loginuser Model =   New Loginuser ();
Model. ID =   - 1 ;
Model. Username =   " Dd " ;
Model. Password =   "" ;
LB. Add (model );
}

[Test]
Public   Void Testaddofblank ()
{
Loginbll lb =   New Loginbll ();
Model. loginuser Model =   New Loginuser ();
Model. ID =   - 1 ;
Model. Username =   " Dd " ;
Model. Password =   " Dd " ;
LB. Add (model );
}
}

 

 

 

I don't know how to upload attachments. The Demo project cannot be uploaded. To use the demo, you must install entlib2.0 or later. We recommend that you install nunit and testdriven.net to try it out.

Download connection http://files.cnblogs.com/emilchan/MyCzbb.rar

 

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.