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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35488213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 08:01:37  来源:igfitidea点击:

scalatest : object scalatest is not a member of package org

scalaintellij-ideasbtscalatest

提问by Benjamin

EDIT: it works if the file is in src/test/scala/tests/ but not in src/main/scala/mypackage/ Why ?

编辑:如果文件在 src/test/scala/tests/ 但不在 src/main/scala/mypackage/ 为什么?

I have try solutions from topics with people having nearly the same issue but none works.

我已经尝试过与有几乎相同问题的人的主题解决方案,但没有任何效果。

In details, I have this in build.sbt :

详细地说,我在 build.sbt 中有这个:

libraryDependencies ++= Seq(
   ...
  "org.scalatest" % "scalatest_2.10" % "2.2.1" % "test",
   ...

In intellij a class with:

在 intellij 中,有一个类:

import org.scalatest.{BeforeAndAfterAll, Suite}

with {BeforeAndAfterAll, Suite} in red so i guess scalatest is found

{BeforeAndAfterAll, Suite} 为红色,所以我想找到了 scalatest

sbt package does not work too :

sbt 包也不起作用:

object scalatest is not a member of package org [error] import org.scalatest. {BeforeAndAfterAll, Suite}

对象 scalatest 不是包 org [error] import org.scalatest 的成员。{BeforeAndAfterAll,套房}

I have already tries this :

我已经尝试过这个:

  • sbt clean update
  • restart + invalidate cache of intellij
  • remove .idea/ and reimport
  • libraryDependencies += "org.scalatest" % "scalatest_2.10" % "2.0" % "test" instead ogf the actual
  • magical ritual on my keyboard
  • sbt 干净更新
  • 重新启动 + 使 Intellij 的缓存无效
  • 删除 .idea/ 并重新导入
  • libraryDependencies += "org.scalatest" % "scalatest_2.10" % "2.0" % "test" 而不是 ogf 实际
  • 我键盘上的魔法仪式

Nothing works

没有任何效果

Any idea ?

任何的想法 ?

回答by Onilton Maciel

You should take a look at sbt project structure:

你应该看看sbt 项目结构

src/
  main/
    resources/
       <files to include in main jar here>
    scala/
       <main Scala sources>
    java/
       <main Java sources>
  test/
    resources
       <files to include in test jar here>
    scala/
       <test Scala sources>
    java/
       <test Java sources>

Your scala tests should go under src/test/scala/.

您的 Scala 测试应该在src/test/scala/.

If you really want to use scalatest in main (and you know what you are trying to do), try this:

如果您真的想在 main 中使用 scalatest(并且您知道自己要做什么),请尝试以下操作:

  "org.scalatest" % "scalatest_2.10" % "2.2.1",

In your original build.sbt, "test" is the configuration and it means that scalatest will only be on the test classpath and it isn't needed by the main sources. This is generally good practice for libraries because your users don't typically need your test dependencies to use your library (ref).

在您的原始文件中build.sbt,“test”是配置,这意味着 scalatest 只会在测试类路径上,主要来源不需要它。这通常是库的好习惯,因为您的用户通常不需要您的测试依赖项来使用您的库 ( ref)。