zoukankan      html  css  js  c++  java
  • 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);

            }

        }

    }

     

  • 相关阅读:
    .NET 客户端上传本地excel文件到服务器上,并在客户端显示
    C#实现数字字符串左补齐0的3种方法
    C# 输出pdf文件流在页面上显示
    ASP.NET 将数据生成PDF (二)
    asp.net生成PDF文件 (1)
    stl常用的查找算法
    stl中的transform()注意其与for_each的不同点(有无返回值)
    stl中的for_each() 函数的注意事项
    如何在VMware系统中的ubuntu16.04中建立与win7系统的共享文件夹
    关于linux中用vi新建立一个.c文件无法保存,显示E212错误的时候
  • 原文地址:https://www.cnblogs.com/Gemgin/p/3136364.html
Copyright © 2011-2022 走看看