开发者

Ensuring Service Connectivity

I am planning to set up a very simple NUnit test to just test if the WCF Service is up and running or not. So, I have

http://abc-efg/xyz.svc

Now, I need to write an unit tes开发者_StackOverflow中文版t to connect to this URI and if it is functional just log the success and if it fails log the failure and exceptions/errors in a file. There is NO necessity for separate hosting etc.

What would be the ideal methodology and methods to invoke and achieve this objective?


This is what we use in our tests which connect to the WCF server. We don't explicitly test if the server is up, but obviously if it isn't then we get an error:

[Test]
public void TestServerIsUp()
{
    var factory = new ChannelFactory<IMyServiceInterface> (configSectionName);
    factory.Open ();
    return factory.CreateChannel ();
}

if there is no endpoint listening at the endpoint specified in the configuration then you'll get an exception and a failing test.

You could use one of the other overloads of the ChannelFactory constructors to pass in a fixed binding and endpoint address rather than use config if you wanted.


Not sure if this is ideal, but if I understand your question, you're really looking for an integration test to make sure that a certain URI is available. You're not really looking to unit test the implementation of the service -- you want to make a request to the URI and inspect the response.

Here's the NUnit TestFixture that I'd set up to run this. Please note that this was put together pretty quickly and can definitely be improved upon....

I used the WebRequest object to make the request and get the response back. When the request is made, it's wrapped in a try...catch because if the request returns anything but a 200-type response, it will throw a WebException. So I catch the exception and get the WebResponse object from the exception's Response property. I set my StatusCode variable at that point and continue evaluating the returned value.

Hopefully this helps. If I'm misunderstanding your question, please let me know and I'll update accordingly. Good luck!

TEST CODE:

[TestFixture]
public class WebRequestTests : AssertionHelper
{
    [TestCase("http://www.cnn.com", 200)]
    [TestCase("http://www.foobar.com", 403)]
    [TestCase("http://www.cnn.com/xyz.htm", 404)]
    public void when_i_request_a_url_i_should_get_the_appropriate_response_statuscode_returned(string url, int expectedStatusCode)
    {
        var webReq = (HttpWebRequest)WebRequest.Create(url);
        webReq.Method = "GET";
        HttpWebResponse webResp;
        try
        {
            webResp = (HttpWebResponse)webReq.GetResponse();

            //log a success in a file
        }
        catch (WebException wexc)
        {
            webResp = (HttpWebResponse)wexc.Response;

            //log the wexc.Status and other properties that you want in a file
        }

        HttpStatusCode statusCode = webResp.StatusCode;
        var answer = webResp.GetResponseStream();
        var result = string.Empty;

        if (answer != null)
        {
            using (var tempStream = new StreamReader(answer))
            {
                result = tempStream.ReadToEnd();
            }
        }

        Expect(result.Length, Is.GreaterThan(0), "result was empty");
        Expect(((int)statusCode), Is.EqualTo(expectedStatusCode), "status code not correct");
    }
}


You can use the unit testing capability within Visual Studio to do it. Here is an example

http://blog.gfader.com/2010/08/how-to-unit-test-wcf-service.html


WCF and Unit Testing example with Nunit

Here is also a similar question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜