Run junit test cases from multiple class files (Robotium) - android

I am actually using Robotium testing framework for Android, but I believe it's based on JUnit. I am new to JUnit as well as TDD.
I want to create different test class files based on features, like "LoginTest.java", "EditProfileTest.java", and each file contains some test cases for that feature.
The question is that, when I run it within Eclipse by clicking F11, only one file will run. I don't want to put all test cases in a single file. Is there a way to run multiple files at once? Or other ways to solve this problem.

You can either create a JUnit Test Suite, or if you just want to run all tests in all the classes in a particular java package you can do that in Eclipse by right clicking on the package, choosing Run As, and Android JUnit Test.

Related

Is it necessary to integrate the testing code within the target project if only use UI Automator?

I was wondering if maybe we could just use a separate project to hold the testing project, instead of integrating it within the target project.
I know we should use Espresso, but in our specific case, our team decided to use UI Automator only. The advantage of using a separate project to hold the testing project is:
Building is much faster, since we don't need to compile the whole large target project every time. And only build the separated mock project.
Debugging is much faster, in the case of using ./gradlew :connectedAndroidTest , android would remove the app after test. That means we will have to login the app every time for the new test. In the case of separated project, only the mock project be removed, and the target app keeps still as-is.
You can use UiAutomator to test many different components even some that you are not developing and don't belong to your application (i.e. Home Screen).
Then, you can create an independent project to test whatever you want.

A way to share code between multiple android test modules

I want to achieve something like this:
[ComponentTestsModule] com.android.test
[FunctionalTestsModule] com.android.test
both depends on
-> [TestLibraryModule] ?
which depends on
-> [AppModule] com.android.application
Is there any way to do it with android Gradle plugin 3.0+?
Why I need multiple test modules?
I want different test runners for different types of tests, also target different variants.
It is working right now with single codebase under androidTest, but with ugly switches in the custom test runner.
Why I need a test library module?
I want to share the same page-objects between different types of tests, and maybe some utility code.
Problem is: that page objects must have access to R class of app (locators: R.id.*)
None of the module types I'm aware of can depend on APK-producing module, expect from com.android.test, but i cannot depend from com.android.test with another com.android.test.
I have recently faced this problem and just want to share my solution in case some body needs it.
What I have done so far (not a perfect solution - I guess, but at least it works).
Create a new module named testing_base or some thing like that
In testing_base module only put things related to testing (Like the code you want it to be shared between modules) in to normal packages, NOT in the test packages/folders.
From others module, try to import things from testing_base module
ex: testImplementation project(":testing_base")
Hope it can helps someone, happy testing!
I want different test runners for different types of tests
To run a test class with a specific test runner, use the #RunWith annotation.
also target different variants
To target a specific variant, create your tests in app/src/androidTestVariantName for instrumented tests or in app/src/testVariantName for unit tests.

Which test to run when using android studio for unit testing

I am working through an android app tutorial course on udacity.com. I have come to a lesson where it's introducing testing. However, the video for the current class is showing how to run a test where only one run test option is available. seen here: https://youtu.be/CHb8JGHU290?t=170
but my android studio shows a number of options
and I am not sure what is the correct one to use, or even what the differences between them are. Could anyone shed some light on why I have the 4 different choices and what they are? In particular the first and second options are confusing me. the third and fourth options are intuitive enough to understand.
Thank you.
The options you are given are:
1- Run tests using Gradle:
This has been added in version 1.1 of Android Studio, to run tests using Android's build system, Gradle.
2- Run tests using Android JUnit, which will probably require a device/emulator. This is the option to use if you have test cases that make use of Android's test suite, like AndroidTestCase, also useful for running more complex and Android-related test cases.
3/4 - Run using JUnit framework. In your case, the only difference between these options is that the first indicates All Tests available in the project, while the last option offers running all tests existing in the specified package. In your case, probably both options are equivalent.
If you are running basic unit tests, I'd definitely stick with the first option.
More details on Android Studio testing here:
http://tools.android.com/tech-docs/unit-testing-support

Android Junit Testing vs. Normal Junit Testing

I'm taking over an android project and I wish to introduce unit tests to the project, to help avoid possible regressions.
For normal java projects, I have two source folders: src and test. The src source folder contains all of my source files and my test source folder contains all of my unit tests, which I believe is pretty standard for keeping test separate from the source, so you don't have to ship with them.
I've been doing some reading online and the approach with android apps looks to be a little bit different. Several examples talk about setting up a 2nd project for an android test project and then referencing the android project.
I wish to confirm a few things:
Is having a 2nd project for the testing the appropriate thing to do when it comes to testing android projects or am I just finding bad examples?
Should all unit tests be android unit tests? E.g. Yes they should all be, or no I should mix between android unit tests and junit because junits have less overhead.
What additional benefits do android unit tests give over junit tests? E.g. Handles to the emulator, etc.
Is having a 2nd project for the testing the appropriate thing to do
when it comes to testing android projects or am I just finding bad
examples?
Yes, it's typical to have a separate "test project" for testing Android specific code. http://developer.android.com/tools/testing/testing_android.html
Should all unit tests be android unit tests? E.g. Yes they should all
be, or no I should mix between android unit tests and junit because
junits have less overhead.
You'll usually have a mix because you can't test Android specific code on a standard JVM with regular ole' JUnit (not without some helper libraries, more on that in a moment).
In practice I've found that it makes sense to divide your application into plain JVM components and Android portions. For instance, if you need to communicate with a REST API you can have a separate component that does only this and which is plain Java. These types of components can easily be tested with standard JUnit. This type of architecture also leads to a clearer separation of responsibility and often and easier to understand and maintain design as well. (Such components can be included in your Android app as regular JARs.)
What additional benefits do android unit tests give over junit tests?
E.g. Handles to the emulator, etc.
Android testing can be slow and painful because a full Android test runs the Android stack in an emulator (or on a device). Android tests are however necessary for testing the parts of your application that are Android specific, such as Context/Activity/Service and so on.
Because of the cumbersome and slow nature of native Android tests several frameworks have been created that mock or stub parts of the SDK and take different approaches to help. You may want to look into Robolectric and Robotium, for instance. (These each have their own pros and cons.)

Creating a test project to test classes in another project in Android

I have a project with many classes and activities. I would like to test some of those classes by creating another project and using them. Is this possible? I tried creating an Android test project in Eclipse, linking to the project I want to test, creating an activity and there using some of those classes. Unfortunately, I get NoClassDefFoundError. Could you please address me to the right direction on how to create this kind of test?
Thanks!
What you are looking for is JUnit testing. There is plenty out there to get you started.
To test activities you can fire intents using ActivityInstrumentationTestCase2

Categories

Resources