English 中文(简体)
我如何为大多数面向服务的评估撰写有用的单位测试?
原标题:How do I write useful unit tests for a mostly service-oriented app?
  • 时间:2009-10-14 17:53:51
  •  标签:

我成功地进行了一段时间的单位测试,但我开始认为,这些测试仅对实际执行相当数量逻辑的班级/方法有用,而教职员工做数学、复杂的业务逻辑,所有优秀的考试候选人都无问题。 我真的要竭力说明如何为另一类物体使用测试:这些物体主要通过代表团操作。

案例:我目前的项目协调了许多数据库和服务。 大多数课程只是收集服务方法,大多数方法具有某种基本条件逻辑,可能是一种旁观,然后是利用其他服务。

like像这样的物体,实际上是唯一可行的检测战略,因此,我为其中几个人精心设计了 mo。 我确实不喜欢它,原因如下:

  1. Using mocks to specify expectations for behavior makes things break whenever I change the class implementation, even if it s not the sort of change that ought to make a difference to a unit test. To my mind, unit tests ought to test functionality, not specify "the methods needs to do A, then B, then C, and nothing else, in that order." I like tests because I am free to change things with the confidence that I ll know if something breaks - but mocks just make it a pain in the ass to change anything.
  2. Writing the mocks is often more work than writing the classes themselves, if the intended behavior is simple.
  3. Because I m using a completely different implementation of all the services and component objects in my test, in the end, all my tests really verify is the most basic skeleton of the behavior: that "if" and "for" statements still work. Boring. I m not worried about those.

The core of my application is really how all the pieces work together, so I m considering ditching unit tests altogether (except for places where they re clearly appropriate) and moving to external integration tests instead - harder to set up, coverage of less possible cases, but actually exercise the system as it is mean to be run.

我看不出任何使用cks子的情况确实有用。

想法?

问题回答

If you can write integration tests that are fast and reliable, then I would say go for it. Use mocks and/or stubs only where necessary to keep your tests that way.

然而,正如你所描述的那样,使用cks子并不一定是痛苦的:

  1. Mocking APIs let you use loose/non-strict mocks, which will allow all invocations from the unit under test to its collaborators. Therefore, you don t need to record all invocations, but only those which need to produce some required result for the test, such as a specific return value from a method call.
  2. With a good mocking API, you will have to write little test code to specify mocking. In some cases you may get away with a single field declaration, or a single annotation applied to the test class.
  3. You can use partial mocking so that only the necessary methods of a service/component class are actually mocked for a given test. And this can be done without specifying said methods in strings.

To my mind, unit tests ought to test functionality, not specify "the methods needs to do A, then B, then C, and nothing else, in that order."

我同意。 如你发现的那样,用cks子进行 Behavior测会导致碎片测试。 以国家为基础的、带有障碍的检测减少了这一问题。 Fowler weighs in this in Mocks Aren t Stubs

Writing the mocks is often more work than writing the classes themselves

对于 mo或 st,考虑采用隔离(冲击)框架。

in the end, all my tests really verify is the most basic skeleton of the behavior: that "if" and "for" statements still work

分支和通道是逻辑性的;我建议对其进行测试。 我认为没有必要测试采集器和设计器、一线纯代表团方法等等。

一体化测试对于像你这样的综合系统非常宝贵。 除单位测试外,我建议进行这些测试,而不是进行测试。

你们无疑想要测试你低级或堆肥服务背后的班级;你在哪里看到大桶。

EDIT: Fowler不使用“阶级”一词,即我认为它的方式(可能意味着我错)。 当我谈到基于国家的测试时,我指的是在接受测试的受抚养人类别中注入的 st,然后对受测试的类别表示反对。 在纯粹的案例中,我不会对 st子做任何核查。

Integration Tests是这方面可行的选择,但不应取代单位测试。 但是,既然你讲了你的写作 mo,我建议采用一个《孤立框架》,我确信,这个框架也能够为你们的环境提供。

既然你把几个问题放在一起回答。

我如何为大多数面向服务的评估撰写有用的单位测试?

不要依赖单位测试“最面向服务”! 是的,我在一句中说。 这些类型的器具意在做一件事:整合服务。 因此,更迫切的是,你要撰写融合测试,而不是进行单位测试,以证明一体化工作是正确的。

我看不出任何使用cks子的情况确实有用。

cks可能非常有用,但我不会在控制器上使用。 主计长应当接受合并测试。 <can>/em>服务包含在单位测试中,但如果检测量放缓,则作为单独的单元进行,可能是明智的。

想法?

对我来说,我倾向于考虑几个方面:

  1. What is my application doing?
  2. How expensive would it be to perform system level / integration tests?
  3. Can I split my application up into modules that can be tested separately?

在你提供的假想中,我说你的申请is包含许多服务。 因此,I d lean对单位测试的一体化测试进行了大量研究。 我是你们撰写的文章中大多数是用于相关班级的。

• 尽可能进行更大范围的一体化/系统级测试,原因如下:

  1. In this day and age of "moving fast", re-factoring the designs of yesterday happens at an ever increasing rate. Integration tests aren t concerned about implementation details at all so this facilitates rapid change. Dynamic languages are in full swing making mocks even more dangerous / brittle. With a static lang, mocks are much safer because your tests won t compile if they re trying to stub out a non existent or misspelled method name.
  2. The amount of code written in an integration test is usually 60% less than the amount of code written in a unit test to achieve the same level of coverage so development time is less. "Yes but it takes longer to run integration tests..." that s where you need to be pragmatic until it actually slows you down to run integration tests.
  3. Integration tests catch more bugs. Mocking is often contrived and removes the developer from the realities of what their changes will do to the application as a whole. I ve allowed way more bugs into production under the "safety net" of 100% unit test coverage than I would have with integration tests.
  4. If integration testing is slow for my application then I haven t split it up into separate modules. This is often an indicator early on that I need to do some extracting into separation.
  5. Integration tests do way more for you than reach code coverage, they re also an indicator of performance issues or network problems etc.




相关问题
热门标签