Having both JUnit and Cucumber tests in a single Android project - android

I am looking to introduce Cucumber tests into my Android application. In order to do that, I need a custom test instrumentation runner that makes use of CucumberInstrumentationCore as described here.
However I already have Espresso tests that are driven by JUnit, and I therefore already have a custom test instrumentation runner that uses AndroidJUnitTestRunner.
It seems to me that a custom implementation cannot use both simultaneously, because the onStart() methods of each call finish() on the tested activity.
It seems that my options are:
Copy the source code of both CucumberInstrumentationCore and AndroidJUnitTestRunner and produce a new, combined test runner that performs the necessary parts of both
Or...
Have the JUnit tests in the androidTest folder, and create a new instrumentation test folder alongside androidTest which contains only the Cucumber tests. Is this even possible?
How can I proceed, and have I missed a more elegant way of doing this?

I ended up producing a combined instrumentationTestRunner which seems to work OK:
https://gist.github.com/dhoskins/98afa6976c87cb20328d42065c7292ee

Related

Is it possible to use dagger at android project unit testing?

I want to use Dagger + Robolectric in src/test/java package. Is it possible? How?
Somebody wrote their Robolectric tests at src/androidTest/java package then used Dagger, But Robolectric is a framework that runs your tests inside the JVM on your workstation in seconds.
If I write my test in src/androidTest/java package it won't run inside the JVM.
Anyway, Is it right to use Dagger at src/test/java package? Is it possible?
Yes, it's perfectly possible to use dagger in robolectric tests. But in general
You don’t have to use Dagger for single-class unit tests
https://google.github.io/dagger/testing.html

How to run a Junit5 test with Android dependencies and robolectric

I am trying to run a unit test like that:
#org.junit.jupiter.api.Test
void junit5codeCoverage() {
final int result = new Foo().junit5();
Assert.assertEquals(Looper.getMainLooper().getThread(), Thread.currentThread());
assertEquals(-1, result);
}
That is a Junit5 test with Android dependencies (i.e Looper.getMainLooper()) with Robolectric.
I am using the junit5 android plugin from mannodermaus that allows running junit5 within Android setups. But this does not work out of the box because it would not load robolectric. So I tried alixwar's branch that would tackle robolectric and junit5 test coverage, but still, would not use Android classes.
I furthermore started to investigate how to run a robolectric test on junit5, which would require understanding how the RobolectricTestRunner works and how to port the code to the JUnit5 platform. I have little understanding of the new junit5 platform, but I figured out that I could build on top of the org.junit.platform.runner.JUnitPlatform runner, to follow the test runner concept, which is part of the junit-platform-runner package. But this is so far away from the original Junit4 SandBoxTestRunner that I couldn't manage to complete the port.
So what would be the most feasible path to implement robolectric junit5 support, or is there any (obvious) concept I am missing?
I am trying to do this too, but it seems that at the time of this writing, Robolectric simply does not support junit5.
See the discussion here: https://github.com/robolectric/robolectric/issues/2996
Some workarounds are described in that discussion, but for now I am just going to stick to junit4.
You can use junit-vintage-engine to run test with runners from JUnit4.
Just add org.junit.vintage:junit-vintage-engine to your dependencies, thus you can org.junit.runner.RunWith annotation to your tests.

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.

Android JUnit4 testing

I want to run some JUnit4 tests. The code relies on some Android libraries(Android XML parser), but does not create any activites and so on. When I try to run tests I got that an Android class that I need was not found. Is there any way to run JUnit4 tests with Android code, not to test activity but to test code with some libraries.
I had the same problem and tried to adapt JUnit4 to Android's existing TestRunner - without success. Therefore I created a new project called JUnit4Android. It's a TestRunner application library for JUnit4 and JUnit3 tests and test suites. So you can run your existing JUnit4 tests with it. Please find more information on GitHub:
https://github.com/dthommes/JUnit4Android/wiki
There is no way (that I'm aware of) to use JUnit4 on Android. It does support JUnit3 though, if that's an option for you?
Alternatively, you could use Robolectric and run your tests on your development machine (where you'll be able to use whichever unit test framework you like). Whether this will work for you depends on exactly what you're testing, but it might be worth a go?
It might be little bit late, but there's finally an official update from Google about junit4:
based on Android-test-kit project and some other sources it's clear that:
The AndroidJUnitRunner is a new unbundled test runner for Android, which is part of the
Android Support Test Library and can be downloaded via the Android
Support Repository. The new runner contains all improvements of
GoogleInstrumentationTestRunner and adds more features:
- JUnit4 support
- Instrumentation Registry for accessing Instrumentation, Context and Bundle Arguments
- Test Filters #SdkSupress and #RequiresDevice
- Test timeouts
- Sharding of tests
- RunListener support to hook into the test run lifecycle
- Activity monitoring mechanism ActivityLifecycleMonitorRegistry
Actually, it's already presented in Support Repository. If You go to
%ANDROID_HOME%\extras\android\m2repository\com\android\support\test\testing-support-lib\
it's possible to find testing-support-lib in there (aar, jars etc.) which allows to use JUnit4. Even more, it contains espresso library same location which is handy for UI testing. Seems Android sites and support lib official references will be updated soon with that info.

Trying to understand android testing

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.

Categories

Resources