TDD:数断言,什么实际断言? [英] TDD: Number of Asserts, and what to actually assert?

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

问题描述

我目前正在写使用TDD的测试,我已经碰到了一些问题。

I am currently writing tests using TDD and I have come up against a few queries.

一般情况下编写单元测试的时候,我总是习惯用每单元测试1断言,因为这是被定义为好的做法和它很容易明白为什么测试失败。

Normally when writing unit tests, i always used to use 1 assert per unit tests as this is what is defined as good practice and its easy to see why your test is failing.

在TDD,就是它也很好的做法,这样做,如果是这种情况,那么,有效地设计使用TDD,我要结束了超过1单元测试1方法 - 因为我将有效地需要超过1断言。

In TDD, is it also good practice to do the same, if this is the case then to effectively design 1 method using TDD I am going to end up with more than 1 unit test - as I would effectively need more than 1 assert.

另外值得关注的是什么其实我主张?

The other concern is what do I actually assert ?

我可以断言我想返回的对象可能是什么?

I could assert what i think the return object could be ?

所以我必须创建返回类型(可能是许多复杂的属性),并确保这些匹配的断言,这可能是技术上1断言。

So I would have to create the return type (could be complex with many properties) and ensuring that these match on the assert, this could be technically 1 assert.

或者其他方式将确保我的嘲笑,我已经取得了前进的道路实际上是被称为即连接起订量我可以做到以下几点

Or the other way would be to ensure that my mocks that i have made up along the way are actually being called i.e. en MOQ I could do the following

     myServiceMock.Verify(x => x.ItemsReceived(), Times.Once());

所以,我可以保证的方法只调用一次在我的模拟,其实这是真的归类为一个断言。所以又回到了原来的查询,每单元测试1断言,所以我需要创建额外的单元测试,以确保其他嘲笑其它方法被调用。

So i can ensure that a method is called ONLY ONCE on my mock, this is actually really classed as an assert. So it goes back to the original query, 1 assert per unit test so I would need to create additional unit tests to ensure that other methods on other mocks are being called.

什么是每个人在这里做什么?

What is everyone else doing here ?

你断言方法被调用的嘲笑,或返回的值是你所期望的。

Do you assert that methods are called on mocks or that the values being returned as what you expect.

很期待任何输入任何人有这个。

Really look forward to any input anyone has on this.

推荐答案

我看到你的问题有不同的关切,我想掩盖。

I see that your question has different concerns that I would like to cover.

首先,关于一断言每次测试。有一个关于它的好做法与否不同的思维。事实是,它使事情过于复杂严格遵守的时候。 TDD应该帮助你设计好的,干净code,但不会让你拘泥于一定的原则。 你可以阅读有关此主题的罗伯特·马丁的清洁code <一个href="http://books.google.de/books?id=_i6bDeoCQzsC&pg=PT291&dq=single%20assert%20rule%20clean%20$c$c&hl=de&sa=X&ei=dBImUqWpCYSEtQaduoH4BQ&ved=0CDIQ6AEwAA#v=onepage&q=single%20assert%20rule%20clean%20$c$c&f=false">here.

First, regarding the one-assert-per-test. There is different thinking about if it's good practice or not. Fact is, it makes things overly complex when followed strictly. TDD should help you to design good and clean code, but not make you rigidly adhere to certain principles. You can read about this topic in Robert Martin's clean code here.

二,你是否希望看到用讽刺的框架对象内发生了什么,或只是断言某些价值取决于什么样的考验你正在写。

Second, whether you want to see what happens inside an object using a mocking framework or just asserting certain values depends on what kind of test you are writing.

您可以进行分类测试,在确认状态或测试,验证的行为或者测试。

You can classify tests in either tests that verify state or tests that verify behavior.

  • 对于基于状态的测试,你将检查特定的值是相关的测试,例如:如果我的add函数返回正确的值。
  • 对于您使用模拟对象来验证行为,因为它的结果往往是不平凡的验证与生产code,如基于行为的检测想想你将如何自动验证直接输出到像调制解调器或监控设备的数据。

检查Martin Fowler的优秀文章嘲弄是不是存根这个话题。

Check Martin Fowler's excellent article Mocks Aren't Stubs about this topic.

三,说实话,我不完全知道你的意思,说:

Third, to be honest I am not exactly sure what you mean by saying:

在TDD,就是它也很好的做法,这样做,如果是这种情况,那么,有效地设计使用TDD,我要结束了超过1单元测试1方法 - 因为我将有效地需要超过1断言。

In TDD, is it also good practice to do the same, if this is the case then to effectively design 1 method using TDD I am going to end up with more than 1 unit test - as I would effectively need more than 1 assert.

我假设你认为你只写你想实现一种新的方法,这将是疯狂的一个测试。试想想,实现分频功能。你将如何确保产生分裂正常工作,而且在分割的情况下由0错误?这将是不可能的。这意味着,你需要至少两个测试这种方法。

I assume that you think that you only have to write a single test of a new method you want to implement, which would be insane. Just think about implementing a divide function. How would you make sure that the division works properly and that in the case of division by 0 an error is generated? This would be impossible. That means that you need at least two tests for this method.

四,测试驱动开发测试是一个很偏的术语。一般人往往认为在TDD中定义的测试,以确保您的code不破这通常是为测试人员没有开发任务。然而,在TDD你还是认为这是一个单元测试定义一个方法或类是尚未实现的行为。

Fourth, "Test" in test-driven development is a very biased term. In general people tend to believe that in TDD you define tests to make sure that your code is not broken which is usually a task for testers not developers. However, in TDD you should rather consider that a unit test is defining behavior for a method or class that is not yet implemented.

的差异似乎微不足道,但它是一个非常强大的声明。它移动你的位置告诉程序你期待什么,然后再实际计划code一行。想想吧,让它下沉和尝试。它制造了巨大的差异。 BDD被创造出来这个想法。然而,它更。您可以获取有关BDD的详细信息,从它的发明者丹北这个的优良presentation。

The difference seems trivial, but it is a very powerful statement. It moves you in a position to tell the program what you expect before you actually program a single line of code. Think about it, let it sink in and try it. It makes a huge difference. BDD was created out of this thinking. However, it is much more. You can get more details about BDD from it's inventor Dan North in this excellent presentation.

这一切不仅是我对事物的看法,但也有很大的软件开发者的观点。所以,我希望它让你在TDD一个很好的视角,并帮助您继续你的旅程,

All of this is not only my view of things, but also the view of great software developers. So, I hope it gives you a good perspective on TDD and helps you to proceed on your journey,

这篇关于TDD:数断言,什么实际断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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