unit testing – Android: How to test a custom view?-ThrowExceptions

Exception or error:

There are several methods of unit testing in Android, what’s the best one for testing a custom view I’ve written?

I’m currently testing it as part of my activity in an instrumentation test case, but I’d rather test just the view, isolated.

How to solve:

Well unit testing is a method by which individual units of source code are tested to determine if they are fit for use. So when you say you want to test your custom view, you can check various methods of your custom views like “onTouchEvent”, “onDown”, “onFling”, “onLongPress”, “onScroll”, “onShowPress”, “onSingleTapUp”, “onDraw” and various others depending on your business logic. You can provide mock values and test it. I would suggest two methods of testing your custom view.

1) Monkey Testing
Monkey testing is random testing performed by automated testing tools. A monkey test is a unit test that runs with no specific test in mind. The monkey in this case is the producer of any input. For example, a monkey test can enter random strings into text boxes to ensure handling of all possible user input or provide garbage files to check for loading routines that have blind faith in their data. This is a black box testing technique and it can check your custom view in so many unique conditions that you will get astonished 🙂 .

2) Unit Testing

2a) Use Robotium Unit Testing Framwork

Go to Robotium.org or http://code.google.com/p/robotium/ and download the example test project. Robotium is a really easy to use framework that makes testing of android applications easy and fast. I created it to make testing of advanced android applications possible with minimum effort. Its used in conjunction with ActivityInstrumentationTestCase2.

2b) Use Android Testing Framework

Here are the links to the reference:
http://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2.html
and
http://developer.android.com/reference/android/test/ActivityUnitTestCase.html

For starters:
http://developer.android.com/guide/topics/testing/testing_android.html

According to one user : Aside from easily testing non platform
dependent logic I haven’t found a
clever way to run tests, so far (at
least for me) any actual platform
logic testing is cumbersome. It’s
almost non trivial anyway because I’ve
found differences in implementation
between the emulator and my actual
device and I hate to run a unit test
implementation on my device just to
remove the application afterwards.

My strategy has been: Try to be
concise and make the logic well
thought out and then test
implementation piece by piece (less
then desirable).

Also Stephen Ng provides good aproach for real Unit Test for Android projects solution: https://sites.google.com/site/androiddevtesting/

One user has made a screencast.

Here’s a ScreenCast I made on how I got Unit Tests to work. Simple Unit
Tests and more complex unit tests that
depend on having a reference to
Context or Activity objects.
http://www.gubatron.com/blog/2010/05/02/how-to-do-unit-testing-on-android-with-eclipse/

Hope it helps you testing your custom view in all possible conditions 🙂


Comment (futlib) All your suggestions seem to involve testing the ACTIVITY, while I really want to test just the VIEW. I might want to use this view in other activities, so it doesn’t make much sense for me to test it with a specific one. – futlib

Answer: To implement a custom view,
you will usually begin by providing
overrides for some of the standard
methods that the framework calls on
all views. For example “onDraw”,
“onKeyDown(int, KeyEvent)”,
“onKeyUp(int, KeyEvent)”,
“onTrackballEvent(MotionEvent)” etc of
your custom view. So when you want to
do unit testing for your custom you’ll
have to test these methods, and
provide mock values to it so that you
can test your custom view on all
possible cases. Testing these methods
doesn’t mean that you are testing your
ACTIVITY, but it means testing your
custom view (methods/functions) which
is within an activity. Also you’ll
have to put your custom view in an
Activity eventually for your target
users to experience it. Once
thoroughly tested , your custom view
can be placed in many projects and
many activities.

###

A simple solution for the lack of a View-focused TestCase implementation would be to create a simple Activity within your test project that includes your view. This will allow you to write tests against the view using a simple Activity. Information on Activity testing:

http://developer.android.com/reference/android/test/ActivityUnitTestCase.html

###

Here’s a different suggestion which works fine in many cases: Assuming you are referencing your custom view from within a layout file, you can use an AndroidTestCase, inflate the view, and then perform tests against it in isolation. Here’s some example code:

my_custom_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<de.mypackage.MyCustomView ...

MyCustomView.java:

public class MyCustomView extends LinearLayout {

    public MyCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setTitle(CharSequence title) {
        ((TextView) findViewById(R.id.mylayout_title_textView)).setText(title);
    }
...

MyCustomViewTest.java:

public class MyCustomViewTest extends AndroidTestCase {

    private MyCustomView customView;

    @SuppressLint("InflateParams")
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        customView = (MyCustomView) LayoutInflater.from(getContext())
            .inflate(R.layout.my_custom_layout, null);
    }

    public void testSetTitle_SomeValue_TextViewHasValue() {
        customView.setTitle("Some value");
        TextView titleTextView = (TextView) valueSelection.findViewById(R.id.mylayout_title_textView);
        assertEquals("Some value", titleTextView.getText().toString());
    }
...

Leave a Reply

Your email address will not be published. Required fields are marked *