歲月輕品's profile蚂蚁的窝PhotosBlogLists Tools Help

Blog


    25 June

    VSTS作Unit Test的总结

    Visual Studio 2005 集成了单元测试框架(Team Test),正好公司规范开发流程,需要提交Unit Test Log,以前都是用Nunit作单元测试,现在既然VS有了自己的UT类,就打算尝试一下,感觉和Nunit用起来差不多,没怎么深入,只是简单总结一下。
    VSTS的单元测试(主要是Microsoft.VisualStudio.TestTools.UnitTesting)它支持:
                                              1. 生成测试代码框架;
                                              2. 在IDE中运行测试;
                                              3. 支持从数据库中加载数据的测试;
                                              4. 测试运行完成后,进行代码分析覆盖。

    使用VSTS写单元测试的步骤如下:
                                                      1. 创建测试;
                                                      2. 编写测试;
                                                      3. 运行测试;
                                                      4. 代码覆盖。

    1.  创建测试

    打开解决方案中的.cs类文件(如:StudentManager),在其中的一个方法(如:AddStudent() )上右击,选择“创建单元测试”命令。

    在弹出的“创建单元测试”对话框中的“输出项目”下拉框中选择“创建新的Visual C# 测试项目”,单击“确定”按钮,并在“新建测试项目”对话框中输入测试项目的名称(如:MySchoolTest),单击“创建”按钮后,就看见在原有的解决方案中生成了一个新的项目“MySchoolTest”。

    测试项目创建成功后,会同时生成4个与测试相关的文件

    AuthoringTest.txt                  提供创建测试的说明,包括向项目增加其他测试的说明;

    StudentManagerTest.cs         包含AddStudent()的测试,以及测试初始化和测试清除的方法;

    MySchoolPro.vsmdi                测试管理文件;

    localtestrun.testrunconfig      本地测试运行配置文件。


    2.  编写测试

    创建测试完毕后,VSTS 为我们自动生成的只是一个测试框架,默认代码中Assert.Inconclusive 表明这是一个未经验证的单元测试。

    打开生成的测试文件“StudentManagerTest.cs”,    如示例1:

    [TestMethod]
    public void AddStudentTest()
    {
       
    global::MySchool.BLL.StudentManager target = new
                   
    global::MySchool.BLL.StudentManager();  
       
       
    // TODO:初始化为适当的值
       global::MySchool.Models.Student student = null;
       
    string expected = null;
       
    string actual;
       actual 
    = target.AddStudent(student);

       Assert.AreEqual(expected, actual,
                        
    "MySchool.BLL.StudentManager.AddStudent 未返回所需的值。");
       Assert.Inconclusive(
    "验证此测试方法的正确性。");
    }

    单元测试中,几个变量的简单介绍:

    target         表示测试目标对象,通过这个目标对象可以测试该类中的各个方法;

    expected    表示期望得到的值;

    actual         表示实际得到的值;


    单元测试中,常用的断言方法介绍:

    Assert.AreEqual()          测试指定的值是否相等,如果相等,则测试通过;

    Assert.Inconclusive()    表示一个未验证的测试;

    Assert.IsTrue()              测试指定的条件是否为True,如果为True,则测试通过;

    Assert.IsFalse()             测试指定的条件是否为False,如果为False,则测试通过;

    Assert.IsNull()               测试指定的对象是否为空引用,如果为空,则测试通过;

    Assert.IsNotNull()          测试指定的对象是否为非空,如果不为空,则测试通过;


    我们通过对示例1 添加测试所需的初始值,并对断言进行简单的修改后,便得到一个正式的单元测试。

    如示例2:

    [TestMethod]
    public void AddStudentTest()
    {
       
    global::MySchool.BLL.StudentManager target = new
                   
    global::MySchool.BLL.StudentManager();  
       
       
    // TODO:初始化为适当的值
       global::MySchool.Models.Student student = new
                   
    global::MySchool.Models.Student();   // 修改1
               student.LoginId = "003";
               student.LoginPwd 
    = "test003";
               student.UserStateId 
    = 1;
               student.studentName 
    = "test003";
               student.studentNo 
    = "test003";
               student.Sex 
    = "";
               student.ClassID 
    = 1;

       
    string expected = "学员帐户创建成功!";   //修改2
       string actual;
       actual 
    = target.AddStudent(student);

       Assert.AreEqual(expected, actual,
                        
    "MySchool.BLL.StudentManager.AddStudent 未返回所需的值。");
       
    // Assert.Inconclusive("验证此测试方法的正确性。");
    }

    这样,便得到了一个正式的单元测试。用断言Assert.AreEqual()比较expected、actual是否相等。
    如果相等,测试通过。

    配置文件中的设置
    由于我们的测试需要和数据库打交道,并且数据库的连接字符串是从配置文件中读取的,所以我们需要在测试项目中添加配置文件(app.config)。

    如示例3:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        
    <configSections>
        
    </configSections>
            
        
    <connectionStrings>
                 //数据库连接字符串设置
                
    <add name="DataBaseOwner"  connectionString="dbo" />
               
    <add name="MySchoolConnectionString"  
                        connectionString
    =" Data Source=.; Initial Catalog=MySchool;
                                                  User ID=sa; Password
    =123456 "
                        providerName
    ="System.Data.SqlClient" />
        
    </connectionStrings>
    </configuration>


    3.  运行测试

    打开包含有测试项目的解决方案,在 工具栏 就会出现与测试项目相关的 操作按钮栏。
    我们要运行项目中的测试,只需要运行测试项目。

    测试项目的运行方式有两种:
                                       
          运行,并启动调试功能;
                                              运行,但不启动调试功能;

    1.  当我们运行测试后,在“测试结果”窗口中,将列出项目中所有的测试。

    2.  开始的时候,测试会处于“挂起”的状态,测试运行的结果是“通过”或者“失败”。

    3.  如果我们要查看测试结果的额外细节时,选定测试项并双击,便打开了详细信息窗口。


    4.  代码覆盖

    代码覆盖是单元测试的一个关键指标。

    代码覆盖:是指单元测试运行时,覆盖了多少代码。

    Team Test 包含了一个代码覆盖工具,可以详细解释被执行代码的覆盖率,并突出显示哪些代码被执行,哪些代码没有被执行。

    注意: VSTS 在生成单元测试框架时,默认没有启用“代码覆盖”功能。

    启用此功能的办法:

    1.  首先打开“本地测试运行配置文件” localtestrun.testrunconfig ,在解决方案中。

    2.  双击“localtestrun.testrunconfig”文件,弹出该对话框窗口。

    3.  在其对话框窗口的左侧选择“代码覆盖率”,然后在右侧的“要检测的项目”中选择要检测的项目。

    4.  单击“应用”按钮。


    当启用了代码覆盖功能后,再次运行单元测试时:

    在“代码覆盖率结果”窗口中,选中“AddStudent()”双击,便可查看代码覆盖率。

    在“代码覆盖率结果”窗口中,我们还可以查看单元测试中代码覆盖的块数,以及代码覆盖的百分比信息。

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://antfengyiany.spaces.live.com/blog/cns!D1FA5A63D3E2677E!270.trak
    Weblogs that reference this entry
    • None