简体   繁体   English

Visual C#2010 Express中的单元测试?

[英]Unit testing in Visual C# 2010 Express?

Visual C#2010 Express是否具有单元测试功能?

As has been stated, the Express versions do not have any built-in, and do not allow add-ins for, this functionality, but you can use an external tool, eg NUnit . 如上所述,Express版本没有任何内置功能,也不允许使用此功能的加载项,但您可以使用外部工具,例如NUnit

You can also set up a command to run from the 'Tools->External Tools' menu option from within Visual Studio Express and run your test runner from that if you wish. 您还可以在Visual Studio Express中设置从“工具 - >外部工具”菜单选项运行的命令,并根据需要运行测试运行器。

Here is a link that shows how to do it with VS C# 2008 Express, (scroll down to the end) but I think it should hold true for 2010 as well. 是一个链接,显示如何使用VS C#2008 Express,(向下滚动到最后),但我认为它也适用于2010年。

Here is a new working link. 这是一个新的工作链接。

Nothing built in, but you can always use nUnit with it. 什么都没有内置,但你总是可以使用nUnit。

MSTest comes bundled with the Pro edition and above. MSTest与专业版及以上版本捆绑在一起。

In 2010 it's possible using an external application, however debugging unit tests becomes difficult. 在2010年,可以使用外部应用程序,但调试单元测试变得困难。 If you want debugging using NUnit is probably the best route (but not the only option, see ExpressUnit ). 如果你想使用NUnit调试可能是最好的路线(但不是唯一的选择,请参阅ExpressUnit )。 See this answer on another SO thread. 在另一个SO线程上看到这个答案。 It links to a blog that mentions running the test project as a console application and calling the nunit library dlls directly to launch the tests: 它链接到一个博客 ,该博客提到将测试项目作为控制台应用程序运行并直接调用nunit库dll来启动测试:

using System;

namespace RunTests
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            var args = new string[] { Assembly.GetExecutingAssembly().Location, "/run" };
            NUnit.Gui.AppEntry.Main(args);
        }
    }
}

Visual Studio Express editions have the limitation that plugins/addins are expressely disallowed. Visual Studio Express版本具有限制,即插件/插件明显不允许使用。 They do not ship with a built-in testing solution, and you cannot add-in one. 它们没有附带内置测试解决方案,您无法添加一个。

Your best/only option it to use a standalone test runner, such as nUnit, mspec, etc... and run it externally from VSE. 您最好/唯一的选择是使用独立的测试运行器,例如nUnit,mspec等......并从VSE外部运行它。

This is now included in Visual Studio 2013 Express: http://msdn.microsoft.com/en-us/library/dd264975.aspx 现在包含在Visual Studio 2013 Express中: http//msdn.microsoft.com/en-us/library/dd264975.aspx

If Test Explorer is not visible, choose Test on the Visual Studio menu, choose Windows , and then choose Test Explorer . 如果看不到Test Explorer ,请在Visual Studio菜单上选择Test ,选择Windows ,然后选择Test Explorer

在此输入图像描述

As an update, I am currently using Visual Studio Express for Desktop , the VS suite has been completely remodelled since 2010 and more accurately reflects the "big brother". 作为更新,我目前正在使用Visual Studio Express for Desktop ,VS套件自2010年以来已经完全改造,更准确地反映了“大哥”。

Unit tests are now available as a built-in feature and works the same way (I haven't tested all functionality) as Visual Studio non-Express. 单元测试现在作为内置功能提供,并且以与Visual Studio非Express相同的方式(我没有测试过所有功能)。

You could always set up an additional class with a Main() method in the project and select it as a startup object in your project, and debug it from there. 您始终可以在项目中使用Main()方法设置一个附加类,并将其选择为项目中的启动对象,然后从那里进行调试。 It might not be suitable in situation where more complex tasks is done, since you can't take advantage of the more testing-specific features but it might be useful in some simpler projects. 它可能不适用于执行更复杂任务的情况,因为您无法利用更多特定于测试的功能,但在某些更简单的项目中可能会有用。 If your project is a class library, consider convert it to an console application and then switch it back when you finished testing. 如果您的项目是类库,请考虑将其转换为控制台应用程序,然后在完成测试后将其切换回来。

(Note : I know this post is old, but this might help someone ) (注意:我知道这篇文章很老了,但这可能对某人有帮助)

As Andy posted above, you can use NUnit . 正如Andy上面发布的那样,你可以使用NUnit
But the settings in the link posted by Andy do not work anymore in VS C# 2010. 但是Andy发布的链接中的设置在VS C#2010中不再起作用。
Here are the settings I use in the External Tools window : 以下是我在“外部工具”窗口中使用的设置:

Command : C:\\Program Files (x86)\\NUnit 2.6.2\\bin\\nunit-x86.exe 命令: C:\\ Program Files(x86)\\ NUnit 2.6.2 \\ bin \\ nunit-x86.exe
( there is also a nunit.exe in the bin directory ) bin目录中还有一个nunit.exe

Arguments : $(ProjectDir)$(ProjectFileName) 参数: $(ProjectDir)$(ProjectFileName)
Initial directory : $(ProjectDir)bin/Debug/$(TargetName)$(TargetExt) 初始目录: $(ProjectDir)bin / Debug / $(TargetName)$(TargetExt)

Have a look at NHarness at codeplex, a very simple library that allows you to run NUnit test fixtures in a test project. 看看codeplex上的NHarness ,这是一个非常简单的库,允许您在测试项目中运行NUnit测试夹具。 This allows you the ability to debug through your unit tests if required 这使您能够在需要时通过单元测试进行调试

An example (from the codeplex page) of a test runner is as below 测试运行器的示例(来自codeplex页面)如下所示

    public class RunTests
    {
        public static void Main(string[] args)
        {
            TestResults results = Tester.RunTestsInClass<Tests>();

            Console.WriteLine("Tests Run: {0}", results.NumberOfResults);
            Console.WriteLine("Results {0}:PASSED {1}:FAILED", results.NumberOfPasses, results.NumberOfFails);
            Console.WriteLine("Details:");

            foreach (TestResult result in results)
            {
                Console.WriteLine("Test {0}: {1} {2}",
                                            result.MethodName,
                                            result.Result,
                                            result.Result == TestResult.Outcome.Fail ? "\r\n" + result.Message : "");
            }

            Console.ReadLine();
        }
    }

The benefit of this library is that the TestResults class can be used to retrieve information about the tests executed, so the library can also be used in custom unit test applications 此库的好处是TestResults类可用于检索有关所执行测试的信息,因此该库也可用于自定义单元测试应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM