Difference between revisions of "NUnit support tools"
(→One click restore database) |
(→One click restore database) |
||
Line 77: | Line 77: | ||
The utility includes 2 files: a .sql and a .bat. The .bat file is the entry point, its main purpose is launching SQL statements inside .sql file. | The utility includes 2 files: a .sql and a .bat. The .bat file is the entry point, its main purpose is launching SQL statements inside .sql file. | ||
− | + | ===.sql file=== | |
*Create a .sql file, for example: restoredatabase.sql | *Create a .sql file, for example: restoredatabase.sql | ||
*Copy following lines as content: | *Copy following lines as content: | ||
Line 106: | Line 106: | ||
In which: | In which: | ||
− | + | #'''UnitTest57''': name of the restored database | |
− | + | #'''D:\databases\UnitTestBase_FOR_57.bak''': path to the backup file | |
− | + | ===.bat file=== | |
− | <source> | + | This file is rather simple. It is for user to make a double-click on to start restoring. Its main purpose is to running all SQL statements inside .sql file |
+ | <source lang="DOS"> | ||
sqlcmd -S localhost -d master -i "D:\databases\restoredatabase.sql" | sqlcmd -S localhost -d master -i "D:\databases\restoredatabase.sql" | ||
pause | pause | ||
</source> | </source> | ||
+ | In which: | ||
+ | #'''D:\databases\restoredatabase.sql''': path to the .sql file created above | ||
== One click attach NUnit == | == One click attach NUnit == |
Revision as of 05:49, 22 December 2008
Contents
Introduction
NUnit is one of the most popular tool for unit testing in .NET. And the most important thing is that it is used inside CatGlobe and now we are approaching Test-Driven-Development, this means that developers/testers must involve in using NUnit more and more.
In this page, I will introduce some small supporting tools that can add some value working with NUnit.
NCover and NCoverExplorer
NUnit does not provide a mean for checking code coverage, we have to use some third-party tools. The easiest and free tool is NCover. However, NCover just supports for exporting the code coverage result into xml file. Luckily we also have another free tool for exploring the coverage result - NCoverExplorer
Download the 2 tools from:
- NCover: http://ncover.org
- NCoverExplorer: http://www.kiwidude.com/dotnet/DownloadPage.html
NCover
NCoverExplorer
NUnit HTML report generator
There are time when we need to store the result after one unit test running for later reference. NUnit can export the run result into an XML file, however it is not user friendly. In this section, I will introduce a free tools for generate HTML report from such XML file.
The tool is NUnit2Report, its homepage is http://nunit2report.sourceforge.net/
The tool is in fact a NAnt task, so we must have NAnt installed. Homepage of NAnt: http://nant.sourceforge.net/
Installation:
- Download the latest version of NAnt
- Unzip it to a folder name %NANTHOME%
- Make sure %NANTHOME%\bin folder is in your PATH environment variable
- Download the later version of NUnit2Report
- Unzip the file to a folder name %NUNITREPORTHOME%
- Copy all file if %NUNITREPORTHOME%\bin to %NANTHOME%\bin
Usage:
- Export test running result to xml file. This can be done in 2 ways:
- With NUnit-gui, after running the test, go to menu Tools -> Save Results as XML
- With Nunit-console, by default a file TestResult.xml will be generated in working directory. Otherwise, you can set the output file by putting /output parameter
- Assume we have a result file as %RESULT%\TestResult.xml, create a text file in folder %RESULT% with extension .build
- Copy following content to the file:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<project name="NUnitReport" default="nunitreport" basedir=".">
<tstamp property="build.date" pattern="dd-MM-yyyy" verbose="true"/>
<sysinfo/>
<target name="nunitreport">
<nunit2report out="DomainTester.html">
<fileset>
<include name="TestResult.xml" />
</fileset>
</nunit2report>
</target>
</project>
In which:
- DomainTester.html: HTML output file name
- TestResult.xml: NUnit test result file name
- Now, it's ready for generating the report. Open command line console (cmd.exe), move to folder %RESULT%, type nant (make sure NAnt's bin folder is in your PATH environment or you would get an invalid path error message)
- A DomainTester.html is generated in folder %RESULT%. Open it and you will get something like this:
Note that, the result is alphabetically sorted by TextFixture's name, this is not the same as in NUnit-Gui which groups TestFixtures by its namespace. If this causes problem identifying the correct method on NUnit-Gui by referencing to the HTML report (you might want to re-run a failed test method after fixing something) then the following tool can give some help.
Usage:
- Copy \\catproc\Share\CatGlobe Teams\R & D\Personal\Chinh\RefineNUnitTestResult.exe to the same folder as TestResult.xml
- Run the executable. Note: the programe requires no argument and will update the TestResult.xml.
- Now, run NAnt as above and you will get some thing as below:
One click restore database
When running with nunit-console.exe, we don't have feature for selecting backup for restoring but having to prepare the database by ourselves. Most of the time, the name of database and backup file are the same. This section provides a small utility for restoring database in a click instead of lots of clicks/selections using SQL Management Studio.
Moreover, if using the SQL Management Studio, the restored database must have no connection to it otherwise the restoring process would fail. This utility, disconnect all connections to the restored database as a preparing step, so making sure that restoring always success.
The utility includes 2 files: a .sql and a .bat. The .bat file is the entry point, its main purpose is launching SQL statements inside .sql file.
.sql file
- Create a .sql file, for example: restoredatabase.sql
- Copy following lines as content:
declare conns cursor
for
select spid
from master.dbo.sysprocesses
where dbid <> 0 and db_name(dbid)='UnitTest57'
declare @spid as int
OPEN conns
FETCH NEXT FROM conns INTO @spid
WHILE (@@FETCH_STATUS <> -1)
BEGIN
declare @sql as varchar(max)
set @sql = 'KILL ' + cast(@spid as varchar(50))
execute(@sql)
FETCH NEXT FROM conns INTO @spid
END
CLOSE conns
DEALLOCATE conns
GO
RESTORE DATABASE UnitTest57 FROM DISK = N'D:\databases\UnitTestBase_FOR_57.bak' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 10
In which:
- UnitTest57: name of the restored database
- D:\databases\UnitTestBase_FOR_57.bak: path to the backup file
.bat file
This file is rather simple. It is for user to make a double-click on to start restoring. Its main purpose is to running all SQL statements inside .sql file
sqlcmd -S localhost -d master -i "D:\databases\restoredatabase.sql"
pause
In which:
- D:\databases\restoredatabase.sql: path to the .sql file created above