Android unit testing


SDK Version: 
M3

For unit testing you can use the built in JUnit framework just like in "standard" java applications.

A unit test is to test some low-level part - usually a single class - of a project, working individually.

The only trick is to extend AndroidTestCase in your test case instead of TestCase some Android specific methods, like getContext() which is often required by android functions.

Lets see a very simple example using eclipse:

- Create a new project to test, create a new class in it, with a simple method that , like this:

  1. public class ClassToTest{
  2.        
  3.    public int add(int arg1,int arg2){
  4.            return arg1+arg2;
  5.    }
  6.    
  7. }

- Create a new test project: File/new/Other.../Android/Android Test Project


new Android Test Project

Add the previously created project as the test target, the other fields can be left as they are auto filled.

- Add a test case to the test project, by left clicking on the package new/other../java/junit test case


new test case

Be sure tho change superclass to junit.framework.TestCase. For some reason eclipse does not fill out constuctor properly, so just uncheck it for now.

The automatically generated testcase will look like this:

  1. import com.helloandroid.projecttotest.ClassToTest;
  2.  
  3. import android.test.AndroidTestCase;
  4.  
  5. public class TestCaseName extends AndroidTestCase {
  6.         protected void setUp() throws Exception {
  7.                 super.setUp();
  8.         }
  9.  
  10.         protected void tearDown() throws Exception {
  11.                 super.tearDown();
  12.         }
  13. }

The setUp() method runs once before all test methods, the tearDown() runs after all tests are runned.

Now add a testing method, and do some initialization in setUp():

  1. public class TestCaseName extends AndroidTestCase {
  2.         ClassToTest mClassToTest;
  3.         int mArg1;
  4.         int mArg2;
  5.         protected void setUp() throws Exception {
  6.                 mClassToTest=new ClassToTest();
  7.                 mArg1=6;
  8.                 mArg2=3;
  9.                 super.setUp();
  10.         }
  11.  
  12.         protected void tearDown() throws Exception {
  13.                 super.tearDown();
  14.         }
  15.        
  16.         public void testAdd(){
  17.                 assertEquals(9,mClassToTest.add(mArg1,mArg2));
  18.         }
  19. }

Now if you run the test project as an android test project you will hopefully see this screen indicating that all tests passed:


test passed

Comments

Nice information provide here... Thanks

Nikmaya John

Friendzone

Thank you for this tutorial. Had been trying to unit testing, but I am new to it, I did encounter a few puzzling questions and even a couple of headaches.@research paper

thank you for information

حجز فنادق مكة
فنادق مكة

I thank the very Code, which provides management services Almmmezp each member who participates Bmodia deals and the progress of the new and more Thank you
شات
دردشة
شات مصرى
شات مصرية
دردشة مصرية
دردشة مصر
منتديات بحبك
شات بنات
شات مصر
شات بنات مصر
العاب
دليل
اختصار روابط

it is sometimes hard to Pandora in a world of adults Pandora Bracelets