我是从ScalaTest测试java代码的单元,并希望在声明它的同一语句中填充java.util.HashMap.可以在Scala中执行此操作吗?
Maven surefire-plugin不运行集成测试(按照惯例命名为"IT"后缀),但sbt同时运行:unit和integration.那么,如何防止这种行为呢?是否存在区分ScalaTest的集成和单元测试的常用方法(默认情况下不运行FeatureSpec-tests)
当涉及隐式参数时,我在尝试理解如何在Scala中编写测试时遇到了一些困难.
我有以下(简短版本)我的代码和测试:
实施(Scala 2.10,Spray和Akka):
import spray.httpx.SprayJsonSupport._
import com.acme.ResultJsonFormat._
case class PerRequestIndexingActor(ctx: RequestContext) extends Actor with ActorLogging {
def receive = LoggingReceive {
case AddToIndexRequestCompleted(result) =>
ctx.complete(result)
context.stop(self)
}
}
object ResultJsonFormat extends DefaultJsonProtocol {
implicit val resultFormat = jsonFormat2(Result)
}
case class Result(code: Int, message: String)
Run Code Online (Sandbox Code Playgroud)
测试(使用ScalaTest和Mockito):
"Per Request Indexing Actor" should {
"send the HTTP Response when AddToIndexRequestCompleted message is received" in {
val request = mock[RequestContext]
val result = mock[Result]
val perRequestIndexingActor = TestActorRef(Props(new PerRequestIndexingActor(request)))
perRequestIndexingActor ! AddToIndexRequestCompleted(result) …Run Code Online (Sandbox Code Playgroud) 在使用浮点数或包含浮点数的对象(如向量或矩阵)编写操作的测试时,我经常想要测试不是为了相等,而是为了"几乎相等"(差异允许是某些epsilon).
使用ScalaTest FunSuite时,通常会写入assert(xxx == yyy).随着浮动和喜欢我可以写assert(math.abs(xxx - yyy)<epsilon),但后来我没有得到ScalaTest断言宏的好功能,报告比较值作为失败消息的一部分.
如何在ScalaTest中执行float"几乎相等"的测试,以便在测试失败时将值写为失败消息的一部分?
测试示例:
import org.scalatest.FunSuite
class FloatTest extends FunSuite {
test("Testing computations") {
import math._
assert(sin(Pi/4)==sqrt(0.5))
assert(sin(Pi)==0)
}
}
Run Code Online (Sandbox Code Playgroud) 编辑:如果文件在src/test/scala/tests /但不在src/main/scala/mypackage /为什么?
我尝试过主题的解决方案,人们有几乎相同的问题但没有效果.
详细说明,我在build.sbt中有这个:
libraryDependencies ++= Seq(
...
"org.scalatest" % "scalatest_2.10" % "2.2.1" % "test",
...
Run Code Online (Sandbox Code Playgroud)
在intellij中有一个类:
import org.scalatest.{BeforeAndAfterAll, Suite}
Run Code Online (Sandbox Code Playgroud)
{BeforeAndAfterAll,Suite}为红色,所以我觉得scalatest找到了
sbt包也不起作用:
object scalatest不是包org [error] import org.scalatest的成员.{BeforeAndAfterAll,Suite}
我已经尝试过了:
什么都行不通
任何的想法 ?
我的原始代码还有很多,这让我分心了问题的真正原因.这抓住了基本问题.
import org.scalatest.AsyncFlatSpec
import scala.concurrent.Future
class AsyncFlatSpecSpec extends AsyncFlatSpec
{
it should "parse an XML file" in {
// ... Parsing ...
Future.successful(succeed)
}
it should "parse an XML file" in {
// ... Serializing ...
Future.successful(succeed)
}
}
Run Code Online (Sandbox Code Playgroud)
这产生了以下错误:
[info] DeferredAbortedSuite:
[error] Uncaught exception when running AsyncFlatSpecSpec: java.lang.ArrayIndexOutOfBoundsException: 17
[trace] Stack trace suppressed: run last test:testOnly for the full output.
Run Code Online (Sandbox Code Playgroud)
我的代码中没有任何数组访问发生.这是怎么回事?
运行"最后一次测试:testOnly"没有多大帮助:
[info] DeferredAbortedSuite:
[error] Uncaught exception when running AsyncFlatSpecSpec: java.lang.ArrayIndexOutOfBoundsException: 17
sbt.ForkMain$ForkError: java.lang.ArrayIndexOutOfBoundsException: 17
at org.scalatest.exceptions.StackDepth$class.stackTraceElement(StackDepth.scala:63)
at org.scalatest.exceptions.StackDepth$class.failedCodeFileName(StackDepth.scala:77) …Run Code Online (Sandbox Code Playgroud) 我需要检查一个字符串是否包含许多子字符串.以下作品
string should include ("seven")
string should include ("eight")
string should include ("nine")
Run Code Online (Sandbox Code Playgroud)
但它需要三条几乎重复的线条.我正在寻找类似的东西
string should contain allOf ("seven", "eight", "nine")
Run Code Online (Sandbox Code Playgroud)
但这不起作用...断言只是失败,而字符串肯定包含这些子串.
我怎样才能在一行中执行这样的断言?
我有以下测试类:
import org.scalatest.FunSuite
@RunWith(classOf[JUnitRunner])
class NodeScalaSuite extends FunSuite {
Run Code Online (Sandbox Code Playgroud)
有了这个测试方法:
test("Now doesn't terminate future that's not done") {
val testFuture: Future[Int] = Future{
wait(1000)
10
}
assertThrows[NoSuchElementException]{
testFuture.now
}
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
not found: value assertThrows
Run Code Online (Sandbox Code Playgroud)
我查看了http://doc.scalatest.org/3.0.0/#org.scalatest.FunSuite上的ScalaTest文档,与我类似的代码似乎工作正常.
有什么问题?
我想默认禁用标记为"慢"的某些自动化测试,但允许用户使用简单的命令行启用它们的执行.我想这是一个非常常见的用例.
鉴于此测试套件:
import org.scalatest.FunSuite
import org.scalatest.tagobjects.Slow
class DemoTestSuite extends FunSuite {
test("demo test tagged as slow", Slow) {
assert(1 + 1 === 2)
}
test("demo untagged test") {
assert(1 + 1 === 2)
}
}
Run Code Online (Sandbox Code Playgroud)
默认情况下,sbt test将运行标记和未标记的测试.
如果我将以下内容添加到build.sbt:
testOptions in Test += Tests.Argument("-l", "org.scalatest.tags.Slow")
Run Code Online (Sandbox Code Playgroud)
然后,我得到了我想要的默认行为,其中未标记的测试运行,并且慢速标记的测试将不会运行.
但是,当我想运行它们时,我无法弄清楚将运行慢速测试的命令行选项.我做了几次搜索并尝试了几个例子.我有点惊讶,因为这似乎是一种非常常见的情况.
我可以跑:
runrun...run/debug configuration> +>的junit> test kind = all in package>search for tests = in whole project但是,如果我使用scala测试(+> scala测试>测试种类......)执行相同的(2),第一次测试失败非常奇怪(似乎object没有实例化),而相同的测试通过,如果我使用它运行它(1)或(3)
配置:
奖金问题:如何运行所有测试(junit和scalatest)?