如何在FlatSpec中跳过测试?

时间:2018-04-18 10:21:30

标签: scala unit-testing intellij-idea scalatest

我知道单元测试已经破了:

it should "be true" in {
  assert(false)
}

但我现在不想修复它,但将其标记为跳过。在测试运行的输出中应该有一个高度可见且非常简洁的警告,这就是为什么注释代码不够用。

我找到了cancel命令:

it should "be true" in {
  cancel("skipped")
  assert(false)
}

虽然通过sbt test在控制台中看起来很不错:

[info] - should be true !!! CANCELED !!!

它在IntelliJ中使用巨大的堆栈跟踪来阻塞控制台:

Test Canceled: skipped


org.scalatest.exceptions.TestCanceledException: skipped
    at org.scalatest.Assertions.newTestCanceledException(Assertions.scala:531)
    at org.scalatest.Assertions.newTestCanceledException$(Assertions.scala:530)
    at org.scalatest.FlatSpec.newTestCanceledException(FlatSpec.scala:1685)
    at org.scalatest.Assertions.cancel(Assertions.scala:1141)
    at org.scalatest.Assertions.cancel$(Assertions.scala:1137)
    at org.scalatest.FlatSpec.cancel(FlatSpec.scala:1685)
    at com.dreamlines.metronome.entities.MarshallingTest.$anonfun$new$3(MarshallingTest.scala:108)
    at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
    at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
    at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
    at org.scalatest.Transformer.apply(Transformer.scala:22)
    at org.scalatest.Transformer.apply(Transformer.scala:20)
    at org.scalatest.FlatSpecLike$$anon$1.apply(FlatSpecLike.scala:1682)
    at org.scalatest.TestSuite.withFixture(TestSuite.scala:196)
    at org.scalatest.TestSuite.withFixture$(TestSuite.scala:195)
    at org.scalatest.FlatSpec.withFixture(FlatSpec.scala:1685)
    at org.scalatest.FlatSpecLike.invokeWithFixture$1(FlatSpecLike.scala:1680)
    at org.scalatest.FlatSpecLike.$anonfun$runTest$1(FlatSpecLike.scala:1692)
    at org.scalatest.SuperEngine.runTestImpl(Engine.scala:289)
    at org.scalatest.FlatSpecLike.runTest(FlatSpecLike.scala:1692)
    at org.scalatest.FlatSpecLike.runTest$(FlatSpecLike.scala:1674)
    at org.scalatest.FlatSpec.runTest(FlatSpec.scala:1685)
    at org.scalatest.FlatSpecLike.$anonfun$runTests$1(FlatSpecLike.scala:1750)
    at org.scalatest.SuperEngine.$anonfun$runTestsInBranch$1(Engine.scala:396)
    at scala.collection.immutable.List.foreach(List.scala:389)
    at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:384)
    at org.scalatest.SuperEngine.runTestsInBranch(Engine.scala:379)
    at org.scalatest.SuperEngine.runTestsImpl(Engine.scala:461)
    at org.scalatest.FlatSpecLike.runTests(FlatSpecLike.scala:1750)
    at org.scalatest.FlatSpecLike.runTests$(FlatSpecLike.scala:1749)
    at org.scalatest.FlatSpec.runTests(FlatSpec.scala:1685)
    at org.scalatest.Suite.run(Suite.scala:1147)
    at org.scalatest.Suite.run$(Suite.scala:1129)
    at org.scalatest.FlatSpec.org$scalatest$FlatSpecLike$$super$run(FlatSpec.scala:1685)
    at org.scalatest.FlatSpecLike.$anonfun$run$1(FlatSpecLike.scala:1795)
    at org.scalatest.SuperEngine.runImpl(Engine.scala:521)
    at org.scalatest.FlatSpecLike.run(FlatSpecLike.scala:1795)
    at org.scalatest.FlatSpecLike.run$(FlatSpecLike.scala:1793)
    at org.scalatest.FlatSpec.run(FlatSpec.scala:1685)
    at org.scalatest.tools.SuiteRunner.run(SuiteRunner.scala:45)
    at org.scalatest.tools.Runner$.$anonfun$doRunRunRunDaDoRunRun$13(Runner.scala:1346)
    at org.scalatest.tools.Runner$.$anonfun$doRunRunRunDaDoRunRun$13$adapted(Runner.scala:1340)
    at scala.collection.immutable.List.foreach(List.scala:389)
    at org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:1340)
    at org.scalatest.tools.Runner$.$anonfun$runOptionallyWithPassFailReporter$24(Runner.scala:1031)
    at org.scalatest.tools.Runner$.$anonfun$runOptionallyWithPassFailReporter$24$adapted(Runner.scala:1010)
    at org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:1506)
    at org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:1010)
    at org.scalatest.tools.Runner$.run(Runner.scala:850)
    at org.scalatest.tools.Runner.run(Runner.scala)
    at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:131)
    at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:28)

如果我想跳过测试并且不想回退到标签,我还能做些什么呢?

3 个答案:

答案 0 :(得分:4)

您也可以将ignore代替in - 这样您也可以忽略带有“文字”的测试用例。

这是一个例子:

  "After running the code" should "be true" ignore {
    assert(???)
  }
  it should "be true in any case" ignore {
    assert(???)
  }

答案 1 :(得分:3)

虽然ignore将起作用,但测试根本不会运行,即使功能已修复,测试也可能会继续被忽略。既然你说你担心忘记重新开始测试,我建议pendingUntilFixed。测试仍在运行,如果断言失败,则忽略测试。但是,当功能修复并且断言通过时,测试失败,提醒您重新打开它。它是在in之后添加的,如下所示:

it should "be true" in pendingUntilFixed {
  assert(false)
}

答案 2 :(得分:2)

it关键字替换为ignore

ignore should "be true" in {
  assert(false)
}

它会将测试标记为忽略:

 [info] - should be true !!! IGNORED !!!

并且不会在IntellJ中向控制台发送垃圾邮件。

此外,虽然您可以使用cancel进行跳过,但只有在您的测试用例的前置条件失败时才能使用它。例如。如果您依赖于外部Web服务器上的功能测试并且当前无法访问它,您可能不希望测试失败但是取消它。