iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 10
0
Modern Web

學會Elm寫前端系列 第 10

10 如何在elm寫測試?(test in elm)

test in elm

其實test 在elm裡頭和其他語言的test大同小異。不過還是簡單介紹一下常用的套件。目前市面上(至少我知道的,如果有其他套件也請推薦)是由elm-community下的 elm-test 為大宗。

安裝:

npm install -g elm-test

在你的專案的根目錄下:

elm-test init

就會幫你建好你好的,你的test的檔案如下,會有一個獨立的 tests/ 資料夾, 把你的 elm-package.json 複製一份到tests/ 裡,這樣裡頭有三個檔案。在 tests/elm-package.json 可以在加入你想要的,測試專用的東西。

.
├── elm-package.json
├── src
└── tests
    ├── Main.elm
    ├── Tests.elm
    └── elm-package.json

如果你要跑測試的話: elm test 就可以了!

來看例子吧

這是 elm-test github上的例子,直接拿來說明:

suite : Test
suite =
    describe "The String module"
        [ describe "String.reverse" -- Nest as many descriptions as you like.
            [ test "has no effect on a palindrome" <|
                \_ ->
                    let
                        palindrome =
                            "hannah"
                    in
                        Expect.equal palindrome (String.reverse palindrome)

            -- Expect.equal is designed to be used in pipeline style, like this.
            , test "reverses a known string" <|
                \_ ->
                    "ABCDEFG"
                        |> String.reverse
                        |> Expect.equal "GFEDCBA"

            -- fuzz runs the test 100 times with randomly-generated inputs!
            , fuzz string "restores the original string if you run it again" <|
                \randomlyGeneratedString ->
                    randomlyGeneratedString
                        |> String.reverse
                        |> String.reverse
                        |> Expect.equal randomlyGeneratedString
            ]
        ]

describe: String -> List Test -> Test

第一是寫個描述,再來是把你要的test 放在list 之中。

test: String -> (() -> Expectation) -> Test

第一也是個描述,再來是寫個function來test 你要的東西,譬如 \_ -> "ABCDEFG" |> String.reverse |> Expect.equal "GFEDCBA"

Expect

是一個module,像上面的例子常用的是 equal: a -> a -> Expectation

type alias Expectation = 
    Expectation

-- The result of a single test run: either a pass or a fail.

其他在Expect 常用的還有:

equal (arg2 == arg1) notEqual (arg2 /= arg1) lessThan (arg2 < arg1)
atMost (arg2 <= arg1) greaterThan (arg2 > arg1) atLeast (arg2 >=
arg1) true (arg == True) false (arg == False)

fuzz test

這個我覺得很像是在Haskell裡的 QuickCheck
:
automatic testing,也就是說他會幫你產生test,幫你測試就對啦。 不懂haskell 沒有關係,我們來看一下 fuzz
在幹麼:

fuzz : Fuzzer a -> String -> (a -> Expectation) -> Test

-- 和test比一下:

test: String -> (() -> Expectation) -> Test

看起來差不多,多了個 Fuzzer a, 這個就是神奇的地方了:

fuzz (list int) "List.length should always be positive" <|
    -- This anonymous function will be run 100 times, each time with a
    -- randomly-generated fuzzList value.
    \fuzzList ->
        fuzzList
            |> List.length
            |> Expect.atLeast 0

如果這裡你放了個 list int ,在測式的 function 裡,他就會幫你自動產生 list int (上例取了名叫
fuzzList ,你也可以改成你想要的名稱。)

我們一開始的例子是用 fuzz string ,也就是之後的function會幫我自動產生100個隨意的 string

大概就是這樣。

參考資料


上一篇
09 elm 和javascript如果合作?
下一篇
11 如何在elm裡做好delegation?
系列文
學會Elm寫前端30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言