Posted on 13/11/2014 15:36:12
We (Dynamicweb) are using some unit tests, but not as many as we could and should. We tend to call them all unit tests even though some of them are actually integration tests and other types of tests. The important thing is that they can be used to automatically test some small part (unit) of Dynamicweb.
As you note, Dynamicweb depends heavily on a database and a http context and this makes it hard to create tests that can actually test the stuff we want to test without running into all sorts of errors from missing dependencies.
When we run our automated (unit) tests we make some assumptions, e.g. that
* the request url is http://head.local.dynamicweb.dk
* the database is called AutomatedTests
and then we can fake contexts that make it possible to run the tests.
We have a number of helper classes (see Tests.cs) that can help us run tests using various contexts under the assumptions above.
For example, we can then test the way Dynamicweb rewrites urls using the OutputReplacer (this test requires a PageView which in turn requires a WebContext):
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Dynamicweb.Tests.Frontend
{
[TestClass]
public class OutputReplacerTest : Dynamicweb.Tests.Tests
{
[TestMethod]
public void TestParseURLs()
{
using (WebContext)
{
using (PageView)
{
Assert.AreEqual(Dynamicweb.Frontend.SearchEngineFriendlyURLs.RedirectType, Dynamicweb.Frontend.SearchEngineFriendlyURLs.Type.Path);
var page = Dynamicweb.Frontend.PageView.Current().Page;
page.set_Value("PagePathUrl", "/page-with-id-1");
var tests = new string[][] {
new string[] {
@"href=Default.aspx?Id={{pageId}}",
@"href={{pageUrl}}"
},
…
};
var replacer = new Dynamicweb.Frontend.OutputReplacer();
foreach (var test in tests)
{
var input = test[0].Replace("{{pageId}}", page.ID.ToString());
var expected = test[1].Replace("{{pageUrl}}", page.get_Value("PagePathUrl") as string);
var actual = replacer.ParseURLs(input, Dynamicweb.Frontend.PageView.Current());
Assert.AreEqual(expected, actual);
}
}
}
}
}
}
We are aware that we can – and have to – improve in this area, both to test the Dynamicweb core and to make it easier for you to test your Dynamicweb extensions.
Any comments and suggestions on how to make testing (in) Dynamicweb easier and better are more than welcome.
Best regards,
Mikkel