Clojure 自動化測試

2018-12-29 18:35 更新

在本章中,我們將討論由Clojure提供的自動測試選項。

測試客戶端應(yīng)用程序

為了使用測試Clojure框架,你必須使用位于的依賴項 https://github.com/slagyr/speclj#manual-installation

此URL提供了speclj框架,用作Clojure的Test數(shù)據(jù)驅(qū)動或行為驅(qū)動測試框架。 你必須確保在使用任何'speclj'庫時使用Clojure 1.7.0框架。 默認(rèn)情況下,測試文件將不同于Clojure代碼文件,需要放置在'spec'目錄中。

以下是測試文件的示例代碼。

(ns change.core-spec
   (:require [speclj.core :refer :all]))
(describe "Truth"
   (it "is true"
   (should true))
   (it "is not false"
   (should-not false)))
(run-specs)

下面的事情需要注意上面的代碼

  • 我們首先必須確保使用'require'語句來包含'speclj'框架中的所有核心庫。

  • 接下來是'describe'函數(shù)。 這用于為正在創(chuàng)建的測試用例提供描述。

  • 下一個函數(shù)是'it'函數(shù),這是實際的測試用例。 在第一個測試用例中,“is true”字符串是給測試用例的名稱。

  • 應(yīng)該和應(yīng)該不稱為斷言。 所有斷言都以should開頭。 應(yīng)該和應(yīng)該不是可用的許多斷言中的兩個。 他們都采取表達(dá)式,他們將分別檢查真實性和假性。

如果運行測試用例,您將獲得以下輸出。 輸出顯示測試用例運行所花費的時間(以毫秒為單位)。

←[32m.←[0m←[32m.←[0m
Finished in 0.00014 seconds

測試基于Web的應(yīng)用程序

Selenium是用于測試現(xiàn)代基于Web的應(yīng)用程序的關(guān)鍵框架之一。 Clojure庫也可用于測試基于Web的應(yīng)用程序。

讓我們來看看如何使用Selenium庫來測試基于Web的Clojure應(yīng)用程序。

步驟1 -第一步是確保我們使用Ring和Compojure框架來創(chuàng)建一個需要測試的基于Web的應(yīng)用程序。 讓我們使用前面章節(jié)中的一個例子。 以下代碼是一個簡單的Web應(yīng)用程序,它在瀏覽器中顯示“Hello World”。

(ns my-webapp.handler
   (:require [compojure.core :refer :all]
      [compojure.route :as route]
      [ring.middleware.defaults :refer [wrap-defaults site-defaults]]))
(defroutes app-routes
   (GET "/" [] "Hello World")
   (route/not-found "Not Found"))
(def app
   (wrap-defaults app-routes site-defaults))

第2步 -下一步確保下載selenium jar文件,并將其包含在類路徑中。 http://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server/2.47.0

第3步 -還要確保下載“clj”Web驅(qū)動程序,該驅(qū)動程序?qū)⒂糜趶囊韵挛恢眠\行Web測試。https://clojars.org/clj-webdriver/versions/0.7.1

第4步 -在您的項目目錄中,創(chuàng)建另一個名為features的目錄,并創(chuàng)建一個名為“config.clj”的文件。

第5步 -接下來,將以下代碼添加到上一步中創(chuàng)建的“config.clj”文件。

ns clj-webdriver-tutorial.features.config)
(def test-port 3000)
(def test-host "localhost")
(def test-base-url (str "http://" test-host ":" test-port "/"))

上面的代碼基本上告訴web測試框架來測試應(yīng)用程序,這是在URL加載 http://localhost:3000

第6步 -最后,讓我們編寫我們的代碼來執(zhí)行我們的測試。

(ns clj-webdriver-tutorial.features.homepage
   (:require [clojure.test :refer :all]
      [ring.adapter.jetty :refer [run-jetty]]
      [clj-webdriver.taxi :refer :all]
      [clj-webdriver-tutorial.features.config :refer :all]
      [clj-webdriver-tutorial.handler :refer [app-routes]]))
(ns clj-webdriver-tutorial.features.homepage
   (:require [clojure.test :refer :all]
      [ring.adapter.jetty :refer [run-jetty]]
      [clj-webdriver.taxi :refer :all]
      [clj-webdriver-tutorial.features.config :refer :all]
      [clj-webdriver-tutorial.handler :refer [app-routes]]))
(defn start-server []
   (loop [server (run-jetty app-routes {:port test-port, :join? false})]
      (if (.isStarted server)
         server
         (recur server))))
(defn stop-server [server]
   (.stop server))
(defn start-browser []
   (set-driver! {:browser :firefox}))
(defn stop-browser []
   (quit))
(deftest homepage-greeting
   (let [server (start-server)]
      (start-browser)
      (to test-base-url)
      (is (= (text "body") "Hello World"))
      (stop-browser)
      (stop-server server)))

上面的代碼將采取以下操作

  • 啟動應(yīng)用程序的服務(wù)器。
  • 在瀏覽器中打開根路徑。
  • 檢查頁面上是否存在“Hello World”消息。
  • 關(guān)閉瀏覽器。
  • 關(guān)閉服務(wù)器。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號