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;
}
}
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.
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.
Testing can be stopped when: Requirements: 100% Requirements coverage is achieved.
Simplicity: Clearly, the simpler the software, the fewer errors it will have, and the easier it will be to test.
Testing is always context dependent and the risks that you see should guide the amount of testing.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With