标签: scalatest

单元测试几个相同特征/接口的实现

我主要在scala和java中编程,在scala和junit中使用scalatest进行单元测试.我想将相同的测试应用于相同接口/特性的多个实现.我们的想法是验证是否强制执行接口契约并检查Liskov替换原则.

例如,在测试列表的实现时,测试可能包括:

  • 实例应该为空,当且仅当且仅当它的大小为零时.
  • 调用clear后,大小应为零.
  • 在列表中间添加元素,将增加一个rhs元素的索引.
  • 等等

什么是最佳做法?

java junit unit-testing scala scalatest

13
推荐指数
2
解决办法
1133
查看次数

如何将ScalaTest与Spring集成

我需要@Autowired使用Spring上下文中的字段填充我的ScalaTest测试,但是大多数Scalatest测试(例如FeatureSpecs不能由SpringJUnit4ClassRunner.class-

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="myPackage.UnitTestSpringConfiguration", loader=AnnotationConfigContextLoader.class)
public class AdminLoginTest {
    @Autowired private WebApplication app;
    @Autowired private SiteDAO siteDAO;
Run Code Online (Sandbox Code Playgroud)

(Java,但你得到了主旨).

如何@AutowiredApplicationContextScalaTest中填充字段?

class AdminLoginFeatureTest extends FeatureSpec with GivenWhenThen with ShouldMatchersForJUnit {

  @Autowired val app: WebApplication = null
  @Autowired val siteDAO: SiteDAO = null

  feature("Admin Login") {
    scenario("Correct username and password") {...}
Run Code Online (Sandbox Code Playgroud)

spring scala scalatest

13
推荐指数
1
解决办法
7268
查看次数

从sbt运行一个特定的scalatest测试

sbttest-only命令可用于运行在特定测试类中找到的测试.使用JUnit测试,您可以使用test-only在测试类上运行特定方法,例如test-only mypackage.MyTestClass.test1Equals1,仅运行该方法.

使用scalatest的更自由形式的测试语法是否可能这样做,可能是通过计算出它在内部使用的名称来引用特定的测试?如果不可能FreeSpec(由于其性质很容易想象)有没有办法用更简单的测试方法来做到这一点FunSuite

sbt scalatest

13
推荐指数
1
解决办法
1863
查看次数

Scalatest:等待断言成为现实

是否有任何存在于scalatest中等待一段时间让一个断言变为真的?

所以,例如,如果我有一个异步计算某个东西的线程,我想在我的测试中等待计算等于期望值.

scala scalatest

13
推荐指数
1
解决办法
6962
查看次数

断言值的简明方法与ScalaTest中的给定模式匹配

有没有一种很好的方法可以检查ScalaTest中的模式匹配是否成功?scalatest-users邮件列表中提供了一个选项:

<value> match {
  case <pattern> =>
  case obj => fail("Did not match: " + obj)
}
Run Code Online (Sandbox Code Playgroud)

但是,它没有组成(例如,如果我想断言列表的确切2个元素使用Inspectors API匹配模式).我可以编写一个匹配部分函数文字的匹配器,如果已定义则可以成功(如果我想在消息中获取模式,则必须是一个宏).还有更好的选择吗?

scala scalatest

13
推荐指数
1
解决办法
3105
查看次数

如何让intellij在失败的测试中提供文本差异比较

我正在用一些ScalaTest匹配器编写Scala测试.

当我的测试失败时,intellij说了类似的话

{"count":3,"pagination":{"offset":0,"limit":100},"content":{"uri":"http://locahost.com/catalogue/content?order=Query&id=18,20,19"},"list":[{"id":"18","position":27},{"id":"20","position":341},{"id":"19","position":33}]} was not equal to {"count":3,"pagination":{"offset":0,"limit":100},"content":{"uri":"http://locahost.com/catalogue/content?order=Query&id=18,20,19"},"list":[{"id":"18","timestamp":"2015-01-28T11:55:44.494Z","content":"Episode","position":27},{"id":"20","timestamp":"2015-01-19T11:55:44.494Z","content":"Program","position":341},{"id":"19","timestamp":"2015-01-17T11:55:44.494Z","content":"Episode","position":33}]}
org.scalatest.exceptions.TestFailedException: {"count":3,"pagination":{"offset":0,"limit":100},"content":{"uri":"http://locahost.com/catalogue/content?order=Query&id=18,20,19"},"list":[{"id":"18","position":27},{"id":"20","position":341},{"id":"19","position":33}]} was not equal to {"count":3,"pagination":{"offset":0,"limit":100},"content":{"uri":"http://locahost.com/catalogue/content?order=Query&id=18,20,19"},"list":[{"id":"18","timestamp":"2015-01-28T11:55:44.494Z","content":"Episode","position":27},{"id":"20","timestamp":"2015-01-19T11:55:44.494Z","content":"Program","position":341},{"id":"19","timestamp":"2015-01-17T11:55:44.494Z","content":"Episode","position":33}]}
at    org.scalatest.MatchersHelper$.newTestFailedException(MatchersHelper.scala:160)
at org.scalatest.Matchers$ShouldMethodHelper$.shouldMatcher(Matchers.scala:6231)
at org.scalatest.Matchers$AnyShouldWrapper.should(Matchers.scala:6265)
...
Run Code Online (Sandbox Code Playgroud)

然而,intellij并没有给我方便的看法文字功能的区别.

我想这可能是因为我正在比较2个物体

  val responseBody = responseAs[JsValue]
  responseBody should be(viewingByAccountIdResponseJson)
Run Code Online (Sandbox Code Playgroud)

但改变它

assert(responseBody.toString() === viewingByAccountIdResponseJson.toString())
Run Code Online (Sandbox Code Playgroud)

不允许我进行文本比较.

有没有办法配置intellij来做到这一点?

(我目前正在使用带有Matchers的FlatSpec)

注意:这与此问题格式化输出有关, 因此Intellij Idea显示两个文本的差异

但是,即使使用intellij可能会提到的语法,它也不起作用.

scala intellij-idea scalatest intellij-14

13
推荐指数
1
解决办法
977
查看次数

如何使用ScalaTest覆盖Playframework单元测试中的guice模块

我想在PlayFramework中为我的控制器编写功能测试.为此,我想模拟一些类的实现.

我在这里找到了如何使用spec2做到这一点的很好的例子:http://www.innovaedge.com/2015/07/01/how-to-use-mocks-in-injected-objects-with-guiceplayscala/

但我正在使用带有OneAppPerSuite使用FakeApplication的特性的scala测试.以下是文档:https: //www.playframework.com/documentation/2.4.x/ScalaFunctionalTestingWithScalaTest

问题是我无法找到一种方法来拦截GuiceApplicationBuilder和覆盖一些模拟实现的绑定.

以下是FakeApplication实施play.api.test:

case class FakeApplication(
  override val path: java.io.File = new java.io.File("."),
  override val classloader: ClassLoader = classOf[FakeApplication].getClassLoader,
  additionalPlugins: Seq[String] = Nil,
  withoutPlugins: Seq[String] = Nil,
  additionalConfiguration: Map[String, _ <: Any] = Map.empty,
  withGlobal: Option[play.api.GlobalSettings] = None,
  withRoutes: PartialFunction[(String, String), Handler] = PartialFunction.empty) extends Application {

private val app: Application = new GuiceApplicationBuilder()
  .in(Environment(path, classloader, Mode.Test))
  .global(withGlobal.orNull)
  .configure(additionalConfiguration)
  .bindings(
    bind[FakePluginsConfig] to FakePluginsConfig(additionalPlugins, withoutPlugins), …
Run Code Online (Sandbox Code Playgroud)

unit-testing scala guice scalatest playframework

13
推荐指数
1
解决办法
976
查看次数

如何让SBT重新运行失败的测试

有没有办法让SBT重新运行测试套件最后一次运行失败的测试?例如,如果我运行sbt test并且20次测试中有3次运行失败,是否有任何命令我可以运行让SBT重新运行那些失败的3个测试?

具体来说,我正在使用Scala Test和Scala Check来测试我通过SBT运行的测试.

unit-testing scala sbt scalatest scalacheck

13
推荐指数
1
解决办法
3602
查看次数

SBT测试错误:java.lang.NoSuchMethodError:net.jpountz.lz4.LZ4BlockInputStream

获得以下异常,当我尝试使用scalatest在SBT窗口上执行我的火花流代码的单元测试时.

sbt testOnly <<ClassName>>

*
*
*
*
*
*

2018-06-18 02:39:00 ERROR执行程序:91 - 阶段3.0(TID 11)中的任务1.0中的异常java.lang.NoSuchMethodError:net.jpountz.lz4.LZ4BlockInputStream.(Ljava/io/InputStream; Z)V位于org.apache.spark.serializer.SerializerManager.wrapStream的org.apache.spark.io.LZ4CompressionCodec.compressedInputStream(CompressionCodec.scala:122)org.apache.spark.serializer.SerializerManager.wrapForCompression(SerializerManager.scala:163)at org.apache.spark.serializer.SerializerManager.wrapStream (SerializerManager.scala:124)在org.apache.spark.shuffle.BlockStoreShuffleReader $$ anonfun $ 2.适用(BlockStoreShuffleReader.scala:50)在org.apache.spark.shuffle.BlockStoreShuffleReader $$ anonfun $ 2.适用(BlockStoreShuffleReader.scala :50)在org.apache.spark.storage.ShuffleBlockFetcherIterator.next(ShuffleBlockFetcherIterator.scala:417)在org.apache.spark.storage.ShuffleBlockFetcherIterator.next(ShuffleBlockFetcherIterator.scala:61)在scala.collection.Iterator $$匿名在scala上的scala.collection.Iterator $$ anon $ 12.hasNext(Iterator.scala:441)$ 12.nextCur(Iterator.scala:435).collection.Iterator $$匿名$ 11.hasNext(Iterator.scala:409)在org.apache.spark.util.CompletionIterator.hasNext(CompletionIterator.scala:32)在org.apache.spark.InterruptibleIterator.hasNext(InterruptibleIterator.scala: 37)在scala.collection.Iterator $$不久$ 11.hasNext(Iterator.scala:409)在org.apache.spark.sql.catalyst.expressions.GeneratedClass $ GeneratedIteratorForCodegenStage1.sort_addToSorter $(来源不明)在org.apache.spark .sql.catalyst.expressions.GeneratedClass $ GeneratedIteratorForCodegenStage1.processNext(来源不明)在org.apache.spark.sql.execution.BufferedRowIterator.hasNext(BufferedRowIterator.java:43)在org.apache.spark.sql.execution.WholeStageCodegenExec $ $ anonfun 10 $ $$匿名$ 1.hasNext(WholeStageCodegenExec.scala:614):在org.apache.spark.sql.execution.streaming在org.apache.spark.sql.execution.GroupedIterator $.适用(29 GroupedIterator.scala) .FlatMapGroupsWithStateExec $ StateStoreUpdater.updateStateForKeysWithData(FlatMapGroupsWithStateExec.scala:176)**

尝试了几件事来排除net.jpountz.lz4 jar(来自其他帖子的建议)但输出中的同样错误.

目前使用spark 2.3,scalatest 3.0.5,Scala 2.11版本.我在升级到spark 2.3和scalatest 3.0.5后才看到这个问题

有什么建议 ?

scala sbt scalatest apache-spark spark-streaming

13
推荐指数
1
解决办法
6106
查看次数

scalatest:对象scalatest不是包org的成员

编辑:如果文件在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}

我已经尝试过了:

  • sbt清理更新
  • 重启+使intellij的缓存无效
  • 删除.idea /并重新导入
  • libraryDependencies + ="org.scalatest"%"scalatest_2.10"%"2.0"%"test"而不是ogf实际
  • 我的键盘上有神奇的仪式

什么都行不通

任何的想法 ?

scala intellij-idea sbt scalatest

12
推荐指数
1
解决办法
1万
查看次数