I have a android project that has multiple library modules and I am trying to test a specific package that contains all modules.
I tried this command:
./gradlew -Dtest.single=com.moduleone* testProductionDebug
And it does not work: it doesn't execute the tests inside this module, but instead executes all the unit tests in the main project package class.
How do I test just the one module?
You can use test suits: https://developer.android.com/reference/junit/framework/TestSuite.html . Definition of a suit contains classes of tests you need
Assuming you're trying to execute a gradle task against a single module rather than the entire project, you can supply the name of the module in front of the task separated by a colon (module_name:task)
Per your question, this would look something like
./gradlew -Dtest.single=com.moduleone* your_library_module:testProductionDebug
This is a simple example, assuming you have a simple project setup. You can also find further reading on this in the gradle docs for executing a multi-project build
Related
We have a project, that has some instrumented tests that run part of CI
Now we want to add some integration tests (of type instrumentation tests), but they should not run as part of pipeline, but on demand through a separate Jenkins job or command
I am trying to create a separate module for integration tests that depend on "app" module, but that's throwing a lot of errors like below
errors showing up in android studio while it tries to resolve the app module
I wanted to understand if its even possible?
It is possible. There is probably an easier way than what I am about to suggest but you can implement a rule that conditionally ignores test and change the value of this variable in your CI job.
There are different approaches.
1)Determine which tests to run via a boolean stored in server. So mark your normal ui tests with the condition shouldUITest and the others shouldInstrumentationTest and change their value in server
2)Change variables in app.gradle with buildflavors. (Basically create 2 different variants and run whichever one you want
3)Detect the build is a CI build, and act accordingly in app.gradle
def ciBuild= System.getenv("CI") == "true"
if (ciBuild) {
//Do stuff
}
I have several tests in common module for multi platform Kotlin project. When I execute those tests using gradle, e.g. ./gradlew :android:test, they all go through and the tests run.
I have now encountered a more complicated problem where I would like to debug an actual test in IntelliJ. Unfortunately, upon selecting the debug option in IntelliJ, I get an No JDK specified error.
I am using the following dependencies for testing:
testImplementation "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
testImplementation "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
with $kotlin_version being 1.2.41.
The common module settings looks like this:
SDKs section also correctly recognises JDKs:
I have tried changing the Module SDK from Kotlin SDK to java, however IntelliJ then wants me to require jUnit for test execution, which I would prefer not to, if possible.
Is there a way how to make the debugger run in IntelliJ for the Kotlin code?
Found the solution.
Just like it does not make sense to execute tests using Gradle in the common module alone, e.g. ./gradlew :common:test and the tests need to be executed for a specific platform ./gradlew :android:test, because the common module might contain expected declarations which are supposed to be implemented per platform using the actual keyword, it also does not make sense to debug in the common module directly.
Instead, for such purposes, the test to be debugged must be placed in a specific platform module, for my purpose I have chosen the Android module, and then it can be executed and debugged.
As I have mentioned, this approach is necessary because in the Android module the expected structures are actually replaced by the actual implementations.
Our current set-up looks like this: One root-project with nine sub-modules, which are mixed between pure Java libraries and Android libraries.
Each of the modules contains features, one of the modules is the main module which builds the Android app by merging all required modules.
Each of the modules contains a lot of unit tests, and I'd like to execute them all in one gradle step. Right now I'm using gradlew test for this, but since (for example) the main module has multiple product flavors it is executing the unit tests in this module multiple times (for each flavor).
Ideally, I'd like to only execute the main module unit tests once, for a certain flavor and signing configuration, but since the testFlavorDebug tasks are created while evaluating the project (as far as I know) I cannot define this before actually executing the evaluation.
I've tried to find the tasks in afterEvaluate blocks, I've tried to loop over all modules, different combinations and ways, but I can't seem to figure out how to properly set this up.
I'd be glad if someone has some pointers or experience with a set-up like this and could help me out. If you require additional information, let me know and I'll supply them, but as far as the project set-up it's pretty straight forward in regards to the multi-module setting.
You can override the test method in your main module's build.gradle to only run the one time
I have Espresso instrumentaion test cases in following form in Android studio.
Now, I want to run few of the classes separately.
E.g : I want to run Only CrashersTest and EM3AppUtil classes using gradlew command and rest classes using another command.
How can i achieve it?
I am using below command
./gradlew :app:connectedLocalDebugAndroidTest
It runs all the classes for LocalDebug variant
I want to run only few classes
There isn't a great way to do it for instrumented tests right now that I know of.
You can create a test suite to run just certain test you want, like documented here
https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html#test-suites
Or use the #SmallTest, #MediumTest or #LargeTest annotations on your tests and then pass in on command line like so
./gradlew connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.size=small
Place the cursor on the class name in java file or select the file in file view then press ctrl+shift+F10
Refer this How to run only one test class on gradle for running tests from gradlew command
I got an application project under test and a test application project (on Android).
My test app contains a lot of tests but I would like to find a mechanism to exclude a test, or a test class from the test launch (launched from eclipse).
I know I can exclude tests on the command line based on annotations for instance, or use ant to exclude classes, but I don't have this kind of configuration and want to exclude them directly from eclipse).
Is it possible to exclude a test, or a bunch of tests from a from a test application using eclipse android-junit launch configuration ?
Thanks in advance !
Android JUnit Test launch configuration is lack of functionality which only support configure running either all tests or single one test class at the moment.
Alternatively, instead of trying to include/exclude tests right before junit run time, we can include/exclude specific test source files at project build time by configuring project build path in Eclipse, right-click your test project, go to Build Path -- Configure Build Path, in the Source tab, edit Excluded section and add multiple test source files, this will exclude the test source files from your project build path and finally exclude them when you start running Android JUnit test:
DISCLAIMER: This answer is for normal JUnit 5 Test configurations, and is not Android specific. But since this was the first result I got for a more generic Google search, I figured I'd update here for whom it may benefit.
As of Eclipse Oxygen (4.7.3a), I am able to create a new JUnit Run Configuration and add Tags to include/exclude during a test run. Classes (and I believe methods too) can be decorated with the #Tag("") annotation and the test runner will filter those out. I am using JUnit 5 with a relatively straight-forward Java application using the Spring Framework.
Run > Run Configurations... > JUnit > New > Configure...
I have a package src/test/java, with a test class called JoinConfigIT, with an annotation at the top of my classed of #Tag("integration").
Now when I run the IntegrationTests run configuration that I created, it only runs all tests with classes decorated with the #Tag("integration") annotation.
I hope this helps someone. Good luck.
You can use annotations #SmallTest, #MediumTest or #LargeTest and create different configurations for launching different tests. See this docs.
I think testSuite class would do the trick. It is designed to help organize tests.
Please reference sample code in apiDemos test and read corresponding doc. Best of luck.