Run androidTest from app module including androidTest from submodules without own activities - android

I have a project with a single module app and then lots of other modules like search and detail. app has MainActivity and almost nothing else. The other modules have lots of #Composable functions that make up the actual completed application. The individual modules also have tests in androidTest for automated testing.
How can I run the tests declared in each submodule but against the app build apk?
Directory structures may make this clearer:
/project/app/src/main/.../MainActivity.kt
/project/search/src/main/.../SearchComposable.kt
/project/search/src/androidTest/.../SearchTests.kt
/project/detail/src/main/.../DetailComposable.kt
/project/detail/src/androidTest/.../DetailTests.kt
If I run
./gradlew :search:connectedDebugAndroidTest
it will try to run those tests but they don't have an Activity, they only really run from app. Unit tests of course work fine, but automated tests do not.
If I run
./gradlew :app:connectedDebugAndroidTest
Then it will only run those declared within app. I haven't figured out a way to add a dependency from app's androidTest configuration to search/detail's androidTest output (lots of searching, nothing promising yet).
If I run
./gradlew connectedDebugAndroidTest
It will try to run all and fail to run search and detail since they don't have an activity.

Related

Sharing Android instrumentation tests between modules

Our Android app code is setup is like below:
Project
----Module1
----Module2
----Common UI library module
----Common SDK module
Module 1 and Module 2 are built into separate apks and published separately. They however share/extend a great deal of UI that are in the library module. Currently we have instrumentation tests inside each module, which means we have to duplicate tests for the same screens (defined in the UI module) under both Module 1 and Module 2. Is there a way to strip out the instrumentation tests from the specific modules and have a separate test module? I would ideally like to have shared tests where I can specify through command line something along the lines of: "Run test suite T against the test apk for Module 1" or "Run test suite T against the test apk for Module 2.
Any suggestions on how to do this is appreciated.

Android: running instrumented tests on TeamCity server

I am building an Android Studio/Gradle project on TeamCity server. I am somewhat new to TeamCity. Currently, the unit tests auto-generated by Android Studio are run automatically when the project is built on TeamCity and are displayed under "Tests" . I also have an instrumented test (a test which runs on the connected android device), but it does not get run automatically like the unit tests do.
My solution was to add a Gradle build step in TeamCity to run the instrumented test. So far, I've had little success. I used the gradle tasks uninstallAll connectedAndroidTest, which runs the instrumented test, but the test result does not show up under "Tests" along with the unit tests. If the instrumented test fails, the build fails, but the failed test still does not show up under "Tests".
What am I doing wrong? Is there a correct way to run instrumented tests on TeamCity?
The connectedAndroidTest will output files specifying test results according to this pattern:
HTML test result files:
path_to_your_project/module_name/build/outputs/reports/androidTests/connected/
directory.
XML test result files:
path_to_your_project/module_name/build/outputs/androidTest-results/connected/ directory.
(from here)
Using that output file you can use the XML Report Processing feature of TeamCity. In your Build Configuration just go to the Build Features tab and add the XML Report Processing feature. Use the Google Test option and point it to the report output directory like so:
After that you should see your instrumented test results show up in your builds just like regular JUnit tests:
You can tweak test task:
test.dependsOn uninstallAll, connectedAndroidTest
Another way would be to include your test (though not sure if this will work as I am not sure what uninstallAll and connectedAndroidTest tasks are doing):
test {include 'org/foo/**'}

How to run unit test on a android module?

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

Include Tests to run in build process Android

I have been working on a module, whenever it is build using "gradlew assemble"it will generate an aar file in build/output folder. I have created few tests extending the InstrumentationTestCase in android app module, I want to run those tests whenever I build the library using gradle command.If compilation of tests "pass" the build(aar) should happen else it should fail. Could any one please help me on this?
Running gradlew clean connectedCheck should run through all of your tests on all connected devices and will also build out your aar file. That's the easiest way.

How to exclude a test from a test application using eclipse android-junit launch configuration

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.

Categories

Resources