Creating a test project to test classes in another project in Android - 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

Related

Using Robolectric with an Application class

I am getting started with using Robolectric for testing my android app. A bit of context, my app is already built and the repo is fairly large.
I setup my IDE as instructed in the Robolectric documentation here. But trying to run the tests, I ended up with errors in my application class.
It complains that some or the other dependency is missing or running into weird states which I can't explain.(Not a great start I guess).
Is there a way, I can basically mock the application class or have a dummy application class ?
I tried to find examples but it's not clear, where I am supposed to specify the class.
Any help is much appreciated.

Is it possible to get rid of ActivityInstrumentationTestCase2 using Support Test Library?

I'm trying to use Android Support Test Library (previously android-test-kit) and create tests for one of my projects. It contains some 'functional' code along with UI code in single project.
My intention is to use JUnit4 and not mix with JUnit3, so I use AndroidJUnitRunner.
There's number of test classes created for the 'functional' code which follow JUnit4 and work fine.
Recently I've started to implement tests for UI components. Referring documentation receipt number of tests have been implemented. These activity tests follow JUnit4 style and extend ActivityInstrumentationTestCase2 based on above doc.
The problem that I'm facing now: it's not possible to execute all tests at once. If I try to run package / all tests (Activity and 'functional' are presented in the same project) from IntelliJ Idea - only pure JUnit4 tests get executed. I suspect that the problem is in ActivityInstrumentationTestCase2 which extends TestCase and is not pure JUnit4 test class.
Is it possible somehow to get rid of ActivityInstrumentationTestCase2 in Activity tests using Android Support Test Library (android-test-kit) and have all test running? Or my assumption about the issues rootcause (about TestCase) is wrong?
InstrumentationTestCase, ActivityTestCase and ActivityInstrumentationTestCase2 are deprecated, you should be using ActivityTestRule instead. InstrumentationRegistry is also useful to get a hold of application context and instrumentation instances.
Finally found only possible solution: copy InstrumentationTestCase, ActivityTestCase and ActivityInstrumentationTestCase2 into my project from AOSP source. After that, remove inheritance from InstrumentationTestCase of TestCase. Same way it's done in this project (AndroidJunit4).
There's actually no need for TestCase and these changes and documentation receipt are enough to have activity tests to be JUnit4 and avoid mixing issues.

Android: jUnit Test Android Library

I am trying to test a library developed by me through jUnit, but I'm having some problems. This library requires an activity to be instantiated. However, following the tutorial (http://www.vogella.com/articles/JUnit/article.html), I can not instantiate the object because I dont have the activity. You can create a dummy activity?
Thanks :)
To test an Activity you should normally use ActivityInstrumentationTestCase2. When you invoke getActivity() the Activity under test will be created.
If you are testing a library project, the post android: testing library project would be also of help.
Unit testing Android applications is tricky because of the stubbed out API implementation ("java.lang.RuntimeException: Stub!")
If you want to unit test your Android Activities and such, Robolectric is a nice option: http://pivotal.github.com/robolectric/

Run junit test cases from multiple class files (Robotium)

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.

How to test a project using instrumentation test in android

I want to test my project using an instrumentation test. I am a beginner at this.
Can anybody provide a basic tutorial for this?
Robotium is a great tool for testing Android app's you should take a look at it and it's very easy to use.
http://code.google.com/p/robotium/
To use ActivityInstrumentationTestCase2 we can create a wrapper over it. In order to do so you can try out example that this blog talks about.
testing Activities in simple way
The idea used is to take out common code in wrapper called BaseActivityTest so that while testing you just need to do minimum steps, and your activity gets tested too.

Categories

Resources