51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 3715|回复: 1
打印 上一主题 下一主题

[原创] 本人的很烂的C#单元测试的数据驱动的方法

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2008-3-11 14:55:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
第一种:使用visual studio中的unit test。数据源:access。
步骤:
     1. 添加数据源(主菜单-->数据-->添加数据源)
     2. 连接字符串(可参考msdn)
          [TestMethod()]
          [DeploymentItem("C:\\HE.MDB")]
        [DataSource("System.Data.OleDb", "rovider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\He.mdbersist Security Info=True", "HE", DataAccessMethod.Sequential)]
    3. 使用TestContext.DataRow["字段名"]取相应自段的数据。
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
 楼主| 发表于 2008-3-11 14:56:06 | 只看该作者

第二种:直接编程

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Appconsole;


namespace testIO
{
    class testIOPro
    {
        static void Main(string[] args)
        {

            //==============================================================================================
            //===从数据源(文件C:\DS.csv)取测试数据
            //==============================================================================================
            string[] s = System.IO.File.ReadAllLines("C:\\DS.csv"); //DS.csv中的数据以数组s中
            
            // tcno:测试用例编号
            // input1:输入参数1
            // input2:输入参数2
            string[] tcno=new string[s.Length];
            string[] input1 = new string[s.Length];
            string[] input2 = new string[s.Length];

            // 分割字符串,获取用例编号和参数
            for (int i = 0; i < s.Length; ++i)
            {
                string[] t = s.Split(',');
                tcno = t[0];
                input1 = t[1];
                input2 = t[2];
            }



            //==============================================================================================
            //===执行用例,并导出结果
            //===被测方法:public string consoled(string ss1, string ss2)
            //==被测方法属于:
            //      namespace Appconsole
            //          public class appconsole
            //==============================================================================================
            appconsole aa = new appconsole();

            bool pass = false;  //用例成功状态初始化
            int passCount=0,failedCount=0;  //成功和失败数统计初始化

            for (int i = 0; i < s.Length; ++i)
            {
                string expert = input1 + input2; //期望结果

                DateTime starttime = DateTime.Now;  //开始时间

                string actual = aa.consoled(input1, input2);  //调用被测方法
               
                DateTime endtime = DateTime.Now;    //结束时间
                TimeSpan DT = (endtime-starttime);  //用例执行时间段

                //结果比对
                if (expert == actual) pass = true;
                else pass = false;

                //成功失败计数
                if (pass == true) ++passCount;
                else ++failedCount;

                //输入用例执行情况
                File.AppendAllText("C:\\output.csv", tcno + ',' + expert + ',' + actual + ',' + pass + ',' + DT + '\n');
            }
            //输入成功失败数
            File.AppendAllText("C:\\output.csv", "成功数:" + passCount + '\n' + "失败数:" + failedCount);

        }

    }
}
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-9-22 06:38 , Processed in 0.082387 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表