Previously, debuglzq wrote a blog article about how to use nunit to write. Net unit tests. HoweverNunti has a fatal weakness in unit testing: it cannot be debugged. Because our tests in this province are alsoCodeWe are also not sure that our code is correct.This blog article describes how to use testdriven. Net to fix this critical weakness of nunit by writing a unit test by connecting to the database and inserting a field in vs2010.
The architecture of the example project is as follows:
The following is the source code of several main classes. The person class code is as follows:
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Namespace Vs2010 create an sqlserver database test { Public Class Person { Private Int ID; Private String Username; Private String Password; Public Int Id { Get { Return ID ;} Set {Id = Value ;}} Public String Username { Get { Return Username ;} Set {Username = Value ;}} Public String Password { Get { Return Password ;} Set {Password = Value ;}}}}
The source code of the database connection class is as follows:
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Using System. Data. sqlclient; Using System. configuration; Using System. Windows. forms; Namespace Vs2010 create an sqlserver database test { Public Class Connection { Public Static Sqlconnection getconnection (){ String Connectionstring = @" Data Source =. \ sqlexpress; attachdbfilename = E: \ Visual Studio 2010 \ vs2010 create an sqlserver database test \ vs2010 create an sqlserver database test \ database1.mdf; Integrated Security = true; user instance = true " ; Sqlconnection Conn = New Sqlconnection (connectionstring ); Try {Conn. open ();} Catch { Return Null ;} Return Conn ;}}}
The corresponding test class connectiontest is as follows:
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Using System. Data. sqlclient; Using Nunit. Framework; // Namespace Vs2010 create an sqlserver database test {[testfixture] Public Class Connectiontest {[test] Public Void Getconnectiontest () {sqlconnection Conn = Connection. getconnection (); Assert. isnotnull (conn );}}}
Database Help class: dbhelper is as follows:
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Using System. Data. sqlclient; Namespace Vs2010 create an sqlserver database test { Public Class Dbhelper { Public Static Void Insert (person ){ String Sqlstring = " Insert into userinfo (username, password) values (@ username, @ password) " ; Sqlconnection Conn = Connection. getconnection (); sqlcommand cmd = New Sqlcommand (sqlstring, Conn); cmd. Parameters. Add ( " @ Username " , System. Data. sqldbtype. varchar); cmd. Parameters. Add ( " @ Password " , System. Data. sqldbtype. varchar); cmd. Parameters [ " @ Username " ]. Value = Person. Username; cmd. Parameters [ " @ Password " ]. Value = Person. Password; Try {Cmd. executenonquery ();} Catch { Throw New Exception ( " Unexpected exception! " );} Finally {Conn. Close ();}} /// Public Static Person getpersonbyid ( Int ID ){ String Sqlstring = " Select * From userinfo where id = @ ID " ; Sqlconnection Conn = Connection. getconnection (); sqlcommand cmd =New Sqlcommand (sqlstring, Conn); cmd. Parameters. Add ( " @ ID " , System. Data. sqldbtype. INT); cmd. Parameters [ " @ ID " ]. Value = ID; sqldatareader Reader = Cmd. executereader (); person = Null ; If (Reader. Read () {person = New Person () {ID = ID, username = Reader [ " Username " ]. Tostring (), password = Reader [ " Password " ]. Tostring () };} reader. Close (); Conn. Close (); Return Person ;}}}
The results of the target table are as follows:
Dbhelpertest for dbhelper testing is as follows:
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Using System. Data. sqlclient; Using Nunit. Framework; Namespace Vs2010 create an sqlserver database test {[testfixture] Public Class Dbhelpertest {[test] Public Void Inserttest () {person = New Person () {Username = " Debuglzq_a " , Password = " Debuglzq_a " }; Dbhelper. insert (person); person personresult = Dbhelper. getpersonbyid (getmaxid (); Assert. areequal (person. username, personresult. username );} Private Int Getmaxid (){ String Sqlstring =" Select max (ID) as maxid from userinfo " ; Sqlconnection Conn = Connection. getconnection (); sqlcommand cmd = New Sqlcommand (sqlstring, Conn); sqldatareader Reader = Cmd. executereader (); Int Maxid = 0 ; If (Reader. Read () {maxid = Convert. toint32 (Reader [ " Maxid " ]);} Reader. Close (); Conn. Close (); Return Maxid ;}}}
Code compilation is complete.
First, let's take a look at the test results in nunit:
After reading the blog post before debuglzq, we should be able to proceed to this step. Nunit is powerful, but as mentioned earlier, nunit cannot be used for debugging, which is intolerable. It is okay when the code is simple, it is difficult to ensure that our tests are correct. Therefore,It is necessary to debug our tests..
Next we will introduce how to solve this problem. Today's main character is testdriven. net. You can download it from the following website.
I will not talk about this introduction in English. Highlights:
1. "0 friction" is integrated into vs in the form of plug-ins.
2. It supports all versions of
After the download and installation are complete, right-click the test case, the entire project, and even the entire solution (in fact, any location) and choose runtest.
You can view the unit test results in the project output box.
If we need to debug, OK to do this, test with Debugger:
In this way, we can easily view debugging information.
It also has a powerful function: to check which code has been tested and which are not. If so, we need to do this.
In this case, we can view the corresponding results in ncoverexplorer:
The red code indicates that the test has not been performed, and the light green Code indicates that the test has been performed. In addition, we can obtain other details, such as the Code test rate.
We can also generate performance reports for our tests.
Thank you for your time.
If you think thisArticleIt helps you,Please click "Green Channel"-"follow debuglzq" below to share your progress ~WishProgramGood health and good work ~