单元测试通过 [英] Unit test adoption

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

问题描述

我们已经尝试引入单元测试,以我们目前的项目,但它似乎并不奏效。额外的code似乎已经成为维护头痛当我们有我们的内部框架的变化去走一走,修复任何单元测试挂了吧。

We have tried to introduce unit testing to our current project but it doesn't seem to be working. The extra code seems to have become a maintenance headache as when our internal Framework changes we have to go around and fix any unit tests that hang off it.

我们有单元测试我们的控制器,作为模板调用到子类抽象方法的实现起到一个抽象基类,即框架调用初始化所以我们的控制器类都有自己的初始化方法。

We have an abstract base class for unit testing our controllers that acts as a template calling into the child classes' abstract method implementations i.e. Framework calls Initialize so our controller classes all have their own Initialize method.

我以前的单元测试的提倡者,但它似乎并没有工作在我们目前的项目。

I used to be an advocate of unit testing but it doesn't seem to be working on our current project.

谁能帮助确定问题,以及我们如何能够使单元测试为我们而不是反对我们工作?

Can anyone help identify the problem and how we can make unit tests work for us rather than against us?

推荐答案

测试可以是一个熊市,以保持他们是否对书面程序式code,它在很大程度上依赖于全局状态或在一个丑陋的方法体深层所在。
如果你在一个面向对象的语言编写code,使用OO构造有效地减少这种

Tips:

Avoid writing procedural code

Tests can be a bear to maintain if they're written against procedural-style code that relies heavily on global state or lies deep in the body of an ugly method. If you're writing code in an OO language, use OO constructs effectively to reduce this.


  • 在所有可能避免,如果全球状态。

  • 避免静态,因为他们往往​​通过你的codeBase的纹波,并最终导致事情是静态的不应该。他们还膨胀测试环境(见下文)。

  • 有效地利用多态性prevent <一个href=\"http://misko.hevery.com/2008/08/14/procedural-language-eliminated-gotos-oo-eliminated-ifs/\">excessive IFS和标志

  • Avoid global state if at all possible.
  • Avoid statics as they tend to ripple through your codebase and eventually cause things to be static that shouldn't be. They also bloat your test context (see below).
  • Exploit polymorphism effectively to prevent excessive ifs and flags

有在code,它更频繁地发生很大的变化比其他部分的咽喉要道。在codeBase的做到这一点,你的测试将变得更健康。

There are choke points in code that change a lot more frequently than other pieces. Do this in your codebase and your tests will become more healthy.


  • 良好的封装引出好,松耦合的设计。

  • 重构和模块化。

  • 保持测试小,集中注意力。

做任何你可以收缩测试,并在其执行周围的上下文。

Do whatever you can to shrink tests and the surrounding context in which they are executed.


  • 使用组合方法重构测试code的小块。

  • 您使用像TestNG的或JUnit4一个新的测试框架?
    它们允许你通过为您提供更细粒度挂钩到测试生命周期,以消除重复测试。

  • 调查使用测试双打(嘲弄,假货,存根),以减少测试环境的大小。

  • 调查测试数据生成器模式。

  • Use composed method refactoring to test smaller chunks of code.
  • Are you using a newer testing framework like TestNG or JUnit4? They allow you to remove duplication in tests by providing you with more fine-grained hooks into the test lifecycle.
  • Investigate using test doubles (mocks, fakes, stubs) to reduce the size of the test context.
  • Investigate the Test Data Builder pattern.

您可能无法删除所有重复,但仍试图在那里它引起疼痛将其删除。确保你不删除这么多的重复,有人不能进来,告诉测试确实一目了然的。 (见保罗惠顿的邪恶的单元测试为同一概念的另一种解释一文。)

You probably won't be able to remove all duplication, but still try to remove it where it's causing pain. Make sure you don't remove so much duplication that someone can't come in and tell what the test does at a glance. (See Paul Wheaton's "Evil Unit Tests" article for an alternative explanation of the same concept.)


  • 没有人会要修复一个测试,如果他们无法弄清楚它做什么。

  • 按照安排,法,断言模式。

  • 使用只有每个只测试一个说法。

想想参与了记录和回放Selenium测试的复杂性和什么可以在你改变与测试一个方法。

Think about the complexity involved in a record-and-playback Selenium test and what could change under you versus testing a single method.


  • 隔离相互依存关系。

  • 使用依赖注入/控制反转。

  • 使用测试双打初始化为测试对象,并确保你在独立测试code的单个单位。

  • 确保你正在编写相关测试

    • 春天的陷阱故意引入的错误,并保证它被测试抓住了。

    真正的单元测试需要完全隔离。单元测试不打一个数据库或打开插座。停在嘲笑这些相互作​​用。验证你跟你的合作者正确,而不是从这个方法调用正确的结果是42。

    True unit tests need true isolation. Unit tests don't hit a database or open sockets. Stop at mocking these interactions. Verify you talk to your collaborators correctly, not that the proper result from this method call was "42".

    这是为辩论给定的团队是否会采取测试驱动所有code,或写测试第一为code的每一行。但是,他们应该首先至少写一些测试?绝对。有场景中,测试先行无疑是接近一个问题的最佳办法。

    It's up for debate whether or not a given team will take to test-driving all code, or writing "tests first" for every line of code. But should they write at least some tests first? Absolutely. There are scenarios in which test-first is undoubtedly the best way to approach a problem.

    • Try this exercise: TDD as if you meant it (Another Description)
    • See also: Test Driven Development and the Scientific Method
查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆