【问题标题】:WCF integration testing in TeamCityTeamCity 中的 WCF 集成测试
【发布时间】:2010-08-03 18:52:23
【问题描述】:

我希望我的集成测试在 TeamCity 中的每个提交上运行。让我的 WCF 服务自动运行以进行测试的好方法是什么? WCF 服务是经过测试的解决方案的一部分。

目前我在测试中自行托管服务。

host = new ServiceHost(typeof(...), url);

但在这种情况下,我无法应用我的实际 IIS 配置文件。我可以用代码复制设置,但我不想这样做。

什么是持续集成和 WCF 测试的最佳实践?

注意:我见过 WCFStorm 和 SoupUI,但它们是基于 GUI 的应用程序。

【问题讨论】:

    标签: wcf unit-testing continuous-integration automated-tests


    【解决方案1】:

    我在我的测试项目中创建了一个服务宿主类,它在测试项目的 AssemblyInitialize 上自行托管我希望调用的服务。

     [TestClass]
        internal class ServiceHost
        {
            private static ServiceHost<Service1> m_Host = null;
    
            /// <summary>
            /// Setups the specified context.
            /// </summary>
            /// <param name="context">The context.</param>
            [AssemblyInitialize]
            public static void Setup(TestContext context)
            {
                //comment to run against local consolehost
                m_Host = new ServiceHost<Service1>();
                m_Host.Open();
            }
    
            /// <summary>
            /// Tears down.
            /// </summary>
            [AssemblyCleanup]
            public static void TearDown()
            {
                if (m_Host != null)
                {
                    m_Host.Close();
                }
            }
        }
    

    在测试中,我使用 ChannelFactory 来调用服务。然后我关闭 AssemblyCleanup 上的服务。

    try
                {
                    ChannelFactory<IService> factory = new ChannelFactory<IService>("User");
                    IServiceproxy = factory.CreateChannel();
                    try
                    {
                        m_IsAuthenticated = proxy.Method("");
                        // Make sure to close the proxy
                        (proxy as IClientChannel).Close();
                        Assert.IsTrue(m_IsAuthenticated);
                    }
                    catch
                    {
                        if (proxy != null)
                        {
                            // If the proxy cannot close normally or an exception occurred, abort the proxy call
                            (proxy as IClientChannel).Abort();
                        }
                        throw;
                    }
                }
                catch (Exception ex)
                {
                    Assert.Fail(ex.Message);
                }
    

    为测试项目提供其自己的带有相关设置的 App.config 文件,以便在托管时与测试环境相关。这为您提供了一个自动化的黑盒测试。我还使用 Mocks 来隔离我希望测试的服务部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2014-02-02
      • 2012-04-29
      • 2013-12-20
      • 2012-05-19
      • 2013-12-07
      相关资源
      最近更新 更多