Unit Tests run from Gradle and IDEA differ
已回答
I currently have a project that generates different test results depending on if I run them through Gradle -> Tasks -> Verification -> test (in IDEA) vs running them individually in the IDEA editor (e.g. Run 'MyTest' ctrl+shift+F10 from the gutter). Running from the Gradle task succeeds while running them individually in the editor fails.
I have only noticed this behavior recently (since possibly 2017.1) and I've seen it manifest itself in two different ways.
- Loading resources through new File() will attempt to load resources form the .idea/modules directory (where they do not exist). This seems to have gone away after clearing caches and restarting.
- More recently, I'm starting to get Method Not Found errors indicating some type of library conflict in the classpath.
I feel like this is a bug but I'm unable to replicate the behavior with a simple project so I'm wondering how to go about diagnosing/resolving this. Specifically:
- Are there any project/module facet settings I should be looking at that could cause these differences?
- When running an individual test, how does the classpath/context get generated or configured?
- Have recent changes to 2017.1.x altered the standard behavior of the test runners?
Some details about the project I'm running:
- Spring Boot 1.5.2
- Groovy 2.4
- Spock
请先登录再写评论。
Classpath is taken from the dependencies of the module the test belongs to (https://www.jetbrains.com/help/idea/2017.1/working-with-module-dependencies.html).
Files are loaded from the working directory, make sure it's specified in the Run/Debug configuration for the test.
These settings can affect tests execution:
Thank you for the quick response. Has this behavior changed recently? I've tried both "Delegate IDE build/run actions to gradle" and "Run test using: Gradle TestRunner" and both have very inconsistent behavior.
I'm encountering this on tests that I wrote a month or two ago at which time I was able to run them successfully in the IDE. Would rolling back to pre 2017 be a recommendation? Also, delegating to Gradle seems to make the testing cycle 3 to 4 times longer when running a single test so I would prefer to keep things the way they were.
I've asked the responsible developer to comment on that, but it may be hard to tell what's the problem without a reproducible test case.
I also can confirm this behavior,
We also encountered weird behavior when running unit tests in IntelliJ. After removing some exotic constructs from the Gradle files, tests in the IDE seems to work again, when they are NOT delegated to Gradle. But when build actions are delegated, IntelliJ does not recompile the production sources, leading to stale binaries or ClassDefNotFoundErrors, when cleaned before.
I also could not reproduce these issues in a simple project. Maybe they are partially caused by the project's size?
Thanks, your post solved my problem.
Similar thing here. No matter what I select for 'Run tests using'
- Gradle Test Runner
- Platform Test Runner
- Let me choose per test
when I run a test by clicking on a test class / method, IDEA will by now run the test always using the platform test runner.
Since I only build with gradle, this always ends up in a Class not found error.
Just yesterday I could get rid of the problem temporarily by restarting the computer. Today, even that didn't help.
Defining a gradle run config for every test that I run up front is very tedious. Any help is very much appreciated.
Deleting the .idea folder in my project and re-importing the project as new gradle project got rid of the issue for now. Hope it stays like that.
Serge Baranov's solution works for me. It just wants to confirm.
https://youtrack.jetbrains.com/issue/IDEA-175172 may be contributing to the the problem here.