关闭

如何使用VSTS写单元测试

发表于:2008-8-01 14:37

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:xugang    来源:cnblogs.com

  Microsoft的开发平台VSTS(Visual Studio Team System) 集成了单元测试框架(Team Test),即:VSTS单元测试。   

  它支持:
  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>


21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号