如何使用 ScalaTest 以编程方式忽略/跳过测试? [英] How to programmatically ignore/skip tests with ScalaTest?

查看:36
本文介绍了如何使用 ScalaTest 以编程方式忽略/跳过测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ScalaTest 运行一些测试,这些测试依赖于与测试服务器的连接.我目前创建了自己的与此类似的规范:

I am running some tests with ScalaTest which rely on connections to test servers to be present. I currently created my own Spec similar to this:

abstract class ServerDependingSpec extends FlatSpec with Matchers {

  def serverIsAvailable: Boolean = {
    // Check if the server is available
  }
}

当此方法返回 false 时,是否可以忽略(但不会失败)测试?

Is it possible to ignore (but not fail) tests when this method returns false?

目前我以一种hackish"的方式来做:

Currently I do it in a "hackish" way:

"Something" should "do something" in {
  if(serverIsAvailable) {
    // my test code
  }
}

但我想要类似的东西

whenServerAvailable "Something" should "do something" in { 
  // test code
}

"Something" should "do something" whenServerAvailable { 
  // test code
}

我想我应该定义我的自定义标签,但我只能参考inignore的源代码,我不明白我应该如何插入我的自定义实现.

I think I should define my custom tag, but I can only refer to the source code of in or ignore and I do not understand how I should plug in my custom implementations.

我应该如何做到这一点?

How should I accomplish this?

推荐答案

您可以使用标签来实现:

关于如何使用标签的文档:http://www.scalatest.org/user_guide/tagging_your_tests

Documentation on how to use Tags : http://www.scalatest.org/user_guide/tagging_your_tests

使用命令行参数添加和删除标记测试:http://www.scalatest.org/user_guide/using_the_runner#specifyingTagsToIncludeAndExclude

Adding and removing tagged test with command line parameters: http://www.scalatest.org/user_guide/using_the_runner#specifyingTagsToIncludeAndExclude

示例代码:

import org.scalatest.Tag

object ServerIsAvailable extends Tag("biz.neumann.ServerIsAvailable")

"Something" should "do something" taggedAs(ServerIsAvailable) in { 
  // your test here
}

运行测试

运行测试有点棘手.它仅适用于testOnlytestQuick,不适用于test.在示例中,testOnlytestOnly *

Running the tests is a bitt tricky. It only works for testOnly and testQuick not test. In the example testOnly is short for testOnly *

 sbt "testOnly -- -l biz.neumann.ServerAvailable"

这篇关于如何使用 ScalaTest 以编程方式忽略/跳过测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆