确保服务连通性
我计划设置一个非常简单 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我们在连接到 WCF 服务器的测试中使用的。我们没有显式测试服务器是否已启动,但显然如果没有启动,我们会收到错误:
如果配置中指定的端点没有端点侦听,那么您将收到异常和失败的测试。
如果需要,您可以使用 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:
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.
不确定这是否理想,但如果我理解您的问题,您确实在寻找集成测试以确保某个 URI 可用。您并不是真正想要对服务的实现进行单元测试——您想要向 URI 发出请求并检查响应。
这是我设置来运行它的 NUnit
TestFixture
。请注意,这是很快就完成的,并且绝对可以改进......我使用 WebRequest 对象来发出请求并获取响应。当发出请求时,它会被包装在
try...catch
中,因为如果请求返回除 200 类型响应之外的任何内容,它将抛出 WebException。因此,我捕获异常并从异常的Response
属性中获取WebResponse
对象。我在此时设置了StatusCode
变量并继续评估返回的值。希望这有帮助。如果我误解了您的问题,请告诉我,我会相应更新。祝你好运!
测试代码:
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 theWebResponse
object from the exception'sResponse
property. I set myStatusCode
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:
您可以使用 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
WCF 和 Nunit 单元测试示例
这里也是一个类似的问题。
WCF and Unit Testing example with Nunit
Here is also a similar question.