Sbt子项目中testOnly(Scalatest)命令无法执行问题
It looks like your some-service subproject doesn’t have the necessary setup to recognize the testOnly command. Here are the most likely fixes to resolve this issue:
1. Ensure the Subproject Inherits Test Settings
Your root project uses Commons.settings, but if your some-service subproject isn’t explicitly including these settings, it won’t have access to test-related commands like testOnly. Update your project definition to apply the common settings to the subproject:
lazy val `some-service` = (project in file("some-service")) .settings(Commons.settings: _*) // Add this line to inherit test configurations
2. Verify ScalaTest Dependencies & Plugin Are Configured
The testOnly command is provided by ScalaTest, so make sure your build includes the required dependencies and plugin:
In project/plugins.sbt:
Add the ScalaTest sbt plugin to enable test commands:
addSbtPlugin("org.scalatest" % "scalatest-sbt-plugin" % "3.2.15") // Use the latest compatible version
In your Commons.settings (typically in Build.scala or build.sbt):
Include the ScalaTest dependency in the Test scope:
lazy val Commons = Seq( // ... other shared settings ... libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.15" % Test )
3. Double-Check the testOnly Command Syntax
Once the setup is correct, ensure you’re using the right syntax after switching to the subproject:
project some-service testOnly *MyTest
The asterisk (*) acts as a wildcard—if your test lives in a package, you might need to use something like testOnly com.example.*MyTest to match it properly.
4. Reload sbt After Changes
After updating your build files, run reload in the sbt shell to apply the new configuration. Then try the testOnly command again.
内容的提问来源于stack exchange,提问作者FrancMo

