How to debug instrumentation tests in Android Studio? - android

In Android Studio when I debug instrumentation test, the test won't stop on any breakpoint. Debugging unit tests works. I have a simple instrumented test that only checks if username edittext is displayed:
#RunWith(AndroidJUnit4.class)
public class LogonActivityTest {
#Rule
public ActivityTestRule<LogOnActivity> mActivityRule = new ActivityTestRule<>(LogOnActivity.class, true, false);
#Before
public void setUp() throws Exception {
mActivityRule.launchActivity(new Intent()); // breakpoint here
}
#Test
public void testSimple() throws Exception {
onView(withId(R.id.act_logon_et_username)).check(matches(isDisplayed())); // breakpoint here
}
}
In build.gradle I have properly set
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
How can I debug instrumented tests? I'm using Espresso, Mockito and Dagger 2.

You can solve this a couple of ways Tomask.
You can pass the option -e debug true in the configuration for your test if you're invoking the test from the command line.
Otherwise, and more simply, you should choose Debug instead of Run when starting your tests from android studio. If you click Run for your test from android studio, the option -e debug false gets set and the test(s) will not stop execution at breakpoints.
Hope this helps!

For me, switching to "Run Android instrumented Tests using Gradle" works fine.
Go to Android Studio -> Preferences and toggle this option.
If it is already enabled, try disabling it and hope for the best.

Related

Run specific instrumentation test suite with Gradle Managed Devices - Android

So with the release of Android Studio Dolphin & Beta of Electric Eel, I wanted to try the instrumentation tests in gradle. I do however want to exclude some of the tests being run, in order to be able to run specific test suites one at a time.
So here is what I configured so far:
android {
testOptions {
managedDevices {
devices {
pixel2api30 (com.android.build.api.dsl.ManagedVirtualDevice) {
device = "Pixel 2"
apiLevel = 30
systemImageSource = "aosp-atd"
}
}
}
}
}
I know I can run my entire suite using
./gradlew device-nameBuildVariantAndroidTest
In my case that would be
./gradlew pixel2api30gaeDebugAndroidTest
gaeDebug being my build variant. This command is being run in my project root.
If I want to run the tests in the tests/large folder for example
How would I go about doing that? Thanks.
For this you could use 2 different approaches:
Create and run test suites.
For example, for each of these folders, create a TestSuite and define Test classes. For example:
#RunWith(Suite.class)
#Suite.SuiteClasses({
ExampleLargeTest.class,
ExampleTwoLargeTest.class,
ExampleThreeLargeTest.class
})
public class LargeTestsSuite {
}
This suite can be run using following command
./gradlew pixel2api30gaeDebugAndroidTest - Pandroid.testInstrumentationRunnerArguments.class=path.to.your.suite.class
Use Test Categories
Annotate your Test classes like this:
#Category("Large")
public class ExampleLargeTest { ... }
And then you could execute the following command for running all tests with same category:
./gradlew pixel2api30gaeDebugAndroidTest -PtestCategory=Large
Hopefully one of these two approaches will suite you.

JUnit tests always pass when executing in device

This simple test
#RunWith(JUnit4::class)
class Test {
#Test
fun test() {
assert(false)
}
}
Unexpectedly, this passes when put in androidTest (both through Android Studio and in the terminal), but obviously fails as expected when put in test.
You need to use JUnit assertions for running tests. The base assert() functionality is normally disabled when running "production" code, so you cannot depend that a plain assert statement will throw an assertion exception.
Use:
org.junit.Asserts.assertTrue( false )
to make the test fail properly.

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.

Espresso Instrumentation Tests - how to uninstall or delete the app after the test

I setup Espresso instrumentation framework to run my Android Functional Automation tests.
For every test, I want to login to the app and delete the app after I finish the test.
So, I setup something like below:
public class FirstSampleTest extends BaseTest {
private final BaseTest baseTest;
// private final ElementUtils elementUtils;
public FirstSampleTest() throws InterruptedException {
this.baseTest = new BaseTest();
}
#Before
public void initiate() throws InterruptedException {
//I have setup login method here to login to the app after it installs
}
#Rule
public ActivityTestRule<SplashScreenActivity> splashScreenActivityActivityTestRule = new ActivityTestRule(SplashScreenActivity.class);
#Test
public void testTheHomeScreen() throws InterruptedException {
//Some tests go here.
}
#After
public void teardown() throws InterruptedException {
//I want to uninstall the app or delete it from the emulator once the test is run
}
}
You can add a gradle task in the Android Studio Before launch section in Run -> Edit Configurations.
Click + -> Add a gradle-aware Make -> :app:uninstallAll
note: "app" in :app:uninstallAll depends on your main module name. So it can be :my_module:uninstallAll, or :company:uninstallAll
Uninstalling the app from the Instrumentation tests is not possible. However, once all the tests are run, the app is uninstalled automatically.
Note: The app is not uninstalled only when a single test is run. Please run the whole build using the command
./gradlew connectedAndroidTest

Android Unit Test Not Reporting Failing with fail()

I've written a unit test that simply extends TestCase and I have the following:
public class MetricParserTests extends TestCase {
#Override
protected void setUp() throws Exception {
super.setUp();
}
#Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testFailure() {
fail("This needs to fail");
}
}
When I run my tests using ant test or adb shell am instrument I get the following results:
... [exec] OK (1 tests) ...
I'd expect to see a failure on the command line.
I believe I know what the issue is. I was able to reproduce the issue and solve it. The command you use does not rebuild and re-install your test project onto a device. When you call ant test it will just execute the tests which are already installed on that device.
What you need to call is the three commands in your test project's directory:
ant debug
ant installd
ant test
Then all tests will be rebuild and re-installed and latest tests will be executed. If you don't call debug and installd, the changes you did to the tests do not get applied.
I haven't had recent experience in Android testing, but here is what I have found...
You can use normal JUnit tests if your code is totally decoupled from Android (see here
for an example). This would run on your JVM using the JUnit runner.
However, if you are trying to run these tests on an Android device (either via ant, or the command line tools) then you need to create a full android test project (See here).
To test "on device" your test cases need to extend one of the Android test classes like ActivityInstrumentationTestCase2<T>
and are run using the InstrumentationTestRunner in the Dalvik VM on the Android device.
Using an IDE or the command-line tools to create a test project should create a sample test for you to work from.
This blog post linked from the comments of the post above is a good source of information, as is the Android Testing Fundamentals doc.
The method testFailure() does not have a #Test annotation. Is that correct?

Categories

Resources