TDD ...如何? [英] TDD ...how?

查看:35
本文介绍了TDD ...如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我即将开始我的第一个 TDD(测试驱动开发)程序,我(自然地)有一个 TDD 心理障碍..所以我想知道是否有人可以帮助指导我应该从哪里开始.

I'm about to start out my first TDD (test-driven development) program, and I (naturally) have a TDD mental block..so I was wondering if someone could help guide me on where I should start a bit.

我正在创建一个函数,该函数将从套接字读取二进制数据并将其数据解析为类对象.

I'm creating a function that will read binary data from socket and parses its data into a class object.

据我所知,有 3 个部分:

As far as I see, there are 3 parts:

1) 解析数据的逻辑2) 套接字类3) 类对象

1) Logic to parse data 2) socket class 3) class object

我应该采取哪些步骤才能逐步进行 TDD?我肯定打算在实现功能之前先编写测试.

What are the steps that I should take so that I could incrementally TDD? I definitely plan to first write the test before even implementing the function.

推荐答案

TDD 中的问题是可测试性设计"

The issue in TDD is "design for testability"

首先,您必须有一个用于编写测试的接口.

First, you must have an interface against which to write tests.

要达到目标,您必须大致了解可测试单元是什么.

To get there, you must have a rough idea of what your testable units are.

  1. 某个类,由函数构建.

  1. Some class, which is built by a function.

一些函数,它从套接字读取并发出一个类.

Some function, which reads from a socket and emits a class.

第二,给定这个粗略的接口,您将其形式化为实际的非工作类和函数定义.

Second, given this rough interface, you formalize it into actual non-working class and function definitions.

第三,您开始编写测试——知道它们会编译但会失败.

Third, you start to write your tests -- knowing they'll compile but fail.

在此过程中,您可能会开始对自己的功能感到头疼.你如何为你的函数设置一个套接字?那是脖子疼.

Part-way through this, you may start head-scratching about your function. How do you set up a socket for your function? That's a pain in the neck.

然而,你上面粗略的界面不是法律,它只是一个好主意.如果你的函数接受一个字节数组并创建一个类对象怎么办?这更容易测试.

However, the interface you roughed out above isn't the law, it's just a good idea. What if your function took an array of bytes and created a class object? This is much, much easier to test.

所以,重温这些步骤,改变界面,编写非工作类和函数,现在编写测试.

So, revisit the steps, change the interface, write the non-working class and function, now write the tests.

现在您可以填写类和函数,直到所有测试通过.

Now you can fill in the class and the function until all your tests pass.

当您完成这一部分测试后,您所要做的就是在真正的套接字中挂钩.您信任套接字库吗?(提示:你应该)这里没有什么可测试的.如果您不信任套接字库,现在您必须提供可以以受控方式运行的数据源.这是一个很大的痛苦.

When you're done with this bit of testing, all you have to do is hook in a real socket. Do you trust the socket libraries? (Hint: you should) Not much to test here. If you don't trust the socket libraries, now you've got to provide a source for the data that you can run in a controlled fashion. That's a large pain.

这篇关于TDD ...如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆