I’m trying to get my unit tests running from the c...
# gradle
r
I’m trying to get my unit tests running from the command line. For unit testing purposes, I need the sources in
src/test/java/
,
src/mock/java
, and
src/main/java
. When I add these sources to the
test
source set and run
./gradlew test -i
from the command line, I don’t see any test results at all.
Copy code
sourceSets.forEach {
    if(it.name.contains("test")) {
        it.java.srcDirs("src/test/java/", "src/mock/java")
    }
    it.java.srcDirs("src/${it.name}/java")
}
Is there something else I should be doing to ensure that my tests actually run?
g
By default it works like that, just put tests to src/test/java, without any additional config I'm not 100% sure, buy this snippet looks incorrect for me, you override test source dir srcDits set new set of dirs, not append them, as I remember, but I may be wrong, should check code In general it doesn't look correct, why not just select test source set manually and add additional source directly there
r
Yeah, I realized later that something (maybe ABP?) is already managing these source sets for me, so I deleted this block. However, I still have the problem that I can't run my tests from command line. I'm still doing some testing, but preliminarily, it looks like there's something about using
.kts
files for gradle that prevents the usual tasks from being detected. For example, when I run an IntelliJ (Android Studio)
AndroidUnitTest
configuration, all my tests run as expected. But if I run
./gradlew testMockDebugUnitTest
from the command line, I get no tests run. If I run the same task from the gradle panel, I get the message
Test events were not received
.
An interesting side-note, probably unrelated: as I understand it, there should be a task named
test
that gets auto-generated for the project. While I can see one in the list of tasks, if I try to get it by name in either my root or module
build.gradle.kts
files, it fails to build because it can't find a task by that name.
I just started a brand new project and just tried running the auto-generated test from the command line and it doesn't work. It's starting to look unrelated to Kotlin. Thanks for all the assistance
g
I’m actually not sure what is
testMockDebugUnitTest
, there is
testDebugUnitTest
and it should work
auto-generated test from the command line and it doesn’t work
this work for me without problem, you can use
test
or
testDebugUnitTest
r
I started a brand new project and ran
testDebugUnitTest
and it did not work
testMockDebugUnitTest
is just a flavor dimension for my android build
g
Could you share this project?
Are you sure that your build results are not just cached?
r
Like I mentioned, I started a brand new project and just ran
testDebugUnitTest
from cli and no tests ran. (Android projects are initialized with an example test)
The results can’t have been cached, I have tried cleaning and re-running
I can’t share the whole project, but I can share the build file: https://pastebin.com/HJx2yBvt
g
Looks that you use kotlin.test, maybe this is somehow related
r
I removed that dependency a re-ran the task. There was no change
Brand new project. Completely untouched.
g
You removed Kotlin.test, but did you add junit?
Also test task marked as "up-to-date", so it means that it may be cached, try to add/change test before run, just to be sure
r
JUnit is already included in addition to Kotlin.test
g
Okay
r
I also changed the test and re-ran
testDebugUnitTest
with no change
I have now tried pulling external projects and tried running their tests from cli and those don’t work either. So, my established projects, brand new projects, and external projects.. none of their unit tests run in command line.
g
If you share some ready to test sample, I can check tomorrow
r
To reproduce my exact environment, open Android Studio, start a new project, and try to run the tests from command line. That’s it.
g
I did it many times and it always work, maybe there are some specific problems with particular version or something like that
r
Yeah, there’s something really mysterious going on here, because I’ve tried it on Windows/Mac, Gradle 4.7/5.0/5.1.1, AGBP 3.2.0/3.3.0/3.4.0, multiple other computers, and multiple other projects. I’ve cleaned, I’ve reimported the gradle projects…
I’m at a total loss as to what the common element is here
g
Maybe version JVM? Just a random guess
r
Unfortunately, that did not solve my problem either
g
@rook I just created new Android project using AS 3.4, default settings, “No activities” template, run from command line
Copy code
./gradlew testDebugUnitTest
test is passed. Added one more test with failed assert, run again:
2 tests completed, 1 failed
Are you sure that you check that test are run from command line? Because on screenshot above you run it from AS and showed me log from AS which itself looked completely fine, maybe just broken integration with gradle test tasks in AS
r
I just ran everything again from the command line this morning in that empty project with a failing test, and it worked! I actually got test outputs! However, when I switch the test to be passing, I don’t get any test outputs. All that aside, I know now that something must be wrong with my project’s configuration. Thanks for all the help Andrey
g
I don’t get any test outputs.
you don’t get any output in reports or you just don’t see any output in console? In case if all your tests are passed you shouldn’t see any additional output with rich console, just successfull build, but repot file still will be generated
r
I just meant I don’t see anything in the console. I was unaware that gradle created report files from tests
g
yes, by default you have no output if test are passed, test just passed and that all, you can find reports in build/reports dir
r
Thank you very much Andrey. I think that I incorrectly assumed that running the gradle tasks from the gradle panel was equivalent to running the gradle tasks from the command line
Which masked the fact that removing the
sourceSet
settings fixed the problem. Which I was confused about the result because there were no command line outputs.
What a ride. Thank you so much for your time and patience @gildor
g
You are welcome! 👍 I’m glad it helped
🙇‍♂️ 1
but it still a bug in AS on your screenshot above, which I could reproduce, AS tries to integrate with Gradle test tasks and show reports, but shows nothing, it worked before, maybe some regression in AS 3.4
r
Good to know. Where can I report that?
g
I think Android issue tracker is the right place
yeah, tried on IDEA 2019.1 with Kotlin (not Android) project, works properly, doesn’t work with sample Android project and AS 3.4