简体   繁体   English

如何在 intellij 中为 play 框架应用程序运行单元测试

[英]how to run unit tests for a play framework application within intellij

I tried running unit test on the play framework java starter example within intellij but it doesn't seem to work.我尝试在 intellij 中的play 框架 java starter 示例上运行单元测试,但它似乎不起作用。

I downloaded the project and imported it into intellij via the SBT import.我下载了该项目并通过 SBT 导入将其导入 intellij。

When I tried running the tests from intellij I got "Class not found: "UnitTest"Empty test suite"当我尝试从 intellij 运行测试时,我得到“找不到类:“UnitTest”空测试套件”

在此处输入图片说明

If I run the SBT test task, the tests are running and passing.如果我运行 SBT 测试任务,则测试正在运行并通过。

When I go bask to running the tests from intellij after running the SBT test task, the test run and pass.当我在运行 SBT 测试任务后从 intellij 运行测试时,测试运行并通过。 It's like intellij is not compiling the project这就像intellij没有编译项目

I am working with intellij ultimate edition version 2018.2 and have the scala plugin (v 2018.2.11)我正在使用 intellij 终极版 2018.2 版并拥有 scala 插件 (v 2018.2.11)

How can I make this work?我怎样才能使这项工作?

Edit (in respose to @hce)编辑(响应@hce)

I upgraded intellij to the latest version: 2018.3.2 (couldn't find 2018.3.5) but I still experience similar problems.我将intellij升级到最新版本:2018.3.2(找不到2018.3.5)但我仍然遇到类似的问题。

I started with a newly downloaded starter project.我从一个新下载的入门项目开始。 import the sbt project:导入 sbt 项目:

在此处输入图片说明

在此处输入图片说明

Then tried to compile and got an error:然后尝试编译并得到一个错误:

在此处输入图片说明

I managed to solve the compilation problem by configuring intellij to use the play2 compiler ("Use play2 compiler for this project")我设法通过配置intellij 使用play2 编译器来解决编译问题(“为这个项目使用play2 编译器”)

在此处输入图片说明

I then tried running the tests and but intellij complains it can't find the class然后我尝试运行测试,但 Intellij 抱怨它找不到类

在此处输入图片说明

this is the run configuration that was created by intellij.这是由 intellij 创建的运行配置。 the class name (UnitTest) is marked red...类名(UnitTest)被标记为红色...

在此处输入图片说明

Is it any different in your environment?你的环境有什么不同吗?

Is there anything wrong in my process?我的过程有什么问题吗?

I tested it with IntelliJ 2018.3.5 and the Scala plugin.我使用 IntelliJ 2018.3.5 和 Scala 插件对其进行了测试。 It works well.它运作良好。 So the cause seems to be in the configuration of your project.所以原因似乎在于您的项目配置。 It would be helpful if you can provide your run/debug configuration and the settings.如果您可以提供运行/调试配置和设置,将会很有帮助。 There are many reasons.有很多原因。 You could try the following...你可以试试以下...

  • Make sure you use java 1.8 as the project jdk.确保您使用 java 1.8 作为项目 jdk。
  • Use sbt 1.2.1使用 sbt 1.2.1
  • Check your run/debug configuration.检查您的运行/调试配置。
  • File -> Invalidate Caches/Restart...文件 -> 使缓存无效/重新启动...
  • Build --> Rebuild Project构建 --> 重建项目
  • Run --> Edit Configurations / remove Junit / Apply and restart your test.运行 --> Edit Configurations / remove Junit / Apply 并重新启动您的测试。 A new Junit configuration is created automatically自动创建新的 Junit 配置

Edit: After the comments of @NirBrachel, the solution was to use the sbt-shell to compile the project.编辑:在@NirBrachel 的评论之后,解决方案是使用 sbt-shell 来编译项目。 (I recommend clean, compile and test). (我建议清理、编译和测试)。

sbt-shell 清理、编译、测试

You could automate this during import:您可以在导入期间自动执行此操作:

![激活 sbt-shell 进行导入和构建

Why?为什么? Info: sbt compilation for play framework 2.x disabled by default信息:默认情况下禁用播放框架 2.x 的 sbt 编译

You can try to activate Play2 framework in the Settings...您可以尝试在设置中激活 Play2 框架...

在此处输入图片说明

WARNING: This settings didn't work for me, because it's not saved by IntelliJ.警告:这个设置对我不起作用,因为它不是由 IntelliJ 保存的。 I didn't find out yet why.我还没有找到原因。

EDIT see this link for a possible cause.编辑请参阅此链接以了解可能的原因。

I use Scala plugin and Play Framework.我使用 Scala 插件和 Play 框架。 I can run tests in 3 ways:我可以通过 3 种方式运行测试:

  • simple run 1 test from IDE (but I can't run all tests in package by this way)从 IDE 简单运行 1 测试(但我无法通过这种方式运行包中的所有测试)
  • run tests from sbt从 sbt 运行测试
  • run tests with use this configuration (it's allow run tests with coverage):使用此配置运行测试(它允许运行具有覆盖率的测试):

Sorry for my English.对不起我的英语不好。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM