确保服务连通性

发布于 2024-11-15 11:26:00 字数 242 浏览 8 评论 0原文

我计划设置一个非常简单 NUnit 测试来测试WCF 服务是否已启动并运行。 现在

http://abc-efg/xyz.svc

,我需要编写一个单元测试来连接到此 URI,如果它可以正常工作,则只需记录成功,如果失败,则将失败和异常/错误记录在文件中。没有必要单独托管等。

调用和实现这一目标的理想方法和方法是什么?

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 test 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只等公子 2024-11-22 11:26:01

这是我们在连接到 WCF 服务器的测试中使用的。我们没有显式测试服务器是否已启动,但显然如果没有启动,我们会收到错误:

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

如果配置中指定的端点没有端点侦听,那么您将收到异常和失败的测试。

如果需要,您可以使用 ChannelFactory 构造函数的其他重载之一来传递固定绑定和端点地址,而不是使用配置。

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.

贪了杯 2024-11-22 11:26:01

不确定这是否理想,但如果我理解您的问题,您确实在寻找集成测试以确保某个 URI 可用。您并不是真正想要对服务的实现进行单元测试——您想要向 URI 发出请求并检查响应。

这是我设置来运行它的 NUnit TestFixture。请注意,这是很快就完成的,并且绝对可以改进......

我使用 WebRequest 对象来发出请求并获取响应。当发出请求时,它会被包装在 try...catch 中,因为如果请求返回除 200 类型响应之外的任何内容,它将抛出 WebException。因此,我捕获异常并从异常的 Response 属性中获取 WebResponse 对象。我在此时设置了 StatusCode 变量并继续评估返回的值。

希望这有帮助。如果我误解了您的问题,请告诉我,我会相应更新。祝你好运!

测试代码:

[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");
    }
}

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");
    }
}
疑心病 2024-11-22 11:26:01

您可以使用 Visual Studio 中的单元测试功能来完成此操作。这是一个示例

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

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

橘亓 2024-11-22 11:26:01

WCF 和 Nunit 单元测试示例

这里也是一个类似的问题。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文