日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ScalaTest学习笔记(一)

發布時間:2024/4/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ScalaTest学习笔记(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

最近在看ScalaTest,整理一下筆記,將常用的代碼貼出來。說明一下,ScalaTest可以選擇多種風格(不同的Spechttp://www.scalatest.org/user_guide/selecting_a_style),我參考書里面用的FunSpec,大家可以自己選擇。如果不知道如何選擇的話

If you would rather be told which approach to take rather than pick one yourself, we recommend you use?FunSpec?for unit testing andFeatureSpec?for integration or acceptance testing.

參考資料

  • 《TestingInScala》http://ishare.iask.sina.com.cn/f/36122035.html
  • http://www.scalatest.org/,順便說一下官網的Manual永遠是最權威最好的學習資料,很多書籍都比較舊,很多差異的
  • package com.oreilly.testingscalaimport org.scalatest.{FunSpec, GivenWhenThen, Tag} import org.scalatest.matchers.ShouldMatchersclass Point (val x:Int, val y:Int) {require(x > 0, "x should > 0") }class TestingInScala extends FunSpec with ShouldMatchers with GivenWhenThen {describe("Testing in Scala example") {it ("Different Matchers") {}it("simple matchers") {val list = 2::4::5::Nillist.size should be (3)//必須在()中,否則compile errorlist.size should equal (3)// 跟be一樣,不能用==或者!=,3 == 5會執行得到false,但是不會導致test failed}it("String matchers") {val string = """I fell into a burning ring of fire.I went down, down, down and the flames went higher"""string should startWith ("I fell")string should not endWith ("I fell")string should include ("down, down, down")string should startWith regex "I.fel+"string should not endWith regex ("\\d{5}")string should fullyMatch regex ("""I(.|\n|\s)*higher""")//.不包括所有字符?}it("Ralational operator matchers") {val num = 20num should be < (30)num should not be > (30)num should be === (20)// 不能用==num should not equal(21)num should be >(0)}it("Floating matchers") {(0.9 - 0.8) should be (0.1 plusOrMinus 0.01)}it("Reference matchers") {val list_1 = List(5)val list_2 = list_1val list_3 = List(5)// 都用List()不行,因為都是'empty,是同一個對象list_1 should be theSameInstanceAs (list_2)list_1 should not be theSameInstanceAs (list_3)}it("Iterable matchers") {List() should be ('empty)// scala.Symbol8::7::6::5::Nil should contain (7)}it("Seq and traversable matchers") {(1 to 9) should have length (9)(20 to 60 by 2) should have size (21)}it("Map matchers") {val map = Map("Jimmy Page" -> "Led Zeppelin", "Sting" -> "The Police", "Aimee Mann" -> "Til\' Tuesday")map should contain key ("Sting")map should contain value ("Led Zeppelin")map should not contain key ("magic")}it("Compound matchers") {val redHotChiliPeppers = List("Anthony Kiedis", "Flea", "Chad Smith", "Josh Klinghoffer")redHotChiliPeppers should (contain("Anthony Kiedis") and (not contain ("John Frusciante") or contain("Dave Navarro")))redHotChiliPeppers should not (contain ("The Edge") or contain ("Kenny G"))//and和or以及右邊的斷言必須被()包住info("and/or不像&&或者||不會短路")val numT = 20var total = 3numT should (equal(20) or equal {total += 6})total should be(9)}it("null test") {var nullList:List[Int] = null//nullList should (not be (null) and contain (5))// 會有NullPointerException,用如下寫法則是test failed//nullList should not be (null)//nullList should contain (5)}it("Property matchers") {val point = new Point(1, 2)point should have ('x(1), 'y(2))}it("java.util.Collection matchers") {//java的collection和scala里面的collection測試方式一樣import java.util.{List => JList, ArrayList => JArrayList, Map => JMap, HashMap => JHashMap}val jList:JList[Int] = new JArrayList[Int](20)jList.add(1)jList.add(2)jList.add(3)jList should have size (3)jList should have length (3)// size和length一樣,看自己喜歡用哪一個,并且初始化的20并不表示sizejList should contain (1)jList should not contain (4)val emptyJList:JList[Int] = new JArrayList[Int]emptyJList should be ('empty)val jMap:JMap[String, Int] = new JHashMapjMap.put("one", 1)jMap.put("two", 2)jMap.put("three", 3)jMap should contain key ("one")jMap should contain value (1)jMap should not contain key ("four")}info("Must matchers跟should一樣,只是用must關鍵字,同時應該with MustMatchers")it("Exception Handling") {info("use intercept...")val thrown = intercept[IllegalArgumentException] {// http://www.scalatest.org/user_guide/using_assertionsval point = new Point(-1, 2)}thrown.getMessage should be ("requirement failed: x should > 0")info("use evaluating...")val thrownException = evaluating {new Point(-1, 2)} should produce[IllegalArgumentException]thrownException.getMessage should be ("requirement failed: x should > 0")}it("GivenWhenThen實際上是informer,可用于組織測試結構") {given("create a Point")val point = new Point(1, 2)when("point.x get")val x = point.xthen("x isInstanceOf[Int]")x.isInstanceOf[Int] should be (true)and("x == 1")x should equal (1)}it("pending......") {// 貌似現在是pending之后的不會執行,之前的會執行,如果info下面的true改為false,會test failed,但是pending之后的不會info("下面的測試不會執行,但是info會輸出")1 == 1 should be (true)pending1 == 1 should be (false)}ignore("ignore...") {// 要執行,將ignore改為it即可info("ignore的info不會輸出...")}it("tag...", Tag("point")) {info("可以用-- -n point只執行有point的Tag,用-l則不執行point Tag,sbt目前只能用它testOnly來運行tag")}} }

    轉載于:https://my.oschina.net/magicly007/blog/159277

    總結

    以上是生活随笔為你收集整理的ScalaTest学习笔记(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。