【问题标题】:Acceptance style testing in favour of unit style testing with Specs2?验收风格测试有利于使用 Specs2 进行单元风格测试?
【发布时间】:2012-11-19 11:25:40
【问题描述】:

在 Specs2 的上下文中,纯粹主义者可能会争辩说应该使用单元测试风格来进行单元测试。 Specs2 验收测试风格用于进行验收测试。这听起来有点明显;-)

但是,我甚至喜欢编写单元测试的验收测试风格(主要是为了保持一致性)。有什么技术原因我不应该这样做吗?

我只是喜欢以相同的风格编写所有测试的一致性,并且单元测试风格对于我的项目所有者(不是技术人员)来说有点难以驾驭。虽然验收测试风格允许他们在偶然发现缺失的功能时添加新的测试,例如:

"Cool new feature" ! todo ^

从下面的示例中您可以看到(改编自 Specs2 site),验收测试风格对于非极客来说更具可读性,并且可以更好地分离关注点,尤其是在规范变得更大时。此外,它可能会导致更多的作曲风格或写作测试。

单元测试风格:

  import org.specs2.mutable._

  class HelloWorldSpec extends Specification {

    "The 'Hello world' string" should {
      "contain 11 characters" in {
        "Hello world" must have size(11)
      }
      "start with 'Hello'" in {
        "Hello world" must startWith("Hello")
      }
      "end with 'world'" in {
        "Hello world" must endWith("world")
      }
    }
  }

验收测试风格:

 import org.specs2._

  class HelloWorldSpec extends Specification { def is =

    "This is a specification to check the 'Hello world' string"       ^
                                                                      p^
    "The 'Hello world' string should"                                 ^
      "contain 11 characters"                                         ! e1^
      "start with 'Hello'"                                            ! e2^
      "end with 'world'"                                              ! e3^
      "do something cool"                                             ! todo^
      "do something cooler"                                           ! todo^
                                                                      end

    def e1 = "Hello world" must have size(11)
    def e2 = "Hello world" must startWith("Hello")
    def e3 = "Hello world" must endWith("world")
  }

谁知道,有一天,甚至可能以使用字符串插值(或其他东西)和附加解析例程的更具可读性的 DSL 结束多个文件:

HelloTest.specs2

  s"
  This is a specification to check the 'Hello world' string
  =========================================================

  The 'Hello world' string should
  -------------------------------
  - $e1 contain 11 characters
  - $e2 && $e3 start with 'Hello' and end with 'world'
  - $todo do something cool
  - $todo do something cooler
  "

MyAppSpec2.scala

 import org.specs2._

  class HelloWorldSpec extends Specification { def is = HelloTest.specs2

    def e1 = "Hello world" must have size(11)
    def e2 = "Hello world" must startWith("Hello")
    def e3 = "Hello world" must endWith("world")
  }

【问题讨论】:

    标签: scala specs2


    【解决方案1】:

    没有技术原因不能使用“Acceptance”样式编写单元测试,相反使用“Unit”样式编写验收测试(因为您可以使用带有代码折叠功能的编辑器仅显示文本, 或者您可以使用 plan 参数执行规范并获取规范的全文而不执行示例)。

    您需要注意的两个“技术”事项:

    • “单元”规范使用变量来注册示例,因此如果您尝试使用不同的线程构建这样的规范,它可能会出现并发问题

    • “接受”规范使用示例的最后一个值作为结果。因此,默认情况下,当您想在示例中每行编写一个期望时,它不太方便。要解决此问题,您需要混合 ThrownExpectation 特征或在期望之间使用 and 运算符

    最后,当 Scala 2.10 发布时,我肯定会研究使用字符串插值作为验收规范的可能性。

    【讨论】:

    • 感谢 Eric,关于第二点:是否可以在规范文本中执行“和”操作作为替代解决方法,例如(伪代码)。 “我的测试”! e1 && e2 ^.恕我直言,这组合得更好,例如,您可以将 e1 与 e3 一起使用。
    • 我从来没有使用过这个,但记住这一点确实很好。
    • 对不起,我意识到这是一个问题,而不是一个肯定!答案是肯定的。你可以写:“我的测试”! (e1 and e2) ^... 你只需要加上括号。
    • 啊哈,谢谢埃里克!我阅读了文档但不知何故错过了;-)
    猜你喜欢
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多