当前位置: 首页 > 知识库问答 >
问题:

如何使“测试”类在“IT”(集成测试)配置中可用?

刘绍晖
2023-03-14

下面是一个最小的例子:

project/build.scala

import sbt._
import Keys._

object MyBuild extends Build {

  val scalaTest = "org.scalatest" %% "scalatest" % "2.0" % "test,it"

  lazy val myProject =
    Project(id = "my-project", base = file("."))
      .configs(IntegrationTest)
      .settings(Defaults.itSettings: _*)
      .settings(
        scalaVersion := "2.10.3",
        libraryDependencies ++= Seq(
          scalaTest
        )
      )
}

src/test/scala/helpers.scala

trait Helper {
  def help() { println("helping.") }
}
import org.scalatest._

class TestSuite extends FlatSpec with Matchers with Helper {
  "My code" should "work" in {
    help()
    true should be(true)
  }
}
import org.scalatest._

class ItSuite extends FlatSpec with Matchers with Helper {
  "My code" should "work" in {
    help()
    true should be(true)
  }
}

然后,在sbt中,“test”起作用:

sbt> test
helping.
[info] TestSuite:
[info] My code
[info] - should work
[info] Run completed in 223 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 0 s, completed Dec 17, 2013 1:54:56 AM

但是“it:test”不能编译:

sbt> it:test
[info] Compiling 1 Scala source to ./target/scala-2.10/it-classes...
[error] ./src/it/scala/ItSuite.scala:3: not found: type Helper
[error] class ItSuite extends FlatSpec with Matchers with Helper {
[error]                                                   ^
[error] ./src/it/scala/ItSuite.scala:5: not found: value help
[error]     help()
[error]     ^
[error] two errors found
[error] (it:compile) Compilation failed
[error] Total time: 1 s, completed Dec 17, 2013 1:55:00 AM

共有1个答案

钱旻
2023-03-14

如果您希望共享来自test配置的代码,最好从test创建自定义测试配置。请参见自定义测试配置。

您的项目/build.scala变为:

import sbt._
import Keys._

object MyBuild extends Build {
  lazy val FunTest = config("fun") extend(Test)

  val scalaTest = "org.scalatest" %% "scalatest" % "2.0" % "test"

  lazy val myProject =
    Project(id = "my-project", base = file("."))
      .configs(FunTest)
      .settings(inConfig(FunTest)(Defaults.testSettings) : _*)
      .settings(
        scalaVersion := "2.10.3",
        libraryDependencies ++= Seq(
          scalaTest
        )
      )
}

还将src/it/重命名为src/fun/。现在乐趣:测试工作:

> fun:test
helping.
[info] ItSuite:
[info] My code
[info] - should work
[info] Run completed in 245 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 1 s, completed Dec 17, 2013 8:43:17 AM
 类似资料:
  • 我正在使用Spring Boot(打包到没有SpringBoot运行程序的经典WAR),我想在Spock中实现集成测试。当我使用时,只使用标准Spring上下文(没有从Boot获得任何好处,例如。

  • 我目前有一个maven Web项目,我正在尝试为其编写集成测试。对于项目的结构,我在src/test/java下定义了测试存根,而这些存根的Spring bean定义位于src/test/资源下。 我想做的是,当我构建我的war工件时,我希望所有的测试存根类都被编译并与Springbean定义文件一起包含在war中。我曾尝试使用maven war插件来实现这一点,但我唯一能够复制的就是资源。简单地

  • 我有一些问题。 允许在集成测试类中自动拥有控制器? 如何为这个控制器创建bean. 我有配置问题:help:

  • 问题内容: 我有一个Java方法,可在Mongo集合的两个字段上创建索引。我应该获取集合的索引信息,然后检查索引的名称和字段是否正确。为此编写集成测试的最干净方法是什么?使用自定义的Hamcrest匹配器查看索引是否在集合中是否有意义? 问题答案: 在春天 使用,您可以获取的列表,代表MongoDB集合的索引。由于这是一个常规列表,因此您可以结合使用和进行断言: 如果您觉得这太难以理解或不方便使用

  • 英文原文:http://emberjs.com/guides/testing/integration/ 集成测试通常用来测试应用中得重要工作流。集成测试用来模拟用户交互和确认交互结果。 设置 为了对Ember应用进行集成测试,需要在测试框架中运行应用。首先需要将根元素(root element)设置为任意一个已知将存在的元素。如果根元素在测试运行时可见的话,这对测试驱动开发非常有用,带来的帮助非常

  • 在grails项目(版本2.3.7)中,我有一个使用Spock的集成测试: 这个集成测试用Spring batch启动了一个批。从现有事务启动批处理时,Spring batch不接受: 这样,测试就成功运行了。 但是我的项目中还有其他集成测试,需要事务才能正确运行。并且我的测试类中的指令“transactionnal=false”并不局限于这个测试类,而是影响在我的测试类之后触发的所有其他集成测试