7

嗨,我正在整理我的网站,虽然它非常简单,但我决定尽可能多地测试它。使用只编写有用的测试的精神,这些测试可以解释我可以想象发生的情况(重命名脚本或 css 文件等)

我正在使用 Steve Sanderson 的 MVC 集成测试框架,我的测试如下。

我的问题是双重的,这个级别的测试是否“太多”,如果不是,您还能想到哪些其他场景(与开发人员相关,例如重命名或其他任何内容)。

using System.Web;
using System.Web.Mvc;
using MvcIntegrationTestFramework.Hosting;
using NUnit.Framework;
using website.Properties;

namespace website.tests
{
    [TestFixture]
    public class HomeControllerIndexTests
    {
        [TestFixtureSetUp]
        public void Setup()
        {
            appHost = AppHost.Simulate("Website");
        }

        [Test]
        public void HomeControllerIndexReturnsTheIndexView()
        {
            appHost.Start(session =>
            {
                var result = session.Get("/Home/Index");
                Assert.AreEqual("Index", ((ViewResult)result.ActionExecutedContext.Result).ViewName);
            });
        }

        [Test]
        public void HomeControllerIndexReturnsCorrectRouteData()
        {
            appHost.Start(session =>
            {
                var result = session.Get("/Home/Index");
                Assert.AreEqual("Home", result.ActionExecutedContext.RouteData.Values["controller"]);
            });
        }

        [Test]
        public void HomeControllerIndexReturnsViewResult()
        {
            appHost.Start(session =>
            {
                var result = session.Get("/Home/Index");
                Assert.IsInstanceOf(typeof(ViewResult), result.ActionExecutedContext.Result);
            });
        }

        [Test]
        public void HomeControllerIndexReturnsNoError()
        {
            appHost.Start(session =>
            {
                var result = session.Get("/Home/Index");
                Assert.IsNull(result.ResultExecutedContext.Exception);
            });
        }

        [Test]
        public void HomeControllerIndexReturnsViewWithSiteCssFile()
        {
            appHost.Start(session =>
            {
                var result = session.Get("/Home/Index");
                Assert.IsTrue(result.ResponseText.Contains("/Content/Site.css"));
            });
        }

        [Test]
        public void HomeControllerIndexReturnsViewWithCorrectTitle()
        {
            appHost.Start(session =>
            {
                var result = session.Get("/Home/Index");
                Assert.IsTrue(result.ResponseText.ToLowerInvariant().Contains("<title>{ me: danielelliott.info(); }</title>"));
            });
        }

        [Test]
        public void HomeControllerIndexReturnsViewContainingBanner()
        {
            appHost.Start(session =>
            {
                var result = session.Get("/Home/Index");
                var expected = HttpUtility.HtmlEncode(Resources.SiteName);
                Assert.IsTrue(result.ResponseText.Contains(expected));
            });
        }

        [Test]
        public void HomeControllerIndexViewIncludesBioParagraph()
        {
            appHost.Start(session =>
            {
                var result = session.Get("/Home/Index");
                var expected = HttpUtility.HtmlEncode(Resources.Bio.ToLowerInvariant());
                Assert.IsTrue(result.ResponseText.ToLowerInvariant().Contains(expected));
            });
        }

        [Test]
        public void HomeControllerIndexViewIncludesServicesParagraph()
        {
            appHost.Start(session =>
            {
                var result = session.Get("/Home/Index");
                var expected = HttpUtility.HtmlEncode(Resources.Services.ToLowerInvariant());
                Assert.IsTrue(result.ResponseText.ToLowerInvariant().Contains(expected));
            });
        }

        [Test]
        public void HomeControllerIndexViewIncludesHistoryParagraph()
        {
            appHost.Start(session =>
            {
                var result = session.Get("/Home/Index");
                var expected = HttpUtility.HtmlEncode(Resources.History.ToLowerInvariant());
                Assert.IsTrue(result.ResponseText.ToLowerInvariant().Contains(expected));
            });
        }

        private AppHost appHost;
    }
}
4

2 回答 2

1

Testing is always context dependent and the risks that you see should guide the amount of testing.

  • How critical it would be if some part is not working after some change?
  • Do you believe that these parts could break when something is changed?
  • How big task it would be to maintain these tests if the structure of the page would change?
  • Do you believe these parts will change often? Does it pay off to automate them?
  • How long will running the tests take when the amount grows? Are you ready to wait that time often to see that latest changes have not broken anything?

If the page is not changing very often, this amount seems like quite much. You can also think if it would be enough to test just a sample of things. For example, it seems that you are including multiple parts to the page. If they are coming from same location and are included with the same mechanism, it seems unlikely that including one would fail if others are there.

On the other hand, it is always easier to reduce the amount. You can start with this when learning and then see if you need to change the approach later.

于 2012-04-17T08:03:39.960 回答
1

当我看到您的测试时,我的反应是它们主要验证实现细节。我建议你专注于行为。测试最终用户与站点的交互。验证信息是否存在,而不是其呈现方式。

于 2012-04-17T09:26:23.320 回答