My project needs to automatically test the WEB service in CruiseControl. The test must meet the following conditions:
* Not dependent on IIS
* Do not deploy the WebService and run the WEB service directly in the directory generated by its CC compilation.
If the above requirements are met, you can also test the webpage in VS and CC in the same way.
SCOTT's article NUnit Unit Testing of ASP was found online. NET Pages, Base Classes, Controls and other widgetry using Cassini (ASP. NET Web Matrix/Visual Studio Web Developer), it is really convenient to use Cassini.
I wrote a help class (see the end of the page) and used it like this (run Cassini in TestFixtureSetUp and return its service address to test): [TestFixtureSetUp]
Public void Init ()
{
_ Cassni = new CassiniHelper ();
_ Cassni. Start ();
// You can obtain the address of the WEB service that runs Cassini.
// Config. CurrentServiceEntryUrl = _ cassni. WebServerUrl;
}
[TestFixtureTearDown]
Public void FixtureTearDown ()
{
_ Cassni. Close ();
}
- CassiniHelper internal class CassiniHelper
{
Private int _ webServerPort = 26610;
Private Server _ webServer;
Private readonly string _ webServerVDir = "/ws ";
Private string _ workingPath;
Private string _ webServerUrl;
Private string _ abstractPathFromTestToWebService = @ "... \ WebService ";
Public CassiniHelper ()
{
SetWebServerUrl ();
}
Public int WebServerPort
{
Get {return _ webServerPort ;}
Set
{
_ WebServerPort = value;
SetWebServerUrl ();
}
}
Public string WebServerUrl
{
Get {return _ webServerUrl ;}
Set {_ webServerUrl = value ;}
}
/** // <Summary>
/// Relative path from the directory where the test case Assembly is located to the directory where the WEBServer is located
/// </Summary>
Public string AbstractPathFromTestToWebService
{
Get {return _ abstractPathFromTestToWebService ;}
Set {_ abstractPathFromTestToWebService = value ;}
}
/** // <Summary>
/// WEB Service Virtual Path
/// </Summary>
Public string WebServerVDir
{
Get {return _ webServerVDir ;}
}
Public void Start ()
{
If (! Directory. Exists (_ workingPath ))
Throw new DirectoryNotFoundException (_ workingPath );
_ WebServer = new Server (_ webServerPort, WebServerVDir, _ workingPath );
_ WebServer. Start ();
}
Public void Close ()
{
Try
{
If (_ webServer! = Null)
{
_ WebServer. Stop ();
_ WebServer = null;
}
}
Catch {}
}
Private void SetWebServerUrl ()
{
String currentPath = Path. GetDirectoryName (Assembly. GetExecutingAssembly (). Location );
DirectoryInfo dinfo = new DirectoryInfo (Path. Combine (currentPath, _ abstractPathFromTestToWebService ));
_ WorkingPath = dinfo. FullName;
// Copy Cassini. dll to the bin directory of the web service.
String cassini = "Cassini. dll ";
File. Copy (Path. Combine (currentPath, cassini), Path. Combine (Path. Combine (_ workingPath, "bin"), cassini ));
_ WebServerUrl = String. Format ("http: // localhost: {0} {1}", _ webServerPort, WebServerVDir );
}
}