单元测试中的随机数据? [英] Random data in Unit Tests?

查看:80
本文介绍了单元测试中的随机数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个同事,他为用随机数据填充其字段的对象编写单元测试.他的原因是它提供了更广泛的测试范围,因为它将测试很多不同的值,而普通测试仅使用单个静态值.

I have a coworker who writes unit tests for objects which fill their fields with random data. His reason is that it gives a wider range of testing, since it will test a lot of different values, whereas a normal test only uses a single static value.

我为此提出了许多不同的理由,主要的理由是:

I've given him a number of different reasons against this, the main ones being:

  • 随机值表示测试不是真正可重复的(这也意味着如果测试可以随机失败,则可以在构建服务器上这样做并破坏构建)
  • 如果它是一个随机值,并且测试失败,则我们需要a)修复对象,b)每次都强迫自己测试该值,所以我们知道它是有效的,但是由于它是随机的,所以我们不知道该怎么做值是

添加了另一个同事:

  • 如果我正在测试异常,则随机值将无法确保测试以预期状态结束
  • 随机数据用于刷新系统和负载测试,而不用于单元测试

还有其他人可以补充我可以给他的其他理由,让他停止这样做吗?

Can anyone else add additional reasons I can give him to get him to stop doing this?

(或者,这是编写单元测试的一种可接受的方法,我和我的另一个同事错了吗?)

(Or alternately, is this an acceptable method of writing unit tests, and I and my other coworker are wrong?)

推荐答案

有一个折衷方案.您的同事实际上是在做某事,但我认为他做错了.我不确定完全随机测试是否非常有用,但这肯定不是无效的.

There's a compromise. Your coworker is actually onto something, but I think he's doing it wrong. I'm not sure that totally random testing is very useful, but it's certainly not invalid.

一个程序(或单元)规范是一个假设,即存在一些符合它的程序.然后,程序本身就是该假设的证据.应该进行什么单元测试是为了提供反证,以反驳该程序是否按照规范运行.

A program (or unit) specification is a hypothesis that there exists some program that meets it. The program itself is then evidence of that hypothesis. What unit testing ought to be is an attempt to provide counter-evidence to refute that the program works according to the spec.

现在,您可以手动编写单元测试,但这确实是一项机械任务.它可以是自动化的.您所要做的就是编写规范,一台机器可以生成很多试图破坏您代码的单元测试.

Now, you can write the unit tests by hand, but it really is a mechanical task. It can be automated. All you have to do is write the spec, and a machine can generate lots and lots of unit tests that try to break your code.

我不知道您使用的是哪种语言,但请参见此处:

I don't know what language you're using, but see here:

Java http://functionaljava.org/

Scala(或Java) http://github.com/rickynils/scalacheck

Scala (or Java) http://github.com/rickynils/scalacheck

哈斯克尔 http://www.cs.chalmers.se/~rjmh/QuickCheck/

.NET: http://blogs.msdn.com/dsyme /archive/2008/08/09/fscheck-0-2.aspx

这些工具将采用格式正确的规范作为输入,并使用自动生成的数据自动生成所需的任意单元测试.他们使用缩小"策略(可以调整)来找到最简单的测试用例,以破坏您的代码并确保其很好地覆盖了边缘情况.

These tools will take your well-formed spec as input and automatically generate as many unit tests as you want, with automatically generated data. They use "shrinking" strategies (which you can tweak) to find the simplest possible test case to break your code and to make sure it covers the edge cases well.

测试愉快!

这篇关于单元测试中的随机数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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