0

我希望能够右键单击一个方法,输入一些参数值,然后测试它(或类似的东西)。

我已经看过 Visual-Studio 中的内置 MVC 单元测试,但放弃了学习它.. 只是为了测试一个简单的小方法似乎很麻烦。我确信在创建严肃的项目时使用它是有充分理由的。

但是有没有一个快速而肮脏的选择?


一个很酷的解决方案是,在visual-studio中的一个窗口,您(使用智能感知!)可以编写如下内容:

HelloWorld obj = new HelloWorld();
obj.Print();

单击运行,然后立即向您显示结果。就像即时窗口一样,但这仅在调试时有效:(

4

4 回答 4

3

您可以尝试TestDriven.NETCodeRush。两者都允许您通过简单的右键单击和测试(甚至调试)来测试方法。

我更喜欢前者(TestDriven.NET),因为它允许您通过测试运行器执行任意方法(不仅仅是测试)。

两者都与 NUnit 兼容,并且可能与其他几个框架(如 MbUnit)兼容。

于 2012-07-30T14:22:00.550 回答
1

ReSharper 的单元测试窗口比 Visual Studio 中的“正常”单元测试支持要好得多。看看: http: //www.jetbrains.com/resharper/features/unit_testing.html

于 2012-07-30T14:36:35.790 回答
0

SnippetCompiler 或 LinqPad 是我所知道的最佳选择。这篇 Stack Overflow 帖子讨论了它们。

于 2012-07-30T14:22:15.753 回答
-1

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:

  1. Create a custom attribute.

  2. Create a test page, and find all methods in your assembly with an attribute QTest.

  3. 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.

于 2012-07-30T19:34:37.163 回答