Okay, so i tried out unit testing and gave it a good shot, and i hated it...
I needed to use the HttpContext and it was a pain in the ass to make that work when unit testing. So i wrote my own testing environment in MVC, it's quite simple:
Your put on a attribute on the method you want testet, you build you project, and you refresh a page h**p://localhost/test to see the output of that method.
[QTest]
public string test()
{
return HttpContext.Current.Server.MapPath(@"~\");
}
If anyone should be interested, here is the howto:
Create a custom attribute.
Create a test page, and find all methods in your assembly with an attribute QTest.
Print them out.
My code:
CustomAttribute.cs
public class QTestAttribute : Attribute
{
public QTestAttribute()
{
//Will do so you can define the method paremeter values later on. But for now a emty attribute is fine.
}
}
Your TestController.cs:
static IEnumerable<Type> GetTypesWithAttribute(Assembly assembly)
{
foreach (Type type in assembly.GetTypes())
{
yield return type;
}
}
static IEnumerable<MethodInfo> GetMethodsWithAttribute(Type theClass)
{
foreach (MethodInfo method in theClass.GetMethods())
{
if (method.GetCustomAttributes(typeof(QTestAttribute), true).Length > 0)
{
yield return method;
}
}
}
public ActionResult Index()
{
IEnumerable<Type> classes = GetTypesWithAttribute(Assembly.LoadFrom(HttpContext.Request.MapPath(@"~\bin\YourProjectName.dll")));
List<String> tests = new List<string>();
foreach (var singleClass in classes)
{
try
{
var a = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(singleClass.FullName);
foreach (MethodInfo method in GetMethodsWithAttribute(singleClass))
{
tests.Add(method.Invoke(a, null).ToString());
}
}
catch (Exception ex)
{
try
{
tests.Add(ex.InnerException.Message);
}
catch (Exception)
{
}
}
}
return Content(string.Join("<br>", tests));
}
It's pretty easy to customize, and make it work how you want to, and it's without limitations compared to nunit.