如何在集成测试和单元测试之间共享代码 [英] How Can I Share Code Between Integration Tests and Unit Tests
问题描述
对于我的测试,我创建了一个对象,其中包含我所有案例类的任意实例(即我的生成器):
For my tests I have created an object which holds all my Arbitrary instances of case classes (ie my generators):
object Generators
extends
FooGen
{
def sample[A](implicit gen: Gen[A]): A =
gen.sample.getOrElse(sys.error(s"Could not generate instance with $gen"))
implicit def arb[A](implicit g: Gen[A]): Arbitrary[A] = Arbitrary(g)
}
trait FooGen { this: GenUtils =>
implicit val fooGen: Gen[Foo] = gen[Foo]
}
这当前位于我的/test 文件夹下,因为我需要它为我的单元测试生成我的案例类的任意实例.但现在我想创建一些集成测试,这些测试将在我的/it 文件夹下.将/test 文件夹中的此生成器文件与/it 文件夹中的测试共享的最佳方法是什么?
This currently sits under my /test folder as I need it to generate arbitrary instances of my cases classes for my unit tests. But now I want to create some integration tests which will be under my /it folder. What is the best way to share this generator file in my /test folder with my tests in /it folder?
我的所有案例类都会有很多这样的生成器,所以我不想复制代码,这就是我问的原因.
I will have lots of these generators for all my case classes so I don't want to duplicate the code so that is why I am asking.
推荐答案
基于 gilad hoch 的回答 try
IntegrationTest / dependencyClasspath :=
(IntegrationTest / dependencyClasspath).value ++ (Test / exportedProducts).value
例如你的 build.sbt
可能看起来像
for example your build.sbt
might look like
lazy val root = (project in file("."))
.configs(IntegrationTest)
.settings(
Defaults.itSettings,
libraryDependencies += scalaTest % "it,test",
IntegrationTest / dependencyClasspath :=
(IntegrationTest / dependencyClasspath).value ++ (Test / exportedProducts).value
)
和目录结构
├── it
│ └── scala
│ └── example
│ └── GoodbyeSpec.scala
├── main
│ └── scala
│ └── example
│ └── Hello.scala
└── test
└── scala
└── example
├── FooGen.scala
└── HelloSpec.scala
所以现在 FooGen.scala
可以从 GoodbyeSpec.scala
访问.
so now FooGen.scala
is accessible from GoodbyeSpec.scala
.
另一种选择是创建一个多项目 将通用测试代码构建并分解到自己的项目中,可能是 test-common
,然后拥有主项目 依赖
Another option is creating a multi-project build and factor out common test code into its own project, perhaps test-common
, and then have the main project depend on it
lazy val core = (project in file("core"))
.dependsOn(testCommon)
.settings(
// other settings
)
lazy val testCommon = (project in file("testCommon"))
.settings(
// other settings
)
这篇关于如何在集成测试和单元测试之间共享代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!