用客户端为wcf服务编写回归测试

时间:2014-02-21 11:47:11

标签: c# wcf nunit integration-testing regression-testing

我想编写一个自动回归测试,我的服务已启动,我可以断言我的客户可以从服务中检索一些东西。

    Private Blah proxy;

    [SetUp]
    public void SetUp()
    {
         proxy= new Blah();
    }

    [Test]
    public void GetStuff()
    {
        var result = proxy.GetStuff();
        Assert.NotNull(result);
    }

这不起作用,因为我的服务没有运行。在测试之前我如何开始我的服务?

1 个答案:

答案 0 :(得分:3)

对于WCF服务的集成/验收测试,我建议您使用Self Hosted WCF服务。您可以在此处找到示例:

在夹具设置上创建自托管服务,并在夹具拆卸时关闭它:

EndpointAddress address = new EndpointAddress("http://localhost:8080/service1");
ServiceHost host;        
IService1 service;

[TestFixtureSetUp]
public void FixtureSetUp()
{            
    var binding = new BasicHttpBinding();
    host = new ServiceHost(typeof(Service1), address.Uri);
    host.AddServiceEndpoint(typeof(IService1), binding, address.Uri);
    host.Open();
}

[TestFixtureTearDown]
public void FixtureTearDown()
{
    if (host == null)
        return;

    if (host.State == CommunicationState.Opened)
        host.Close();
    else if (host.State == CommunicationState.Faulted)
        host.Abort();
}

托管服务后,您可以获得服务代理:

var binding = new BasicHttpBinding();
ChannelFactory<IService1> factory = 
     new ChannelFactory<IService1>(binding, address);
service = factory.CreateChannel();

您的测试将如下所示:

[Test]
public void ShouldReturnSomeStuff()
{
    var result = service.GetStuff();
    Assert.NotNull(result);
}