[英]How do you share classes between test configurations using SBT
I have followed the instructions on SBT's documentation for setting up test configurations.我已按照 SBT 文档中的说明设置测试配置。 I have three test configurations Test, IntegrationTest, and AcceptanceTest.
我有三个测试配置 Test、IntegrationTest 和 AcceptanceTest。 So my src directory looks like this:
所以我的 src 目录是这样的:
My question is, how can I configure SBT to allow sharing of classes between these configurations?我的问题是,如何配置 SBT 以允许在这些配置之间共享类? Example: I have a class in the "it" configuration for simplifying database setup and tear down.
示例:我在“it”配置中有一个类,用于简化数据库设置和拆卸。 One of my acceptance tests in the "acceptance" configuration could make use of this class.
我在“验收”配置中的一个验收测试可以使用这个类。 How do I make that "it" class available to the test in "acceptance"
我如何使“它”类可用于“验收”中的测试
Many thanks in advance.提前谢谢了。
A configuration can extend another configuration to use that configuration's dependencies and classes.一个配置可以扩展另一个配置以使用该配置的依赖项和类。 For example, the custom test configuration section shows this definition for the custom configuration:
例如, 自定义测试配置部分显示了自定义配置的此定义:
lazy val FunTest = config("fun") extend(Test)
The extend
part means that the compiled normal test sources will be on the classpath for fun
sources. extend
部分意味着编译后的普通测试源将位于fun
源的类路径上。 In your case, declare the acceptance
configuration to extend the it
configuration:在您的情况下,声明
acceptance
配置以扩展it
配置:
lazy val AcceptanceTest = config("acceptance") extend(IntegrationTest)
In case you want to stick with predefined configurations instead of defining new ones, and since both Test
and IntegrationTest
extend Runtime
(one would expect IntegrationTest
to extend Test
…), you could use the following:如果您想坚持使用预定义的配置而不是定义新的配置,并且由于
Test
和IntegrationTest
扩展了Runtime
(人们希望IntegrationTest
扩展Test
......),您可以使用以下内容:
dependencyClasspath in IntegrationTest := (dependencyClasspath in IntegrationTest).value ++ (exportedProducts in Test).value
This should put all the classes you define in Test
on the IntegrationTest
classpth.这应该将您在
Test
定义的所有类放在IntegrationTest
classpth 上。
##EDIT: ##编辑:
I was just became aware to amuch better solution thanks to @mjhoy:感谢@mjhoy,我才意识到更好的解决方案:
lazy val DeepIntegrationTest = IntegrationTest.extend(Test)
SBT uses the Maven default directory layout . SBT 使用Maven 默认目录布局。
It will recognize folders unders src/test/scala
to compile along with src/main/scala
.它将识别
src/test/scala
文件夹与src/main/scala
一起编译。
So, if you move the other folders under src/test/scala
SBT will compile them and you can share code between them.因此,如果您移动
src/test/scala
下的其他文件夹,SBT 将编译它们,您可以在它们之间共享代码。 eg:例如:
└── scala
├── acceptance
│ └── scala
│ └── Acceptance.scala
├── it
│ └── scala
│ └── IT.scala
└── Test.scala
Running sbt test
will compile all three files in the directory.运行
sbt test
将编译目录中的所有三个文件。 So, with this Acceptance refer to and can create a new IT class for example.因此,通过此 Acceptance 引用并可以创建一个新的 IT 类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.