TeamCity 中的 WCF 集成测试

发布于 2024-09-12 12:13:08 字数 308 浏览 3 评论 0原文

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

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

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

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

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

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

I want my integration tests to run on each commit in TeamCity. What is the good way to get my WCF service running automatically for the tests?
WCF service is part of the solution that is tested.

Currently I self-host the service in the tests.

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

But I can't apply my actual IIS configuration file in this case. I could duplicate the settings with the code, but I would rather not.

What are the best practices for continuous integration and WCF testing?

Note: I've seen the WCFStorm and SoupUI but they are GUI based apps.

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

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

发布评论

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

评论(1

鹿港小镇 2024-09-19 12:13:09

我在测试项目中创建一个服务主机类,该类在测试项目的 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 来隔离我想要测试的服务部分。

I create a service host class in my tests project that on AssemblyInitialize of the test project, self hosts the service I wish to invoke.

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

In the test I use ChannelFactory to invoke the service. I then close the service on 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);
            }

Give the test project its own App.config file with relevant settings so that when it hosts it is relevant to the test environment. This gives you an automated black box test. I also use Mocks to isolate the part of the service I wish to test.

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