注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

阿弥陀佛

街树飘影未见尘 潭月潜水了无声 般若观照心空静...

 
 
 

日志

 
 
关于我

一直从事气象预报、服务建模实践应用。 注重气象物理场、实况场、地理信息、本体知识库、分布式气象内容管理系统建立。 对Barnes客观分析, 小波,计算神经网络、信任传播、贝叶斯推理、专家系统、网络本体语言有一定体会。 一直使用Java、Delphi、Prolog、SQL编程。

网易考拉推荐

ScalaTest quick start  

2014-07-04 23:09:11|  分类: Scala |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

ScalaTest quick start

To get started with ScalaTest, copy this FlatSpec into a file named ExampleSpec.scala:

import collection.mutable.Stack
import org.scalatest._

class ExampleSpec extends FlatSpec with Matchers {

  "A Stack" should "pop values in last-in-first-out order" in {
    val stack = new Stack[Int]
    stack.push(1)
    stack.push(2)
    stack.pop() should be (2)
    stack.pop() should be (1)
  }

  it should "throw NoSuchElementException if an empty stack is popped" in {
    val emptyStack = new Stack[Int]
    a [NoSuchElementException] should be thrownBy {
      emptyStack.pop()
    } 
  }
}

In a FlatSpec, you combine tests with text that specifies the behavior being tested. Each test name specifies a bit of behavior about a subject under test. You start by identifying the subject with a string name (here, "A Stack"), write should, then write another string that describes the behavior expected of the subject. The code of the test appears in curly braces after an in following the text.

You can compile this FlatSpec like this (using this Jar file):

$ scalac -cp scalatest_2.11-2.2.0.jar ExampleSpec.scala

Here's how you run it:

$ scala -cp scalatest_2.11-2.2.0.jar org.scalatest.run ExampleSpec
Discovery starting.
Discovery completed in 21 milliseconds.
Run starting. Expected test count is: 2
ExampleSpec:
A Stack
- should pop values in last-in-first-out order
- should throw NoSuchElementException if an empty stack is popped
Run completed in 76 milliseconds.
Total number of tests run: 2
Suites: completed 1, aborted 0
Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
All tests passed.

To get going on a project, first select a style for unit testing. If you don't have a preference, we recommend starting with FlatSpec for unit testing. We think FlatSpec is a good default choice because it has a flat structure, similar to the XUnit-style tests familiar to most users, but encourages a BDD-style of using descriptive specifications of behavior for test names.

(For a quick guide to ScalaTest's style traits, see the Style trait use cases table in the user guide.)

Once you've selected your unit testing style, create an abstract base class (not a trait) for your unit tests. You should mix into this class all the traits you think you'll use a lot. You can use any name, but we recommend the name UnitSpec. Here's an example:

package com.mycompany.myproject

import org.scalatest._

abstract class UnitSpec extends FlatSpec with Matchers with
  OptionValues with Inside with Inspectors

You can now write unit tests for your project using your custom base class:

package com.mycompany.myproject

import org.scalatest._

class MySpec extends UnitSpec {
  // Your tests here
}

To include ScalaTest in your sbt project, simply add this line:

libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.0" % "test"

To include ScalaTest in your Maven project, use:

<dependency>
  <groupId>org.scalatest</groupId>
  <artifactId>scalatest_2.11</artifactId>
  <version>2.2.0</version>
  <scope>test</scope>
</dependency>

You are off and running! For more information, consult the user guide.

 

 

 

 

 

 

 

 

ScalaTest is brought to you by Bill Venners, with contributions from several other folks. It is sponsored by Artima, Inc.
ScalaTest is free, open-source software released under the Apache 2.0 license.

Copyright ? 2009-2013 Artima, Inc. All Rights Reserved.

artima

  评论这张
 
阅读(363)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017