Unit/Instrumentation tests Android Gradle. Instrumentation tests won't run - android

I have Tests set up in an Android project in Android Studio. I have a Unit test class with tests and an Instrumentation test class with tests. When I run gradlew connectedAndroidTest the unit tests run fine but the instrumentation tests are not run.
Here is my project structure
root
- app
- src
- androidTest
- java
- packagename
- UnitTest class
- InstrumentationTest class
The unit test class extends TestCase. The instrumentation test class extends ActivityInstrumentationTestCase2
An example test in the instrumentation test class is
public void testJSONReturnsString() {
String json = JSON.getJSonFeed(getActivity().getApplicationContext(), "http://foo.bar");
assertNotNull(json);
}
In my build.gradle I have
testInstrumentationRunner "android.test.InstrumentationTestRunner"
in the defaultConfig section
Does anyone know why the Instrumentation test class wouldn't run?

In the end it was because I didn't have a default constructor in my instrumentation class!

Related

Why I always get "No tests were found" "Class not found: xxx" when running the defualt local unit ExampleUnitTest class on Android?

I'm trying to use Unit testing using JUnit in my android project, so I tried to run the ExampleUnitTest class in the (test) folder with this statement:
Assert.assertEquals(4,5)
and it passed I don't know why then I created another class in the same folder and now I get the error even on the ExampleUnitTest class
No tests where found
with a message
Class not found: "{{packageName}}.ExampleUnitTest"
This is the ExampleUnitTest class
ExampleUnitTest class
This is the configuration
ExampleUnitTest configuration
Using this dependency:
testImplementation 'junit:junit:4.13.2'

Android connectedAndroidTest: NO tests found

Android connectedAndroidTest: NO tests found
gradle :aar-lib:connectedAndroidTest
com.android.builder.testing.ConnectedDevice > No tests found.
FAILED No tests found. This usually means that your test classes
are not in the form that your test runner expects (e.g. don't
inherit from TestCase or lack #Test annotations).
Test class:
#RunWith(AndroidJUnit4.class)
public class FooTest {
#Test
public void testFoo() {
....
}
}
Looked at the test APK, it contains the test class.

Failing to run Instrumented tests on a new Android Kotlin project

I just created a new android application with Kotlin Support.
When I've tried to run the default instrumented tests it does not run and shows me this message:
Class not found: "oussaki.com.pos.ExampleInstrumentedTest"Empty test suite.
This the Instrumented test class that I'm trying to run:
#RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
#Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getTargetContext()
assertEquals("oussaki.com.pos", appContext.packageName)
}
}
This is a known issue: https://issuetracker.google.com/issues/38452937 which hopefully will be fixed in the next release.
For the time being you can manually go to 'Edit Configurations' and add the configuration for the specific class/method you want to run under 'Android Instrumented Tests'.
You could also try the latest canary build: https://developer.android.com/studio/preview/index.html but personally I've had trouble getting it to work with my project.

Android Test Orchestrator and custom Application class

I gave a try to Android Test Orchestrator and it doesn't see any tests if Application class were changed. Pretty easy to reproduce.
In Android Studio 3 preview Beta 6 create simple project with wizard
Create custom runner like:
class CustomTestRunner : AndroidJUnitRunner() {
override fun newApplication(cl: ClassLoader?, className: String?, context: Context?): Application {
return super.newApplication(cl, TestApplicationClass::class.simpleName, context)
}
}
Replace instrumentation runner with new one
No test found after running instrumented tests
Any ideas? Looks like the Orchestrator depends on application class name from manifest.
I use this configuration to use special Dagger dependencies for tests.
I had the similar issue, with a custom test runner. Make sure that your TestApplicationClass does not crash at runtime. If the custom runner crashes then orchestrator will not be able to get information about the tests and will return the message:
No tests found. This usually means that your test classes are not in the form that your test runner expects (e.g. don't inherit from TestCase or lack #Test annotations).
That is what happened to me in my custom runner.
Good luck!

Gradle JUnit android run all but #FlakyTest

I have a few tests that have multiple annotations:
#Test
#LargeTest
#FlakyTest
I found a way to run only #LargeTest from here.
./gradlew app:connectedAndroidTest
-Pandroid.testInstrumentationRunnerArguments.size=large
Is there a way to run all Instrumentation Tests except those annotated as #FlakyTest?
android {
defaultConfig {
testInstrumentationRunnerArgument 'notAnnotation', 'android.test.suitebuilder.annotation.FlakyTest'
}
}

Categories

Resources