12 Lessons I learned using unit tests/TDD
在团队中进行单元测试/TDD的12条经验
Introduction
Two years ago, I was working in a project where our goal was to write a web excel-like application to calculate products/services prices. The team was splitted in 3 pieces, the development team, the requirement team and the QA team. This project became so big and we didn’t use any types of automated tests (our QA team was doing manual tests) that the project spent more time being tested than being developed. Each little change, the project spent hours, hours and hours with the QA team. One day I went to a developer meeting and talked about my problem with others programmers. They suggested to me learn about unit tests, acceptance tests and TDD.
Learned Lessons
The list below is a lessons list that I learned while applying unit tests/TDD since 2011.
1. Don’t try to apply TDD for the first time or teach TDD to your team in a real project. It won’t work. First is necessary to know how TDD’s flow works. How to mock up objects, how to mock up the framework internals, how organize tests and so on, if your team is not ready, it will slow down the development and you will miss some deadlines.
2. Coding Dojo is a good way to teach TDD. We do coding dojo sessions. It’s the best way we found to teach TDD to new developers and to keep our skills up-to-date.
3. Try to convince your whole team before applying TDD. There isn’t nothing more frustrating than one or two developers ruining our test efforts, commenting code, trying to commit with failing tests and so on. I had bad experiences with non committed developers. Explain about the benefits, how tests keep our software bug-free, how we can refactor the code without worry about break the software and so on.
4. Write sufficient tests. Build a test suite is like to build a shield against bugs, the team should be able to fully trust in this shield when we are doing a refactoring or evolving the software. If this shield has gaps, we are increasing the risk to create unidentified bugs when we change the code. You don’t have to cover 100% of your code, it’s almost impossible and you will lose too much time but cover the majority of your code is perfectly achievable. A good rule is test everything that can possibly break.
5. Use a coverage tool. Coverage tools will report gaps in our test suite. With these tools, is easy to identify code that aren’t being tested. Most of these tools give to us a visual identification, coloring the lines that are being tested in blue/green and coloring in red the lines that are not being tested. If you are a .NET Programmer, the Visual Studio Ultimate comes with this feature or if you are a Java Programmer you can use the EclEmma.
6. Tests should be fast. Fast to run and fast to write. When we are building software we are always pursuing a deadline. Our tests have to help us to achieve this goal and not be a distraction or a delay.
If our tests take too much time to be written, the team will stop to write them when the deadlines become too tight.
If our tests take too much time to run, the team won’t run them everytime they change the code or they would decrease the team’s productivity.
7. Don’t comment or ignore failing tests. Once your team becomes comfortable with build failing due to 1 test, they will be comfortable with the build failing due to 2, 3, 4 tests and so on. At these times, the test suite feedback will be ignored and the tests won’t be helpful anymore.
8. Pair programming helps the team to adopt TDD. When we are trying TDD for the first time or when our deadline is tight, we will have the will to forget the tests and write only production code. Pair programming will prevent the team to cut corners and will keep it writing tests.
9. Keep your test code clean. Once, to speed our productivity, we decided that our test code shouldn’t be as clean as our production code. At first look, it was a good decision but software will change and tests will have to be changed too. We ended up with tests difficult to maintain and with larger estimates due the cost to maintain the tests.
10. Tests should have one and only one reason to fail. Be careful if your test has a lot of assertions. If functions and classes should have only one responsability, our tests should test only one concept. In this way, it will be easier to look at a failed test and figure out what is wrong.
11. Write unit tests will save debug time. A lot of time is spent debugging code, looking for bugs. Once you are writing unit tests, you will have a real-time feedback of each piece in your code, it will be easier to find bugs and consequently and it will reduce the time we spend debugging.
12. Keep pushing. Apply TDD is all about change our mindset. It’s difficult to start to write tests and is even more difficult to write tests BEFORE to write production code. It’s important to keep pushing and writing tests, one day, they will end up saving our lives. Also, once your team is fully comfortable with TDD, the productivity tend to increase.
相关推荐
本主题将聚焦于如何在基于C语言的嵌入式环境,特别是在Keil IDE中,利用Unity这个轻量级的单元测试框架来搭建自动化测试流程。 Unity是一个专门为微控制器和嵌入式系统设计的开源单元测试框架。它的特点是小巧、...
综上所述,TDD单元测试是软件开发过程中的一个重要环节,它通过先编写测试用例,再编写实现代码的方式,促进了代码的质量和设计。通过熟练掌握TDD,开发者可以更好地控制项目的进度,减少后期维护的难题。
在描述中提到的“我公司的测试规范”,意味着讨论的是公司内部的测试流程、标准和指南,这些规范可能包括单元测试、集成测试、系统测试等不同层次的测试策略,以及如何进行测试文档的编写和维护。博文链接指向了一个...
在VS2005中,微软引入了内置的单元测试框架,使得开发者能够方便地进行TDD实践。 **单元测试**是软件开发中的一个重要环节,它对程序的最小可测试单元进行验证,确保每个模块的功能正确无误。在VS2005中,我们可以...
在PHP世界中,TDD可以帮助开发者构建更健壮、可维护的代码,通过不断的单元测试确保代码质量。本篇文章将深入探讨PHP测试框架在TDD环境下的应用。 首先,我们要理解TDD的基本流程:**红-绿-重构**。"红"阶段意味着...
本案例将深入探讨如何在.NET环境中进行单元测试,以及如何利用各种工具和框架来创建和执行这些测试。 首先,我们要理解单元测试的基本概念。单元测试是一种软件测试方法,它针对程序中的最小可测试单元——通常是一...
1. 编写测试:首先,开发者编写一个失败的单元测试,这个测试针对即将实现的功能。这称为"红"阶段,因为测试会因未实现的功能而失败。 2. 实现功能:然后,开发者编写最小的代码量,使刚刚创建的测试通过。这一步...
测试驱动开发(TDD)是一种软件开发方法,它要求开发者首先编写失败的单元测试用例,然后再编写足够的代码以使测试通过。接下来,开发者会对代码进行重构以改善设计,同时确保测试依然能够通过。这个过程循环进行,...
在Android开发中,单元测试是确保代码质量、可维护性和减少缺陷的重要环节。这个小例子将引导我们了解如何在Android项目中设置和执行单元测试。首先,我们需要知道Android的单元测试框架主要有两个:JUnit和 ...
《软件单元测试讲义》是一份深入探讨软件开发中单元测试技术的重要资料,它由知名IT博主linkyou在CSDN上分享。这份PPT详细阐述了单元测试的基础概念、重要性以及实施方法,旨在帮助开发者提升代码质量,确保软件功能...
这篇博客文章“敏捷开发必要技巧12:单元测试”探讨了单元测试在敏捷开发中的应用和重要性,以及如何有效地进行单元测试。 首先,单元测试是对软件中的最小可测试单元进行检查和验证的过程,通常是函数、方法或类。...
在TDD的过程中,开发者首先会为待开发的功能编写一个简单的单元测试,这个测试最初肯定会失败,因为此时还没有对应的代码实现。然后,开发者编写最少的代码使测试通过,这个阶段被称为“红色”阶段,因为测试失败...
在软件开发过程中,单元测试通常是TDD(Test-Driven Development,测试驱动开发)的一部分。在TDD中,开发者先编写测试用例,然后实现代码以使测试通过,最后重构代码以提高可读性和效率。这种方法促进了设计的改进...
单元测试是一种软件开发过程中的重要环节,它主要针对软件中的最小可测试单元——通常是函数、方法或类——进行验证,确保这些单元按照预期工作。在本“有关单元测试的培训资料”中,我们将深入探讨单元测试的核心...
单元测试是软件开发过程中的重要环节,特别是在Java编程中,它被视为确保代码质量、提高可维护性和降低缺陷率的关键步骤。单元测试是对程序中最小可测试单元进行检查和验证,通常是一个函数、方法或类。它的目的是...
本文将详细介绍如何在Visual Studio环境中使用Visual Studio Team Test (简称TeamTest)进行单元测试。 #### 二、概述 Visual Studio Team Test 是一款内置在 Visual Studio IDE 中的强大单元测试工具,它支持多种...
测试驱动开发(Test-Driven Development,简称TDD)是一种软件开发方法,它强调在编写实际功能代码之前,先编写能够失败的单元测试。TDD的核心理念是“红、绿、重构”:首先写一个测试用例(红色阶段,因为测试会...
在本场景中,我们看到的文件列表包含了一个名为"NUnitExample"的项目,这通常是一个使用NUnit框架进行单元测试的例子。 NUnit是一个流行的开源测试框架,适用于.NET平台,包括C#语言。它提供了丰富的断言方法,使得...
《单元测试之道Java版》这本书,正如它的标题所示,是一部关于如何进行Java单元测试的专业指南,它结合了丰富的实例和实际经验,旨在帮助开发者掌握单元测试的技巧,并在软件开发中应用这些技巧。 这本书的读者们,...