小结 用例 第三篇(共三篇)

2010-03-26  杨群 

以下是我根据第二篇的代码编写的测试用例,有些用例可能是重复了,是不必要的。但是我想用些来试试手,就算是一个不成熟的实验吧。
如果有什么不妥,请大家指正!在此感谢!
其实在这里面是可以用得数据库的,在VS单元测试中可以用到二种数据库,Access SQL,但是呢,在这里我只是简单的做一下,没有使用。
但是,如果有兴趣的朋友可以试着把以下的数据写入到Access或者是SQL中使用数据库用大量的数据进行测试,我想这会更加的完美。
目前对于单元测试,只看到这里。
再有什么东西,我会发出来,请大家指正!
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace TimeTest111
{
   
   
    /// <summary>
    ///这是 Time2Test 的测试类,旨在
    ///包含所有 Time2Test 单元测试
    ///</summary>
    [TestClass()]
    public class Time2Test
    {

        private TestContext testContextInstance;
        /// <summary>
        ///获取或设置测试上下文,上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试属性
        //
        //编写测试时,还可使用以下属性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///ToUniversalString 的测试,把输入的时间,格式化为00:00:00,这个不是标准的时间,如果输入的时间为大于正常的时间
        ///则也会显示出来.如果把这个类测试完全了,则需要 个用例:
        ///1,正常的时间显示:12:13:14   2,错误的时间:25:67:87 
        ///在这里可以定义多个预期值(expected)和多个实际值(actual),分别的调用.
        ///也可以把值存储到数据库中SQL中,从中取值,也可以直接赋值.
        ///</summary>
        [TestMethod()]
        public void ToUniversalStringTest()
        {

            //12:13:14
            Time2 target = new Time2(12,13,14); // TODO: 初始化为适当的值
            string expected = "12:13:14"; // TODO: 初始化为适当的值
            string actual;
            actual = target.ToUniversalString();
            Assert.AreEqual(expected, actual);
            //测试ToUniversalStringTest() 是否可以把这个初始为时间模式
            //25:67:87
            Time2 target1 = new Time2(25, 67, 87); // TODO: 初始化为适当的值
            string expected1 = "25:67:87"; // TODO: 初始化为适当的值
            string expected11 = "00:00:00";// 在settime中如果大于正常的时间(hour>=24 minute>=60 second>=60)则会 返回格式为00:00:00
            string actual1;
            actual1 = target1.ToUniversalString();
            Assert.AreNotEqual(expected1, actual1);
            //所以在此条是不等于的判断
            Assert.AreEqual(expected11, actual1);
            //定义expected11是为了验证settime 是否正确
            //这个函数也就是初始化时间模式,目的是为了验证是否会初始为:00:00:00,所以后面的target1就不是很有必要,在这里只是演示一下.
            //Assert.Inconclusive("验证此测试方法的正确性。");
        }
        /// <summary>
        ///ToStandardString 的测试
        ///此函数是为了把时间设置为标准的格式,也就是23:59:59.也就是24小时制.
        ///用例说明:
        ///1,12点以后算做为下午,也就得加上PM,表示下午.
        ///2,12点以前也就是上午,则会加上AM,表示上午
        ///3,如果输入的时间制为:25:61:62 则会自动转化为00:00:00,因为不在24小时制内,则会出现初始化错误.
        ///</summary>
        [TestMethod()]
        public void ToStandardStringTest()
        {
            //12:13:14为例,这是一个时间在24小时内
            Time2 target = new Time2(12, 13, 14); // TODO: 初始化为12:13:14
            string expected = "12:13:14:PM"; // TODO: 初始化为12:13:14:PM
            string actual;
            actual = target.ToStandardString();
            Assert.AreEqual(expected, actual);
            Time2 target1 = new Time2(2, 3, 4); // TODO: 初始化为适当的值
            string expected1 = "2:03:04:AM"; // TODO: 初始化为适当的值
            string actual1;
            actual1 = target1.ToStandardString();
            Assert.AreEqual(expected1, actual1);
            Time2 target2 = new Time2(25, 61, 62); // TODO: 初始化为适当的值
            string expected2 = "12:00:00:AM"; // TODO: 初始化为适当的值
            string actual2;
            actual2 = target2.ToStandardString();
            Assert.AreEqual(expected2, actual2);
            //Assert.Inconclusive("验证此测试方法的正确性。");
        }
        /// <summary>
        ///SetTime 的测试
        ///SetTime这个函数的作用是把所有的时间制,初始为标准的显示内容.比如:小时不可以大于24,分不可以大于60 ,秒不可以大于60,按这个初始标准执行.
        ///那么,这些的测试用例有以下几个:
        ///1,边界值,00:00:00  25:65:66
        ///2,正常值,10:11:12
        ///</summary>
        [TestMethod()]
        public void SetTimeTest()
        {
            //10:11:12 正常值
            Time2 target = new Time2(10, 11, 12); // TODO: 初始化为适当的值
            int hourValue = 10; // TODO: 初始化为10
            int minuteValue = 11; // TODO: 初始化为11
            int secondValue = 12; // TODO: 初始化为12
            target.SetTime(hourValue, minuteValue, secondValue);
            //int actual1 = target.hour;
            //int actual2 = target.minute;
            //int actual3 = target.second;
            //以下是验证,SetTime();得出结果是否正确.
            Assert.AreEqual(hourValue, target.hour);
            Assert.AreEqual(minuteValue, target.minute);
            Assert.AreEqual(secondValue, target.second);
        }
        [TestMethod()]
        public void SetTimeTest1()
        {
            Time2 target = new Time2(25, 65, 66); // TODO: 初始化为适当的值
            int hourValue = 25; // TODO: 初始化为25
            int minuteValue = 65; // TODO: 初始化为65
            int secondValue = 66; // TODO: 初始化为66
            target.SetTime(hourValue, minuteValue, secondValue);
            //int actual11 = target2.hour;
            //int actual22 = target2.minute;
            //int actual33 = target2.second;
            //以下是验证,SetTime();得出结果是否正确.由于此次的结果是验证是不正确,所以使用方法Assert.AreNotEqual();
            //Assert.AreNotEqual(hourValue, actual11);
            //Assert.AreNotEqual(minuteValue, actual22);
            //Assert.AreNotEqual(secondValue, actual33);
            Assert.AreNotEqual(hourValue,target.hour);
            Assert.AreNotEqual(minuteValue,target.minute);
            Assert.AreNotEqual(secondValue,target.second);
            //Assert.AreNotEqual("预期值", "实际值");
            //Assert.Inconclusive("无法验证不返回值的方法。");
        }
        /// <summary>
        ///Time2 构造函数 的测试
        ///此函数是当只传入一个值(Hour)的时候,这个时候也要进行以下的操作.
        ///</summary>
        [TestMethod()]
        public void Time2ConstructorTest4()
        {
            //12
            int hour = 12; // TODO: 初始化为适当的值
            Time2 target = new Time2(hour);
            Assert.AreEqual(hour, target.hour);
            //Assert.Inconclusive("TODO: 实现用来验证目标的代码");
        }
        [TestMethod()]
        public void Time2ConstructorTest41()
        {
            //44
            int hour = 44; // TODO: 初始化为适当的值
            Time2 target = new Time2(hour);
            Assert.AreNotEqual(hour, target.hour);
            //Assert.Inconclusive("TODO: 实现用来验证目标的代码");
        }
        [TestMethod()]
        public void Time2ConstructorTest42()
        {
            //0
            int hour = 0; // TODO: 初始化为适当的值
            Time2 target = new Time2(hour);
            Assert.AreEqual(hour, target.hour);
            //Assert.Inconclusive("TODO: 实现用来验证目标的代码");
        }
 
        /// <summary>
        ///Time2 构造函数 的测试
        ///此方法测试一个什么参数都不传的函数,
        ///使用两个测试用例:第一个预期值为0,第二个预期值为11随机的.
        ///下面就使用两个方法
        ///</summary>
        [TestMethod()]
        public void Time2ConstructorTest3()
        {
            Time2 target = new Time2();
            string expected = "0";
            string actual = (target.hour + target.minute + target.second).ToString();
            Assert.AreEqual(expected,actual);
            //Assert.Inconclusive("TODO: 实现用来验证目标的代码");
        }
        [TestMethod()]
        public void Time2ConstructorTest31()
        {
            Time2 target = new Time2();
            string expected = "11";
            string actual = (target.hour + target.minute + target.second).ToString();
            Assert.AreNotEqual(expected, actual);
            //Assert.Inconclusive("TODO: 实现用来验证目标的代码");
        }

        /// <summary>
        ///Time2 构造函数 的测试
        ///此方法是测试有两个参数的:hour,minute,查看返回值是什么样的.
        ///在这里预期值中肯定有second 为0
        ///下面实际值中有hour:minute:0
        ///</summary>
        [TestMethod()]
        public void Time2ConstructorTest2()
        {
            //hour=10,minute=30
            int hour = 10; // TODO: 初始化为10
            int minute = 30; // TODO: 初始化为30
            Time2 target = new Time2(hour, minute);;
            Assert.AreEqual(hour, target.hour);
            //定义的hour为预期值,target.hour为实际值
            Assert.AreEqual(minute, target.minute);
            //定义的minute为预期值,target.minute为实际值
            //Assert.Inconclusive("TODO: 实现用来验证目标的代码");
        }
        /// <summary>
        ///Time2 构造函数 的测试
        ///</summary>
        [TestMethod()]
        public void Time2ConstructorTest1()
        {
            Time2 time = new Time2(1, 1, 1); // TODO: 初始化为1:01:01
            Time2 target = new Time2(time);
            Assert.AreEqual(time.hour, target.hour);
            Assert.AreEqual(time.minute, target.minute);
            Assert.AreEqual(time.second, target.second);
            //Assert.Inconclusive("TODO: 实现用来验证目标的代码");
        }
        /// <summary>
        ///Time2 构造函数 的测试
        ///</summary>
        ///
        [TestMethod()]
        public void Time2ConstructorTest()
        {
            //初始值 12:25:42
            int hour =12; // TODO: 初始化为适当的值 也就是预期的值
            int minute = 25; // TODO: 初始化为适当的值 也就是预期的值
            int second = 42; // TODO: 初始化为适当的值 也就是预期的值
            Time2 target = new Time2(12, 25,42);//调用Time2(),传入三个值的,这是实际运行后会出现的值
            Time2 target1 = new Time2(hour, minute, second);
            //两种使用方法,
            Assert.AreEqual(hour+":"+minute+":"+second, target.hour+":"+target.minute+":"+target.second);
            //测试引用Time2(),传入三个参数是否正确.
            //如果AreEqual(a,b)中的a与b的值相等,则测试通过
        }
    }
}
254°/2546 人阅读/0 条评论 发表评论

登录 后发表评论