What I Learned as a Junior Developer Writing Tests for Legacy Code(转载)
I go to the gym and lift weights because I like the feeling of getting stronger and better. Two months ago, I started to feel a throbbing pain in my knees, yet I continued to go to the gym.
I wanted so badly to keep getting stronger that I neglected my health and pushed myself to keep lifting weights. As you can imagine, my knee pain didn’t improve, and more and more of my body needed to recover after each grueling workout session.
As a junior developer at Rackspace (as part of the Airbrake team), at times I feel a similar drive to continue pushing out code that I think will make the product stronger. But just like my knees would complain each time I squatted, untested legacy features would buckle and sometimes break under the weight of my new features.
Eventually my physical therapist convinced me to focus on rehabilitation and prevention before continuing to lift weights. When I was healthy and had more stabilizing muscle strength, he said, I could progress faster and less painfully because I’d be less likely to hurt myself.
Taking his advice to heart, I saw a connection to my job. I felt obliged to write tests for legacy features in my product so I could progress quickly and have more confidence that I wasn’t destroying any other features along the way.
Here are some key takeaways I learned during that process:
Tools are important
I use RSpec, Capybara, FactoryGirl and Selenium for testing my Rails app. Before you write tests, be sure you research the right tools. You don’t want to spend more time wrestling with new tests than fixing broken code.
Even simple tasks like cleaning your database before and after the tests run are important to get right if you want to keep tests fast and repeatable. To do this, I use the database_cleaner gem.
If you’re the first developer on your team to catch the testing bug, it’s important to also consider that tools are important because a poor choice here can lead to others on your team neglecting to test. The whole team has to commit to testing, which means you have to make it as easy as possible to begin testing, starting with powerful, flexible tools.
Specs tell you how code should work and not how it does work
When I began testing other people’s code, I noticed I was getting in the habit of writing tests that passed first and then assessing whether the code was actually supposed to work that way. Sometimes it worked, other times I ended up writing tests that passed only because I was overfitting to my examples.
It can be tempting to take the easy way out and assume all up-to-date code works as desired and then write tests to confirm that assumption. This can lead to a strange pattern of reverse-test-driven-development: the code becomes the spec for the tests.
Focusing on the original intent of the code is a safer option, and senior developers will most likely be willing to clarify code if you can’t figure it out on your own.
You can’t test it all, so don’t try
Writing tests for an entire production app can be a full-time job. Of course, we all want 100%(+) test coverage, just like we want to have a sink free of dirty dishes. But instead of arduously cleaning the whole sink once in a while, another alternative is cleaning and putting away your dishes away as you use them. I took this strategy and now write tests to go with any new code and any legacy code I may have touched in the process.
I recommend testing as a useful procrastination for junior developers, since it forces you to learn about the code base and gets good muscle memory ingrained. But do make sure you follow these guidelines when you test.
PS: My knee feels great, thanks for asking.
What I Learned as a Junior Developer Writing Tests for Legacy Code(转载)的更多相关文章
- Writing Reentrant and Thread-Safe Code(译:编写可重入和线程安全的代码)
Writing Reentrant and Thread-Safe Code 编写可重入和线程安全的代码 (http://www.ualberta.ca/dept/chemeng/AIX-43/sha ...
- PL/SQL Developer使用技巧、快捷键(转载)
1.类SQL PLUS窗口:File->New->Command Window,这个类似于oracle的客户端工具sql plus,但比它好用多了. 2.设置关键字自动大写:Tools-& ...
- C# Note37: Writing unit tests with use of mocking
前言 What's mocking and its benefits Mocking is an integral part of unit testing. Although you can run ...
- Mocks Aren't Stubs
Mocks Aren't Stubs The term 'Mock Objects' has become a popular one to describe special case objects ...
- [Unit Test] Unit Test Brief Introduction
Levels of Testing- Acceptance- Performance- Functional- Integration- Unit Why Unit Testing- Feedback ...
- 【Java基础】Java开发过程中的常用工具类库
目录 Java开发过程中的常用工具类库 1. Apache Commons类库 2. Guava类库 3. Spring中的常用工具类 4. 其他工具 参考 Java开发过程中的常用工具类库 1. A ...
- Best practices for a new Go developer
https://blog.rubylearning.com/best-practices-for-a-new-go-developer-8660384302fc This year I had the ...
- C# Development 13 Things Every C# Developer Should Know
https://dzone.com/refcardz/csharp C#Development 13 Things Every C# Developer Should Know Written by ...
- 27 Best Free Eclipse Plug-ins for Java Developer to be Productive
Eclipse offers an integrated development environment having an extensible plug-in system. This enabl ...
随机推荐
- 【Java】JDBCUtil模板
package jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; ...
- Unity3D实现3D立体游戏原理及过程
Unity3D实现3D立体游戏原理及过程 183 0 0 下面的教程是我今天整理的资料,教大家一步步完成自己的3D立体游戏,并向大家介绍一些3D成像的原理. 理论上,每个普通的非立体3 ...
- 洛谷 P3648 [APIO2014]序列分割 解题报告
P3648 [APIO2014]序列分割 题目描述 你正在玩一个关于长度为\(n\)的非负整数序列的游戏.这个游戏中你需要把序列分成\(k+1\)个非空的块.为了得到\(k+1\)块,你需要重复下面的 ...
- swagger2的常用注解,传递参数的注意使用方法
背景介绍: 刚开始的时候,在controller层使用@RequestParam的时候,发现这个参数是必须要输入值的,但是我们有时候必须查询的时候允许参数为空,使用这个注解就不行了. 在集成了swag ...
- Linux常用网络工具:路由扫描之traceroute
之前两篇<Linux常用网络工具:fping主机扫描>和<Linux常用网络工具:hping高级主机扫描>都是关于主机扫描的,本篇介绍Linux下常用的路由扫描工具tracer ...
- chrome 浏览器如何安装草料二维码
https://cli.im/news/6527 实测有效
- struts标签错误:Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
今天使用eclipse开发ssh,出现Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/co ...
- 前端PHP入门-028-文件操作-掌握级别
作为刚入门我们员经常会干的意见事情是ctrl+c和ctrl+v,鼠标右键删除文件.会control+c(或右键)复制.粘贴文件以及新建文件,还可以设置文件的是否为只读文件等等 可不可以写入修改配置文件 ...
- JS数组---转及补充--
javascript之数组操作 1.数组的创建 var arr=Array();//写的角标数及直接写角标对应的内容简写 var arr=Array("我","爱&quo ...
- checkbox选择根据后台List数据进行回显
需求:记住用户已经选择的 checkbox 选项,当用户再次对该 checkbox 进行选择操作时,应对该用户已经选择的 checkbox 选项进行选中操作. 示例代码: checkbox,js遍历后 ...