Sphereとともに

scalaとかplayframeworkとか。技術ブログにしたいなと。環境は Windows7x64, mac です。たまに声優さん情報が混ざります。最近ちょっとClojure触りました。

テストを充実させる

テストと一言で言っても

単体テスト結合テストシステムテスト、受け入れテスト・・・etc

色々とあるわけですが、一番効果が出るのはいったいどこなんでしょう

受け入れテストでしょうか

前職(SIer)では単体テストカバレッジ100%を強いられていましたが

100%にするだけでは、テストの意味が無く、後工程で結局「バグがぁー」ってなる。

であれば、単体テストはそこそこに実際の動きをテストするのがいいのではないかと思う今日このごろ。

実際の動きというと、ブラウザを使用して挙動を確認するということ

で、使うのが Selenium + Scalatest です。

こんな感じでスクリーンショットを取りながら自動でテストが出来ます。

import org.scalatest.{ BeforeAndAfterAll, FunSpec }
import org.scalatest.Matchers._
import org.scalatest.selenium.WebBrowser

import org.openqa.selenium.{By, Keys, WebDriver}
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.support.ui.{ ExpectedConditions, WebDriverWait }

class SampleSpec extends FunSpec
    with WebBrowser
    with BeforeAndAfterAll {
  implicit val webDriver: WebDriver = new FirefoxDriver

  describe("Stanby") {
    it("Take Screenshot") {
      setCaptureDir("./pic")

      go to ("https://jp.stanby.com/")

      pageTitle should be ("スタンバイ 日本最大級の求人検索エンジン")

      val query = "エンジニア"
      click on "q"
      textField("q").value = "エン"
      val element = webDriver.findElement(By.name("q"))
      element.sendKeys(Keys.ARROW_LEFT)
      capture to "captureX"
      enter(s"$query")
      capture to "capture3"
      submit()

      (new WebDriverWait(webDriver, 10))
        .until(ExpectedConditions.titleContains(s"${query}の求人"))

      pageTitle should be (s"${query}の求人| スタンバイ")

      capture to "capture2.png"
    }
  }

  override def afterAll(): Unit = webDriver.quit()
}

BDDについても学んで振る舞いのテストを充実させたいです。

※追記

こんな記事ありました

qiita.com

Testing Python: Applying Unit Testing, TDD, BDD and Acceptance Testing

Testing Python: Applying Unit Testing, TDD, BDD and Acceptance Testing