How to run a single unit test using gradle wrapper 2.2.1 from command line - gradle

Using Android studio with gradle wrapper version 2.2.1, I am trying to run all the tests in one single test class, as well as a specific test inside that class and have tried using:
./gradlew test --tests DownloadsActivityTest
like the documentation suggests, as well as
-DandroidTest.single=DownloadsActivityTest
But neither of these versions work.
How do I run a single test class, and a single test from the command line using the gradle wrapper?
./gradlew --version
------------------------------------------------------------
Gradle 2.2.1
------------------------------------------------------------
Build time: 2014-11-24 09:45:35 UTC
Build number: none
Revision: 6fcb59c06f43a4e6b1bcb401f7686a8601a1fb4a
Groovy: 2.3.6
Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM: 1.7.0_51 (Oracle Corporation 24.51-b03)
OS: Linux 3.17.6-200.fc20.x86_64 amd64
[16:33][j#localhost:~/myHomeDir]$ ./gradlew test --tests DownloadsActivityTest
WARNING: Dependency commons-logging:commons-logging:1.1.1 is ignored for debugTest as it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.3 is ignored for debugTest as it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class packages
FAILURE: Build failed with an exception.
* What went wrong:
Problem configuring task :app:test from command line.
> Unknown command-line option '--tests'.
* Try:
Run gradlew help --task :app:test to get task usage details. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 4.466 secs

To only run instrumentation tests (androidTests) in a specific test class, execute:
./gradlew app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.android.testing.blueprint.ui.espresso.EspressoTest
To only run local tests ("unit", JVM tests) in a specific test class or package execute:
./gradlew :app:testDebugUnitTest --tests "com.example.android.testing.blueprint.unit.integrationTests.*"

./gradlew :<module name>:test<CapitalCasedBuildVariant> --tests "<Test name pattern>"
It is important to specify the module name where the test lives, otherwise, when gradle builds dependent modules, it will attempt to test them, and might fail because none of the tests fit the test pattern:
No tests found for given includes: [FooTest]
Also, unless you specify the fully-qualified-name (FQN) of the test, your test name pattern should start with a *.
In my case, I have a module named app and a variant named localDebug, and I wanted to test FooTest, so I ran:
./gradlew :app:testLocalDebug --tests "*FooTest"

This is supported in Android Studio 1.1, using the Android Gradle plugin v1.1.0.
Follow the guide here

The Android Gradle plugin, as of 1.0.0, doesn't have support for running single Android tests. The feature request for it is filed at https://code.google.com/p/android/issues/detail?id=74196.
I know that better test support in general is very high on the post-1.0 priority list, but I can't say with any certainty when this will be implemented.

Related

Building Springboot 2.6.3 from source - unable to exclude tests

I am attempting to build the spring-boot 2.6.3 packages from source as per the spring-boot instruction on the official github page by running .gradlew publishToMavenLocal -x test , with tests excluded. However, this is failing because one of the executed tasks spring-boot-project/spring-boot-actuator-autoconfigure:asciiDoctor has a dependency on its tests!
Hence the build fails if the tests are excluded.
Are there any workarounds to this ?

Issue with Gradle and Gradlew

I have a project that has both gradle (6.3) and gradlew. When I do a "./gradlew build", all is fine. But when I do "gradle build" I have the following error:
What went wrong:
A problem occurred evaluating root project ....
> Failed to apply plugin [id '...']
> Could not create an instance of type ....
> org.gradle.api.file.ProjectLayout.directoryProperty()Lorg/gradle/api/file/DirectoryProperty;
Any idea what could cause the wrapper to work OK and not gradle?
Thanks - C
./gradlew build uses a different version of Gradle than what gradle build uses. That's exactly the reason for the gradle wrapper: it will look at the contents of the file gradle/wrapper/gradle-wrapper.properties to figure out which version of Gradle to use, and then automatically downloads and uses that Gradle version. The Gradle you have installed, version 6.3, is newer than the one used by the gradlew (gradle wrapper) script. This is why gradle build does not work: your build script is incompatible with this new gradle version, it only works with the older one used by the gradlew script.
The error you see is caused by an incompatibility of your Gradle build script with a newer Gradle version. Let's look at the first part:
> Could not create an instance of type ....
> org.gradle.api.file.ProjectLayout.directoryProperty()Lorg/gradle/api/file/DirectoryProperty;
It tells you that Gradle is looking for a method directoryProperty in the class ProjectLayout. This member exists up to Gradle Version 5 (see https://docs.gradle.org/5.0/javadoc/org/gradle/api/file/ProjectLayout.html) but is no longer present in Gradle 6.3 (https://docs.gradle.org/current/javadoc/org/gradle/api/file/ProjectLayout.html). So the Gradle API changed, and your build script is no longer compatible.
The second part of the error:
> Failed to apply plugin [id '...']
tells you that this happened in the implementation of the plugin (given by the ... in the id). This means that to fix the error with newer gradle versions, the plugin needs to be modified.

Corda - Gradle Error With Quasar When Upgrading to Corda 4.3

I've updated my build.gradle file to Corda 4.3, however applying this plugin...
apply plugin: 'net.corda.plugins.quasar-utils'
...causes the following error when trying to refresh gradle...
A problem occurred evaluating root project 'template'.
> Failed to apply plugin [id 'net.corda.plugins.quasar-utils']
> Could not create an instance of type net.corda.plugins.QuasarExtension_Decorated.
> No signature of method: org.gradle.api.internal.provider.DefaultPropertyState.convention() is applicable for argument types: (java.lang.String) values: [co.paralleluniverse]
Any ideas what is causing this?
I had the same problem executing Corda 4.4 release. Basically you have to update your gradle version to recognize some dependencies in gradle.
First of all, execute the comand: gradle -version
After that, you have to update your gradle version according to the gradle version supported by your current Corda version. In my case, It is supported by gradle version 5.4.1. So, to execute an upgrade, try this: ./gradlew wrapper --gradle-version 5.4.1
Next, check again your gradle version using ./gradlew --version (in the CorDapp directory)
Finally, you can execute your task. So, in my case I would like to execute a test with heap dump allocating more memory and check an OOM. I execute the follow command:
./gradlew test -Dlog4j.configurationFile=../config/test/log4j2.xml -Dcapsule.jvm.args=["-Xmx10G","-Xms512m","-XX:+UseG1GC","-XX:+HeapDumpOnOutOfMemoryError"]
That is it!
Have you tried with gradle-5.4.1?
Source : https://www.corda.net/blog/corda-version-upgrade-guide/

How to run only specific tests with Gradle and JUnit 5?

Using Gradle and its JUnit 4 support, I can choose a specific test using the --tests option like this:
$ ./gradlew test --tests de.mp.BarMT
This option is ineffective when using the JUnit 5 Gradle task. It is ignored silently when using the test tasks from the command line. The true work is done by junitPlatformTest anyway, and it does not support the option:
$ ./gradlew clean junitPlatformTest --tests de.mp.BarMT
…
Problem configuring task :junitPlatformTest from command line.
> Unknown command-line option '--tests'.
Does the JUnit 5 plugin support choosing specific tests?
Answering myself… A lot of time has passed, new versions came, and I can confirm that with Gradle 5.4.1 and JUnit Jupiter Platform 1.4.2, the simple configuration:
test {
useJUnitPlatform()
}
will allow running single tests as seen in https://stackoverflow.com/a/31468902/2621917, even when using weird names (from Kotlin):
gradle test --tests 'HtmlForwardInterceptorTest.should forward requests'

Using Gradle to find dependency tree

Is it possible to use Gradle to produce a tree of what depends on what?
I have a project and would like to find out all the dependencies so I may be able to prune it a little with forward declarations etc.
Without modules:
gradle dependencies
For Android:
gradle app:dependencies
Using gradle wrapper:
./gradlew app:dependencies
Note: Replace app with the project module name.
Additionally, if you want to check if something is compile vs. testCompile vs androidTestCompile dependency as well as what is pulling it in:
./gradlew :app:dependencyInsight --configuration compile --dependency <name>
./gradlew :app:dependencyInsight --configuration testCompile --dependency <name>
./gradlew :app:dependencyInsight --configuration androidTestCompile --dependency <name>
You can render the dependency tree with the command gradle dependencies. For more information check the section Listing dependencies in a project in the online user guide.
If you find it hard to navigate console output of gradle dependencies, you can add the Project reports plugin:
apply plugin: 'project-report'
And generate a HTML report using:
$ ./gradlew htmlDependencyReport
Report can normally be found in build/reports/project/dependencies/index.html
It looks like this:
In Android Studio (at least since v2.3.3) you can run the command directly from the UI:
Click on the Gradle tab and then double click on :yourmodule -> Tasks -> android -> androidDependencies
The tree will be displayed in the Gradle Console tab
Often the complete testImplementation, implementation, and androidTestImplementation dependency graph is too much to examine together. If you merely want the implementation dependency graph you can use:
./gradlew app:dependencies --configuration implementation
Source: Listing dependencies in a project
Note: compile has been deprecated in more recent versions of Gradle and in more recent versions you are advised to shift all of your compile dependencies to implementation. Please see this answer here
If you want to visualize your dependencies in a graph you can use gradle-dependency-graph-generator plugin.
Generally the output of this plugin can be found in build/reports/dependency-graph directory and it contains three files (.dot|.png|.svg) if you are using the 0.5.0 version of the plugin.
Example of dependences graph in a real app (Chess Clock):
For me, it was simply one command
in build.gradle add plugin
apply plugin: 'project-report'
and then go to cmd and run following command
./gradlew htmlDependencyReport
This gives me an HTML report WOW Html report 💕
Or if you want the report in a text file, to make search easy use following command
gradlew dependencyReport
That's all my lord.
Things have moved forward in Gradle so I believe this question merits another answer.
Since Gradle 4.3, "build scans" were introduced. All relevant info is available in the Gradle docs (1, 2). For me, this seems to now be the easiest way to check your dependencies (and generally your build) in a clear, organized way.
They are very easy to create, just execute:
gradle build --scan
(or ./gradlew build --scan if you use a wrapper)
This produces a randomly generated link where you can see your scan. When opening that link, you enter your email and gain full control of the link: eg. share it or delete it. It has got a lot of info about your build, not just dependencies. You can see your dependencies, their hierarchies, the repository used to obtain them but also a lot of other stuff about your build, namely, its performance (which is of interest in big complex builds), your tests, even your console output and your system configuration, which JDK and JVM was used, max heap size etc.
This is a printscreen from a mock project:
A build scan is a shareable record of a build that provides insights into what happened and why. You can create a build scan at scans.gradle.com for free.
Note however, that info for your build process will be sent to the Gradle servers. You have full control to delete it when you are finished with your inspection.
Finally, you can use build scans with Gradle versions earlier than 4.3 too, you just have to manually add the scans plugin in your buildscript.
Edit:
Incorporating some feedback from the comments some extra notes:
1) It is very difficult to do this by mistake or without understanding that some info for your build will be online (private to you, with the ability to delete it, but still online).
When executing gradle build --scan the following message appears:
Publishing a build scan to scans.gradle.com requires accepting the Gradle
Terms of Service defined at https://gradle.com/terms-of-service. Do you
accept these terms? [yes, no]
You have to explicitly write yes and then the message continues:
Publishing build scan...
https://gradle.com/s/a12en0dasdu
2) In Gradle Enterprise you can host gradle build scans in your own servers. However I have no experience in this and my proposed approach was about the standard Gradle distribution, using Gradle's servers for your build scans.
3) Gradle itself promotes the build scans as the way to deal with most your build problems.
For Android, type this in terminal
gradlew app:dependencies
It will list all the dependencies and the ones with newer versions for you to upgrade like
com.android.support:customtabs:26.1.0 -> 27.1.1 (*)
For recent versions of Gradle (I tested with the 6.4.1 version):
gradle dependencies --configuration compileClasspath
or if you're using the Gradle Wrapper:
gradlew dependencies --configuration compileClasspath
When building for Android with the 'debug' and 'release' compilation profiles, the debugCompileClasspath and releaseCompileClasspath configurations can be used instead of compileClasspath.
I also found useful to run this:
./gradlew dI --dependency <your library>
This shows how are being dependencies resolved (dependencyInsight) and help you debugging into where do you need to force or exclude libraries in your build.gradle
See: https://docs.gradle.org/current/userguide/tutorial_gradle_command_line.html
In Android Studio
1) Open terminal and ensure you are at project's root folder.
2) Run ./gradlew app:dependencies (if not using gradle wrapper, try gradle app:dependencies)
Note that running ./gradle dependencies will only give you dependency tree of project's root folder, so mentioning app in above manner, i.e. ./gradlew app:dependencies is important.
Note that you may need to do something like ./gradlew <module_directory>:<module_name>:dependencies if the module has extra directory before reach its build.gradle. When in doubt, do ./gradlew tasks --all to check the name.
If you want all the dependencies in a single file at the end within two steps.
Add this to your build.gradle.kts in the root of your project:
project.rootProject.allprojects {
apply(plugin="project-report")
this.task("allDependencies", DependencyReportTask::class) {
evaluationDependsOnChildren()
this.setRenderer(AsciiDependencyReportRenderer())
}
}
Then apply:
./gradlew allDependencies | grep '\-\-\-' | grep -Po '\w+.*$' | awk -F ' ' '{ print $1 }' | sort | grep -v '\{' | grep -v '\[' | uniq | grep '.\+:.\+:.\+'
This will give you all the dependencies in your project and sub-projects along with all the 3rd party dependencies.
If you want to get this done in a programmatic way, then you'll need a custom renderer of the dependencies - you can start by extending the AsciiDependencyReportRenderer that prints an ascii graph of the dependencies by default.
double click and run dependency under help in gradle view
Try either this
./gradlew dependencies > ~/dependencies.txt
or
gradle dependencies > ~/dependencies.txt`
which should write the dependencies in text file under user's home directory.

Resources