Test Driven Development

Test Driven Development --TDD

 

 

测试系统开发工具,程序员观念要从断点(F9)转向测试(业务),测试先行。首要条件就是

需求明确,先把需求分割成每一小块,编辑这一小块的测试代码,然后再编写满足测试的源代码就是我们项目。整个思想就是这样的。

 

测试代码列表编辑打开过程,在工具栏找到测试→窗口→测试列表编辑器(VS2008)/ 测试管理器(VS2005)

 

此工具进行WebTest,不能直接压力测试,主要是输出什么,返回什么。

 

下面举个例子:

 

目标:利用现有的类(如ArrayList),实现一个未绑定的堆栈(暂定名为AdvStack)。该堆栈可以有Push、Pop、Top方法,以将object(第一阶段为object,第二阶段为特定单一类型)类型的对象放入或移出堆栈。还应有IsEmpty属性,表示当前是否堆栈为空。

Figure 2-1: Push operation

 查看更多精彩图片

Figure 2-2: Pop operation

 查看更多精彩图片

Figure 2-3: Top operation

 查看更多精彩图片

方法:TDD

步骤:小步前进,测试先行

路径:

1、挑选简单的:IsEmpty为线索

2、挑选核心业务功能:Push/ Pop/ Top

 

测试用例:

1、新建一个AdvStack,测试IsEmpty属性为True

2、添加(Push)一个元素,测试IsEmpty属性为false;

3、添加(Push)一个元素,然后Pop,测试IsEmpty属性为true;

4、push一个元素,pop后,判断两者是否相等

5、顺序push3个元素、然后pop,看是否一致

6、pop一个空堆栈,是否出现异常

 

//AdcStack.cs源代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;

 

 

namespace ConsoleTDD

{

    public class AdcStack

    {

        public int Add(int a, int b)

        {

            int c = a + b;

            return c;

        }

        ArrayList elements = new ArrayList();

        public bool IsEmpty

        {

            get { return this.elements.Count == 0; }

        }

        public void Push(object item)

        {

            int i = 1;

            i++;

            if (i > 10)

            {

                Console.WriteLine(i);

            }

            this.elements.Insert(0, item);

        }

        public object Pop()

        {

            object result = this.Top();

            this.elements.Remove(0);

            return result;

        }

        public object Top()

        {

            object result = null;

            if (!this.IsEmpty)

            {

                result = this.elements[0];

            }

            else

            {

                throw new InvalidOperationException();

            }

            return result;

        }

    }

}

//AdcStackTest.cs 测试用的

using ConsoleTDD;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using System;

namespace TestProject1

{ 

    /// <summary>

    ///这是 AdcStackTest 的测试类,旨在

    ///包含所有 AdcStackTest 单元测试

    ///</summary>

    [TestClass()]

    public class AdcStackTest

    {

        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()

        //{

        //     AdcStack target = new AdcStack(); // TODO: 初始化为适当的值

        //}

        //

        //使用 TestCleanup 在运行完每个测试后运行代码

        //[TestCleanup()]

        //public void MyTestCleanup()

        //{

        //}

        //

        #endregion

        /// <summary>

        ///Push 的测试

        ///</summary>

        [TestMethod()]

        public void PushTest()

        {

            AdcStack target = new AdcStack(); // TODO: 初始化为适当的值

            object item = "123"; // TODO: 初始化为适当的值

            target.Push(item);

           // Assert.Inconclusive("无法验证不返回值的方法。");

            Assert.IsFalse(target.IsEmpty, "The stack should not be empty.");

        }

        /// <summary>

        ///IsEmpty 的测试

        ///</summary>

        [TestMethod()]

        public void IsEmptyTest()

        {

            AdcStack target = new AdcStack(); // TODO: 初始化为适当的值

            bool actual;

            actual = target.IsEmpty;

           // Assert.Inconclusive("验证此测试方法的正确性。");

            Assert.IsTrue(actual, "A new stack should be empty.");

        }

        /// <summary>

        ///PushOneTest 的测试

        ///</summary>

        [TestMethod()]

        public void PushOneTest()

        {

            AdcStack target = new AdcStack(); // TODO: 初始化为适当的值

            object expected = "123"; // TODO: 初始化为适当的值

           target.Push(expected);

           object actual = target.Pop();

            // Assert.Inconclusive("无法验证不返回值的方法。");

            Assert.IsFalse(target.IsEmpty, "The stack should not be empty.");

            Assert.AreEqual(expected,actual, "Not match!.");

        }

        /// <summary>

        ///Pop 的测试

        ///</summary>

        [TestMethod()]

       [ExpectedException(typeof(InvalidOperationException))]

        public void PopAdcStackTest()

        {

            AdcStack target = new AdcStack(); // TODO: 初始化为适当的值

            object expected = null; // TODO: 初始化为适当的值

            object actual;

            actual = target.Pop();

            Assert.AreEqual(expected, actual);

            Assert.Inconclusive("验证此测试方法的正确性。");

        }

        /// <summary>

        ///Top 的测试

        ///</summary>

        [TestMethod()]

 

        public void TopTest()

        {

            AdcStack target = new AdcStack(); // TODO: 初始化为适当的值

            object expected = "1"; // TODO: 初始化为适当的值

            target.Push(expected);

            object actual=target.Top().ToString();

            Assert.AreEqual(expected, actual.ToString(), "Should be equal");

            Assert.AreEqual(expected, actual.ToString(), "Should be equal");

 

        }

        /// <summary>

        ///Add 的测试

        ///</summary>

        [DataSource("System.Data.SqlClient", "Data Source=CWSZT121-03;Initial Catalog=ReportServer;Integrated Security=True", "Table_1", DataAccessMethod.Sequential), TestMethod()]

        public void AddTest()

        {

            AdcStack target = new AdcStack(); // TODO: 初始化为适当的值

            int a = int.Parse(TestContext.DataRow[2].ToString()); // TODO: 初始化为适当的值

            int b = int.Parse(TestContext.DataRow[0].ToString()); // TODO: 初始化为适当的值

            int expected = int.Parse(TestContext.DataRow[1].ToString()); ; // TODO: 初始化为适当的值

            int actual;

            actual = target.Add(a, b);

            Assert.AreEqual(expected, actual);

        }

    }

}

 

转载于:https://www.cnblogs.com/Gemgin/archive/2013/06/13/3136364.html

1.敏捷开发强调测试先行,因为先编写测试代码,就可以有目的的预防某些bug的产生,要知道,治病不如预防!!!所以我个人觉得这个还是比较重要的,文件也不大,只有8M,但是高清。 2.本版本与网上其他资源不同之处在于,此版本可进行编辑,搜索,已进行内容识别扫描。可全选,可编辑,可剪切文字。 部分目录如下: 目录 第一部分资金实例 第1 章多币种资金··························································································································3 第2 章变质的对象························································································································10 第3 章一切均等····························································································································13 第4 章私有性·································································································............................... 16 第5 章法郎在诉说........................................................................................................................ 18 第6 章再谈一切均等.................................................................................................................... 21 第7 章苹果和桔子........................................................................................................................ 25 第8 章制造对象····························································································································27 第9 章我们所处的时代................................................................................................................ 31 第10 章有趣的Times 方法.…....….. …... …..... ….. ….... …... ….. ……….... …... ….. …….... ……... ……...... 36 第11 章万恶之源.......................................................................................................................... 41 第12 章加法,最后的部分..…..….. ….... …... ….... …..... …... ….. ….. …..... ….. …... …....... ……………· ….. 44 第13 章完成预期目标.................................................................................................................. 48 第14 章变化.................................................................................................................................. 53 第15 章混合货币.......................................................................................................................... 57 第16 章抽象,最后的工作.......................................................................................................... 61 第17 章资金实例回顾.................................................................................................................. 65 . 下一步是什么? •••••••••••·••••••••• 暹···········…...... 匿·…... …......... 霉.........…········· 暹....….. 查......................... 65 比喻66 JUnit 的向.法::::::::::::鲁....愈::::::::::::::::::::::::::::::::::::::::::::::::::::董:::::::::::::::::::::::::::::::::::::::::::::::::::::::66 代码统计.......................................... 夔················…................................................................ 麟..... 67 过程............................................................................................................................................ 68 了:回顾::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 第二部分xUnit 实例 第18 章步入xUnit ......................................... 簪.........…......... …....... ….... …..................................... 73 第19 章设置表格··················--.. ·········.. ··••• 暹........... 薹··································"······"·····…................... 77 第20 章后期整理................. 畛················............................................................ 爹...参................….... 80 第21 章计数.......................................................................................…................................... ….. 83 第22 章失败处理.......................................…... 窜........….. ….............................. ….. ….... …............... 86 第23 章如何组成一组测试...........雪…………………..………………………………………….. 霞...…………….. 88 第24 章xUnit 回顾.............雪...... 暹........... 匿········ 摹…················--雹............ …............ 霉….........…............. 93 第三部分测试驱动开发的模式 第25 章测试驱动开发模式会....................…........... 吻.....……..... ……....... 诊....………………………......... 97 测试(名词)暹.雪.... 雪......................…......................................... 崎................................................. 97 相互独立的测试(Isolated Test) ……..... …·…….......... …………………………...... …………….... 雀..... 98 测试列表(Test List) ................................................... 豪....................…..... …........................... 99 测试优先(Test First) 断言优先 (Assert First;·::::::::::::::::::::::二 ::::::::::::::::::::神: ....:::...:.......二二厂::::厂厂 100 101 测试数据(Test Data) .显然数据(Evident Dat~·;··:::::::::::::::::::::::::::::::::::::: 二二:::::::::::::::::::::::::::::::::::::::::::…::::: 102 103 第26 章不可运行状态模式104 一步测试(One Step Test)··:::::::::: 二:....:..:墓:::::::二:厂二二二::::::::::: 二勹二二::..二二:: 104 启动测试(Starter Test) ................................... 金.......…·······....................... 渗........................... 105 说明测试<Explanation Test) 106 学习测试(Leaming Test) .... 二二二二二二二二二二二二二二二二二二二二二二二: 106 另外的测试 回归测试(;~鲁酝已卢;比~~;噜:::::::::::::: 二..二:.:二二二二二二:.二:::::::::::::::::::::::::::: 108 107 ::开 闷:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::缴:惫:::::::::: 便宜的桌子,舒适的椅子…………........……·…………………· ……………..... …………………………... 110 第27 章测试模式................…........ ….......................... 愈............................................................... 111 子测试(Child Test) .......... 筝.............….................................... 噙.............................................. 111 模拟对象(Mock Object) . …...... 摹.......………………·……………………………………………………· …111 :::1言lf(:n~~~~·;···::::::::::二::::::::::::::::::::::::::::::::::::::::::::::: 二::.二:.:.. :::....}}: 清扫割试死角(Crash Test Dummy) . ……... ………· ……………... ……….. …………......... …….. ….. 114 昙需它需心为芯芯;沁五::::::::::::::::::::::::::::::::::::睿 ......查::::::::::::::::::::::::::::::::::::::::::: :~! 第2~伪!:~昙,~:动了二::.二:./.二:.二二二:...::二二:/二三:.靡:...二::::::::::::::::: !!; 三角法(Triangulation) .... 暹........ 参...................................................................佛....................... 119 显明实现(Obvious Impl ementatlon) ….................... 拳...............................…......................... 119 从一到多(One to Many) ........................................ …....... 匿.............….................................. 120 第29 章xUnit 模式 断言 (Assertion).. 二二二.二二二::::.二:::::::::::::::::::::::::看 :.:..令::::::::::::::::::::::::::::::::::::::: 122 122 固定设施(Fixture) ............................................................................................................... 123 ;古婴:哼宁:st(贮笠:飞F~~~~~-~...:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ~ 芦 异常测试(Exception Test) . …... ……....... …....... ………... …............... 争............. …·鲁............. 令奄.... 127 全部测试(All Tests) ..................... 耄..令......................................... 伞......................................... 127 第 3~命:巴尸尸式..:. .::.: ..: :::.,二:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ;~~ 值对象簪............... 逾............................ 雪... 潭.................................................................................... 131 盓:去:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ~!! 插入式对象 插入式选择霹:::::: 二::::::::::::::::::::::令: ........:噜:::::::::::::::::::::::::::::::::::::::::::::::::::::::::135 134 工厂方法.................................... 参·········"···········...........................................噜..............….......... 137 递归:::::::::参::::::::::::::::::::::::::::::::::::::::::参:::::::::::::::::::::::::::::::::::睿...集:::::::::::::::::::::::::::::::: :~; 收集参数 单例模式亭~~-;心如~;--·::::::::二二:.二二二二二二二二二二二二二二:.. :.二:../...: 140 141 第31 章重构........................................................................................................................…..... 142 调和差异( Reconcile Differences) . …………………….. ……………………........ 萼..........……….. …... 142 隔离变化(Isolate Change) 帚...……………………........ …...... ……………... …………· …….. ………….. 143 :芯:亡::~!:~~==~;··::: 勹....勹勹:::::::二/..::/厂:.二:::::::::二二二二二二:..::::: ;芯之笘:芦::兄芯立立e)鲁·:参:.:.:.:食::::::::: 俺.. :..:二:::::::::::::::::::::::: 二:参:..二::::::::::::::: 二.::::..:芯 转移方法(Move Method) ………· ….. ……..... ……….... ………..... ………............. ………………....... 147 方法对象(Method Object) .. …………· ……….. ……………......... ……………….... …………………….. 148 添加参数(Add Parameter) …….. ….. ……………………………..... ……………..... ……………..... ….... 149 把方法中的参数转变为构造函数中的参数••• 重..............................................……....... …........ 149 第32 章掌握TDD ….....…….. ……..... ………….. ……………………...... ……………………………….. …... 夸... 150 附录沁尸尸严:::::::::::::::::::::::::::::::::睿 :.二:::::::::: 二:.:::二:.二:::.:.:::..::.::.:.::::.:..::::.二.:::::二二:.:::二腐 附录B 斐波纳契数列.…………·.........·….. …………....... ·······………….. 睿... 夸................. ….............. 食摩.. 164
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值