与NUnit的一个ASP.NET网站 [英] NUnit with an ASP.NET web site

查看:161
本文介绍了与NUnit的一个ASP.NET网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在努力工作,以提升我们构建服务器,从没有构建服务器具有一个去!

I'm currently trying to upgrade our build server at work, going from having no build server to having one!

我使用JetBrains公司' TeamCity的(有使用的为几年ReSharper的我相信他们的东西),并打算使用的 NUnit的的MSBuild

I'm using JetBrains' TeamCity (having used ReSharper for a couple of years I trust their stuff), and intend to use NUnit and MSBuild.

不过,我想出了一个问题:看来,它是不可能的,以测试ASP.NET网站与NUnit的。我曾以为这将有可能将其配置构建之后测试APP_ code,但似乎做的测试很好的唯一途径是通过网站转换为一个Web应用程序(我的老板不喜欢的想法)。

However, I've come up with an issue: it appears that it is not possible to test an ASP.NET web site with NUnit. I had assumed it would be possible to configure it to test App_Code after a build, however it seems that the only way to do tests nicely is through converting the web site to a web application (which my boss does not like the idea of).

我怎么能去呢?

请记住,测试需要能够从TeamCity的自动解雇了。

Please bear in mind that the testing needs to be able to be fired automatically from TeamCity.

推荐答案


  • 如果您想抽烟测试你的网站,或者喋喋不休一些端点 - 请参阅下面的code

  • If you want to smoke test your site, or bang on some endpoints - see the code below.

    如果,在另一方面,你要测试的不可测(可恨的)ASP.NET网站组件(相对于一个Web应用程序),你来了,因为他们在法国,SOL说

    If, on the other hand, you want to test the untestable (detestable) ASP.NET website assembly (as opposed to a web app), you are, as they say in France, S.O.L.

    该组件一个随机命名的动态编译的程序集深的框架ASP.NET临时文件的肠子,使得测试几乎是不可能的。

    The assembly is a randomly named dynamically compiled assembly deep in the bowels of the framework temporary ASP.NET files, making testing close to impossible.

    您真的需要考虑几个选项:

    You really do need to consider a couple of options:


    1. 将需要在一个单独的组装测试的逻辑。

    2. 变化,提供了一个可测试组件的Web应用程序项目。

    对不起,我不认为你会发现你在找什么,但我可能是错的。让我们来看看。

    Sorry, I don't think that you will find what you are looking for, but I could be wrong. Let's see.

    好运

    下载Visual Studio中的网站和applicatgion 2008年的样本。

    Download Visual Studio 2008 sample with site and applicatgion.

    我写了 WebHost.WebServer.dll 的包装,它是该的CI 的。我用它所有的时间。

    I wrote a wrapper for WebHost.WebServer.dll which is the core of the development server that works quite well in CI. I use it all the time.

    下面是一个缩小版本,包括使用示例。

    Here is a scaled down version including a usage example.

    test.cs中

    using System.Net;
    using NUnit.Framework;
    
    namespace Salient.Excerpts
    {
        [TestFixture]
        public class WebHostServerFixture : WebHostServer
        {
            [TestFixtureSetUp]
            public void TestFixtureSetUp()
            {
                // debug/bin/testproject/solution/siteundertest - make sense?
                StartServer(@"..\..\..\..\TestSite");
    
                // is the equivalent of
                // StartServer(@"..\..\..\..\TestSite",
                // GetAvailablePort(8000, 10000, IPAddress.Loopback, true), "/", "localhost");
            }
            [TestFixtureTearDown]
            public void TestFixtureTearDown()
            {
                StopServer();
            }
    
            [Test]
            public void Test()
            {
                // while a reference to the web app under test is not necessary,
                // if you do add a reference to this test project you may F5 debug your tests.
                // if you debug this test you will break in Default.aspx.cs
                string html = new WebClient().DownloadString(NormalizeUri("Default.aspx"));
            }
        }
    }
    

    WebHostServer.cs

    // Project: Salient
    // http://salient.codeplex.com
    // Date: April 16 2010
    
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Net.NetworkInformation;
    using System.Threading;
    using Microsoft.VisualStudio.WebHost;
    
    namespace Salient.Excerpts
    {
        /// <summary>
        /// A general purpose Microsoft.VisualStudio.WebHost.Server test fixture.
        /// WebHost.Server is the core of the Visual Studio Development Server (WebDev.WebServer).
        ///
        /// This server is run in-process and may be used in F5 debugging.
        /// </summary>
        /// <remarks>
        /// If you are adding this source code to a new project, You will need to
        /// manually add a reference to WebDev.WebHost.dll to your project. It cannot
        /// be added from within Visual Studio.
        ///
        /// Please see the Readme.txt accompanying this code for details.
        /// </remarks>
        /// NOTE: code from various namespaces/classes in the Salient project have been merged into this
        /// single class for this post in the interest of brevity
        public class WebHostServer
        {
            private Server _server;
    
            public string ApplicationPath { get; private set; }
    
            public string HostName { get; private set; }
    
            public int Port { get; private set; }
    
            public string VirtualPath { get; private set; }
    
            public string RootUrl
            {
                get { return string.Format(CultureInfo.InvariantCulture, "http://{0}:{1}{2}", HostName, Port, VirtualPath); }
            }
    
            /// <summary>
            /// Combine the RootUrl of the running web application with the relative url specified.
            /// </summary>
            public virtual Uri NormalizeUri(string relativeUrl)
            {
                return new Uri(RootUrl + relativeUrl);
            }
    
            /// <summary>
            /// Will start "localhost" on first available port in the range 8000-10000 with vpath "/"
            /// </summary>
            /// <param name="applicationPath"></param>
            public void StartServer(string applicationPath)
            {
                StartServer(applicationPath, GetAvailablePort(8000, 10000, IPAddress.Loopback, true), "/", "localhost");
            }
    
            /// <summary>
            /// </summary>
            /// <param name="applicationPath">Physical path to application.</param>
            /// <param name="port">Port to listen on.</param>
            /// <param name="virtualPath">Optional. defaults to "/"</param>
            /// <param name="hostName">Optional. Is used to construct RootUrl. Defaults to "localhost"</param>
            public void StartServer(string applicationPath, int port, string virtualPath, string hostName)
            {
                if (_server != null)
                {
                    throw new InvalidOperationException("Server already started");
                }
    
                // WebHost.Server will not run on any other IP
                IPAddress ipAddress = IPAddress.Loopback;
    
                if(!IsPortAvailable(ipAddress, port))
                {
                    throw new Exception(string.Format("Port {0} is in use.", port));
                }
    
                applicationPath = Path.GetFullPath(applicationPath);
    
                virtualPath = String.Format("/{0}/", (virtualPath ?? string.Empty).Trim('/')).Replace("//", "/");
    
                _server = new Server(port, virtualPath, applicationPath, false, false);
                _server.Start();
    
                ApplicationPath = applicationPath;
                Port = port;
                VirtualPath = virtualPath;
                HostName = string.IsNullOrEmpty(hostName) ? "localhost" : hostName;
            }
    
            /// <summary>
            /// Stops the server.
            /// </summary>
            public void StopServer()
            {
                if (_server != null)
                {
                    _server.Stop();
                    _server = null;
                    // allow some time to release the port
                    Thread.Sleep(100);
                }
            }
    
            public void Dispose()
            {
                StopServer();
            }
    
    
           /// <summary>
            /// Gently polls specified IP:Port to determine if it is available.
            /// </summary>
            /// <param name="ipAddress"></param>
            /// <param name="port"></param>
            public static bool IsPortAvailable(IPAddress ipAddress, int port)
            {
                bool portAvailable = false;
    
                for (int i = 0; i < 5; i++)
                {
                    portAvailable = GetAvailablePort(port, port, ipAddress, true) == port;
                    if (portAvailable)
                    {
                        break;
                    }
                    // be a little patient and wait for the port if necessary,
                    // the previous occupant may have just vacated
                    Thread.Sleep(100);
                }
                return portAvailable;
            }
    
            /// <summary>
            /// Returns first available port on the specified IP address.
            /// The port scan excludes ports that are open on ANY loopback adapter.
            ///
            /// If the address upon which a port is requested is an 'ANY' address all
            /// ports that are open on ANY IP are excluded.
            /// </summary>
            /// <param name="rangeStart"></param>
            /// <param name="rangeEnd"></param>
            /// <param name="ip">The IP address upon which to search for available port.</param>
            /// <param name="includeIdlePorts">If true includes ports in TIME_WAIT state in results.
            /// TIME_WAIT state is typically cool down period for recently released ports.</param>
            /// <returns></returns>
            public static int GetAvailablePort(int rangeStart, int rangeEnd, IPAddress ip, bool includeIdlePorts)
            {
                IPGlobalProperties ipProps = IPGlobalProperties.GetIPGlobalProperties();
    
                // if the ip we want a port on is an 'any' or loopback port we need to exclude all ports that are active on any IP
                Func<IPAddress, bool> isIpAnyOrLoopBack = i => IPAddress.Any.Equals(i) ||
                                                               IPAddress.IPv6Any.Equals(i) ||
                                                               IPAddress.Loopback.Equals(i) ||
                                                               IPAddress.IPv6Loopback.
                                                                   Equals(i);
                // get all active ports on specified IP.
                List<ushort> excludedPorts = new List<ushort>();
    
                // if a port is open on an 'any' or 'loopback' interface then include it in the excludedPorts
                excludedPorts.AddRange(from n in ipProps.GetActiveTcpConnections()
                                       where
                                           n.LocalEndPoint.Port >= rangeStart &&
                                           n.LocalEndPoint.Port <= rangeEnd && (
                                           isIpAnyOrLoopBack(ip) || n.LocalEndPoint.Address.Equals(ip) ||
                                            isIpAnyOrLoopBack(n.LocalEndPoint.Address)) &&
                                            (!includeIdlePorts || n.State != TcpState.TimeWait)
                                       select (ushort)n.LocalEndPoint.Port);
    
                excludedPorts.AddRange(from n in ipProps.GetActiveTcpListeners()
                                       where n.Port >= rangeStart && n.Port <= rangeEnd && (
                                       isIpAnyOrLoopBack(ip) || n.Address.Equals(ip) || isIpAnyOrLoopBack(n.Address))
                                       select (ushort)n.Port);
    
                excludedPorts.AddRange(from n in ipProps.GetActiveUdpListeners()
                                       where n.Port >= rangeStart && n.Port <= rangeEnd && (
                                       isIpAnyOrLoopBack(ip) || n.Address.Equals(ip) || isIpAnyOrLoopBack(n.Address))
                                       select (ushort)n.Port);
    
                excludedPorts.Sort();
    
                for (int port = rangeStart; port <= rangeEnd; port++)
                {
                    if (!excludedPorts.Contains((ushort)port))
                    {
                        return port;
                    }
                }
    
                return 0;
            }
        }
    }
    

    注: Microsoft.VisualStudio.WebHost 命名空间包含在文件中 WebDev.WebHost.dll 。该文件是在GAC中,但它是不可能的引用在Visual Studio中添加该组件。

    NOTE: The Microsoft.VisualStudio.WebHost namespace is contained in the file WebDev.WebHost.dll. This file is in the GAC, but it is not possible to add a reference to this assembly from within Visual Studio.

    要添加你需要打开你的.csproj文件中的一个文本编辑器和手动添加引用的引用。

    To add a reference you will need to open your .csproj file in a text editor and add the reference manually.

    查找的ItemGroup 包含项目引用并添加以下元素:

    Look for the ItemGroup that contains the project references and add the following element:

    <Reference Include="WebDev.WebHost, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=x86">
      <Private>False</Private>
    </Reference>
    

    参考:<一href=\"http://www.$c$cproject.com/KB/aspnet/test-with-vs-devserver-2.aspx\">http://www.$c$cproject.com/KB/aspnet/test-with-vs-devserver-2.aspx

    这篇关于与NUnit的一个ASP.NET网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆