I have just created my first unit test as part of an android project. I followed the guide found here.
Now I would like to include all unit tests in the "test" package before the app is launched to the phone. How would I include this in the gradle file?
Related
I have one project(android and iOS) where I am sharing the common code inside shared folder. I have written both unit tests under commonTest(unit-test) folder as well as Instrumentation test inside androidTest(instrumentation test) folder. It works well when I run individual test. But I want to see the coverage also. While I can do this "run with test coverage" for commonTest but it's not available for androidTest. Any tool that will be helpful here. I tried searching and tried few stuffs with jacoco and kover but could not help.
gradle-ndk-gtest-sample
Android NDK with
README.NDK
I have been trying to configure my Android NDK project for use with Google Test. I started with native-activity sample as the base project and began following the guides above. I inserted the code from the first link into my root build.gradle. I am really not having any success. It looks like i was able to generate the test library:
./libs/googleTest/1.7.0/lib/osx/libgtest.a
./libs/googleTest/1.7.0/lib/linux/libgtest.a
./app/.externalNativeBuild/cmake/debug/x86_64/libnative_app_glue.a
./app/.externalNativeBuild/cmake/debug/x86_64/libFOO.a
./app/.externalNativeBuild/cmake/debug/x86_64/libgtest.a
./app/.externalNativeBuild/cmake/debug/arm64-v8a/libnative_app_glue.a
./app/.externalNativeBuild/cmake/debug/arm64-v8a/libFOO.a
./app/.externalNativeBuild/cmake/debug/arm64-v8a/libgtest.a
But I don't know where to go from here. How do I actually run the tests and see if they passed?
Try something like https://github.com/DanAlbert/GTestJNI. It's an alternative test runner for JUnit and GTest that exposes GTests as if they were JUnit, so you can just run your normal androidTests and they'll run your GTests for you.
Some day I want to get this either added to the NDK or at least made trivial to add to a project as a git submodule or something, but for now you can merge the pieces of that into your project.
i am looking for some inputs if anybody as any specific or relevant info about my scenario mentioned below :
In Our Android Project, we have one common Library folder and 2 separate(Different) App Folders. I have a requirement to write the Espresso Test for both the Apps. I have Written Espresso Test at the App Level(App->Src->AndroidTest->Java-PKG-LoginFlowTest.java) for both App Module, which is working fine. But now When i moved my Test Folder in Library Module (with same Level/Depth like in App), its not working. Throwing Below Error.
Error :
java.lang.RuntimeException: Unable to start activity ComponentInfo{xx.xxx.xxxxxx.lib.test/xx.xxx.xxxxxxxx.lib.login.views.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'xx.xxx.xxxxxxxxx.lib.dependency_injection.components.AppComponent xx.xxx.connected.lib.dependency_injection.DaggerInjector.getAppComponent()' on a null object reference
Here My intension is to write a Test common for both the Apps(2 different apps Generated using the same Source Sets with some different feature for both apps).
Thanks
Default Package for Android Tests is androidTest. If you use another package is not going to work unless you make some configuration changes on gradle.
If you have two 'app' in the root project, then they should have their own Espresso Tests under their 'androidTest' package.
It does not make sense to have two 'apps' and expect to write one espresso test for both of them. My understanding is that those 'apps' are not identical since they generate different apks. If developers want to create multiple apks for the same project, then they can configure different flavors (debug, release, etc) for a given project on gradle.
I do not understand. How can I add and use Espresso in my project?
Espresso url: https://code.google.com/p/android-test-kit/
How to connect this library to my project? I would like to have an example in Eclipse.
To use Espresso for Android you have to create test project which will test your main application under test. This link contains Espresso setup instruction for Eclipse and Android Studio. And also some basic steps you have to do (with test project but not with the main app) before running your tests: adding GoogleInstrumentationTestRunner into AndroidManifest.xml file and changing Run Configurations. After you finish - take a look on examples from here.
I found Espresso jar file https://code.google.com/p/android-test-kit/source/browse/#git%2Fbin%2Fespresso-dependencies . I added jar file to my project and it's very easily =).
I'm a long time Java developer with many years of Java EE, Ant, Maven, unit testing, mocks, etc. I'm currently using gradle to build android apps and am looking at unit testing them. And it's got me tearing my hair out!
My reading indicates that to test an app, I have to create another app inside it in a test directory. But I'm not sure how this embedded app can see the main apps classes. I presume that google came up with this because of something to do with the manifests which are different. I'm not sure why.
Without doing this embedded app system, I've been able to get unit tests to run by including the test classes with the main classes in the APK, adding the instrumentation declarations to the manifest, deploying it and running the test runners. But I don't want to be compiling test classes with app classes and including all the dependencies so that's not really an option and I'm not really sure of the effects of the changes to the manifest as I cannot find any documentation about the effects.
None of this is understood by gradle which follows the maven system of building. Also note that the android way seems to be that the embedded sub-project (test) is dependant on the main parent project, something that is totally contray to gradle and maven.
Another option seems to be separate the test project so that it's completely outside the app project. But really, I'd like to use a similar convention to maven and simply have tests in a separate directory, along with the manifest in test resources.
Has anyone managed to get testing on the emulators running unit tests following a more maven like directory structure?
You can try Robotium. It provides lots of features for a better testcase. You can have a look at it here.
Do you have to run the unit tests in the emulator? Isn't that too slow? I've been using robolectric ( http://pivotal.github.com/robolectric/ ) which creates shadow objects that work similar to mocks. I use progaurd ( http://proguard.sourceforge.net/ ) to strip out the tests for the release build.