TDD:为什么每个功能只能进行一项测试? [英] TDD: Why is there only one test per function?

查看:63
本文介绍了TDD:为什么每个功能只能进行一项测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解为什么在我所看到的大多数专业TDD代码中,每个功能都只有一个测试.最初使用TDD时,我倾向于将每个功能(如果有关联)进行4-5个测试分组,但我发现这似乎并不是标准.我知道每个功能仅具有一个测试更具描述性,因为您可以更轻松地缩小问题的范围,但是我发现自己很费力地想出功能名称来区分不同的测试,因为许多测试是如此相似.

所以我的问题是:将多个测试放在一个函数中真的是一种不好的做法吗?如果是,为什么呢?有共识吗?谢谢

很棒的答案.我相信.您需要真正将它们全部分离出来.我经历了最近编写的一些测试,并将它们全部分离,然后发现,它更容易阅读,并帮助我更好地了解了我所测试的内容.通过为测试指定自己的冗长的冗长名称,它还给我带来了诸如哦,等等,我没有测试其他东西"的想法,因此,我认为这是一条路.

好答案.很难选出胜利者

解决方案

看起来像是在问为什么在我所见过的大多数专业TDD代码中,每个测试为什么只有一个 asserttion ?这可能会增加测试隔离度,并增加出现故障时的测试覆盖率.这当然就是我以这种方式制作TDD库(用于PHP)的原因.说你有

function testFoo()
{
    $this->assertEquals(1, foo(10));
    $this->assertEquals(2, foo(20));
    $this->assertEquals(3, foo(30));
}

如果第一个断言失败,您将看不到其他两个断言会发生什么.这并不能完全帮助查明问题:这是特定于输入的内容,还是系统性的?

I'm having a hard time understanding why there is only one test per function in most professional TDD code that I have seen. When I approached TDD initially I tended to group 4-5 tests per function if they were related but I see that doesn't seem to be the standard. I know that it is more descriptive to have just one test per function because you can more easily narrow down what the problem is, but I find myself struggling to come up with function names to differentiate the different tests since many are so similar.

So my question is: Is it truly a bad practice to put multiple tests in one function and if so why? Is there a consensus out there? Thanks

Edit: Wow tons of great answers. I'm convinced. You need to really separate them all out. I went through some recent tests I had written and separated them all and lo and behold it was way more easier to read and helped my understand MUCH better what I was testing. Also by giving the tests their own long verbose names it gave me ideas like "Oh wait I didn't test this other thing", so all around I think it's the way to go.

Great Answers. Gonna be hard to pick a winner

解决方案

looks like you're asking "why there is only one assertion per test in most professional TDD code I have seen". That's probably to increase test isolation, as well as test coverage in presence of failures. That's certainly the reason why I made my TDD library (for PHP) that way. say you have

function testFoo()
{
    $this->assertEquals(1, foo(10));
    $this->assertEquals(2, foo(20));
    $this->assertEquals(3, foo(30));
}

If the first assert fails, you don't get to see what would happen with the other two. That doesn't exactly help pinpoint the problem: is this something specific to the inputs, or is it systemic?

这篇关于TDD:为什么每个功能只能进行一项测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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