【问题标题】:Scala - ScalaTest: simulate input while testingScala - ScalaTest:在测试时模拟输入
【发布时间】:2018-03-21 05:35:23
【问题描述】:

我有一个在终端上运行的应用程序。

此应用仅使用 Scala 和 SBT,我正在使用 ScalaTest 对其进行测试。

我想测试所有组件,例如集成测试,运行应用程序,但是,为此,我想模拟用户使用机器人之类的东西通过标准输入发送值。最重要的是,当我调用readLinereadInt 时,我想在测试时发送不同的值。

提前致谢!


编辑 1

所以,我有这个代码。它基本上为用户打印选项。例如,我想发送1,然后发送3,4,以创建一个新单元并检查我的 CellArray 以检查是否真的在该位置创建了一个新单元。

do {
  println("Select one of the options: \n \n"); 
  println("[1] Make a cell alive");
  println("[2] Next generation");
  println("[3] Halt");

  print("\n \n Option: ");

  option = parseOption(readLine)

}while(option == 0)

option match {
  case MAKE_CELL_ALIVE => makeCellAlive
  case NEXT_GENERATION => nextGeneration
  case HALT => halt
}

【问题讨论】:

  • 基本上,您想要做的是对程序边缘(readLine/readInt)进行测试实现,提供手动编码或生成(例如 ScalaCheck)值。将您的一些代码放在这里,我们可以尝试提供更多细节方面的帮助。

标签: scala scalatest


【解决方案1】:

一般方法将遵循这些思路。

// This is the class containing the logic you want to test
class MyInputThing {
  def readInput() = readLine

  def thingIWantToTest = {
    val input = readInput
    doStuffWithInput(input)
  }
}

// This test class returns a value representing something you want to verify.
class TestMyInputThing extends MyInputThing {
  override def readInput = "123"
}

然后,在您的测试中,使用 TestMyInputThing.thingIWantToTest() 并验证响应。您还可以将 readInput 提取到特征中,参数化 TestMyInputThing 的创建等,以清理它。我还建议您研究 ScalaCheck,这样您就无需手动编写测试场景。

【讨论】:

    猜你喜欢
    • 2021-01-07
    • 1970-01-01
    • 2017-09-24
    • 2020-10-04
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    相关资源
    最近更新 更多