vs2008软件测试实战 2 test driven development

    技术2023-03-19  43

    Test Driven Development


    1.什么是测试驱动开发?

    2.使用“测试驱动开发”过程?

    3.测试驱动开发的好处是什么?

    4.一个好的unit test的特征

    5.通过一个简单实例了解“测试驱动开发”的过程

    6.参考资料


    1.什么是测试驱动开发?

     

    简单的讲“测试驱动开发”就是说在编写代码之前,首先编写测试用例,编写完成的代码需要能够通过测试用例的验证,也就是相当于首先明确要编写的代码的功能需求,然后开始编写代码,最终需要满足上述的功能需求。下面这幅漫画生动的解释了tdd是如此的具有魅力:

     

     


     

    2.使用“测试驱动开发”过程?

     

    了解需求 -> Red: 编写测试用例,显然测试是无法通过的,这是真正的代码还没有编写 -> Green: 编写代码使这些test通过 -> Refactor: 重构上面编写的代码,已达到更好的设计同时需要保证功能的实现。

     

     


     

    3.测试驱动开发的好处是什么?

     

    如何说服你的team member使用“测试驱动开发”?这就不得不说测试驱动开发的好处所在。

    测试即文档减少调试代码时间,因为每个unit已经测试通过软件能够被更好的设计,重构

    4.一个好的unit test的特征

    run fastSeparates or simulates environmental dependencies such as databases, file systems, networks, queues, and so onIs very limited in scope. If the test fails, it's obvious where to look for the problemOften uses stubs and mock objectsClearly reveals its intention

    5.通过一个简单实例了解“测试驱动开发”的过程

     下面将通过实现一个简单的stack的实例了解“测试驱动开发”的整个过程。

    明确task:我们需要实现一个stack,需要能够满足一定的功能:入栈,出栈,判空等。to brainstorm a list of tests for the task选择一个test item,首先进行测试,这个选择的标准可能是:“最简单测试优先”等。这里假定选择的是Create a Stack and verify that IsEmpty is true。首先编写测试用例:

    [Test] public void Empty() { Stack stack = new Stack(); Assert.IsTrue(stack.IsEmpty); }

     

    显然这时还不存在任何实现栈的代码,上面的测试肯定是通过不了的,那么为了上面的测试能够通过,现在开始编写stack的isEmpty的实现代码:

    using System; public class Stack { private bool isEmpty = true; public bool IsEmpty { get { return isEmpty; } } }

     

    显然第一个任务已经完成,下面需要使第二,三...测试通过。最终审核上面为了使测试用例通过编写的代码是否是最优化的代码,进行重构,修改代码。


    6.参考资料

     

    test driven development in microsoft .net.chm 下载

     


     

    本博客中的内容均是在学习过程中的总结,其中难免存在不足之处,欢迎您指正。

    最新回复(0)