Use NUnit to test WEB Services in CruiseControl

Source: Internet
Author: User
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 );
    }
    }

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.