Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Too much testing on simple view?

Hi I am putting together my website and whilst it is very simple I decided to test as many aspects of it as I could. Using the ethos of only writing useful tests that could account for situations I could imagine happening (renaming of a script or css file etc)

I am using Steve Sanderson's MVC Integration testing framework and my tests are below.

My question is two fold, is this level of testing "too much" and if not, what other scenarios (either developer related such as renaming or anything else) can you think of.

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;
    }
}
like image 993
Daniel Elliott Avatar asked Apr 17 '12 07:04

Daniel Elliott


People also ask

How many unit tests is too many?

But you should NEVER test more than one method in one unit test. It reduce the amount of work and error in fixing your test in case your API changes.

How do you reduce test efforts?

We identified five different areas that exploit different ways to reduce testing effort: approaches that predict defect-prone parts or defect content, automation, test input reduction approaches, quality assurance techniques applied before testing, and test strategy approaches.

When should testing be stopped How much testing is enough?

Testing can be stopped when: Requirements: 100% Requirements coverage is achieved.

What is simplicity in testing?

Simplicity: Clearly, the simpler the software, the fewer errors it will have, and the easier it will be to test.


1 Answers

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.

like image 65
Edu Avatar answered Nov 10 '22 04:11

Edu