220 likes | 497 Views
Unit Test on Android Application . What is Unit Test. Unit testing is a method by which individual units of source code are tested to determine if they are fit for the use. A unit test provides a strict, written contract that the piece of code must satisfy the purpose.
E N D
What is Unit Test • Unit testing is a method by which individual units of source code are tested to determine if they are fit for the use. • A unit test provides a strict, written contract that the piece of code must satisfy the purpose. • Unit tests find problems early in the development cycle.
Why Unit Test? We have to write the following function: Boolean ValidatePassword(String sPwd) { //returns true if the password contains at least 6 characters with at least 1 digit and 1 capital alphabet. }
Why Unit Test? • The programmer wrote the function. • Now how to verify the function is accurate? • Hire great developers (But even great developers make very small mistakes.) • Review (But the reviewer might be wrong and it doesn’t leave any documentation.) • Leave the responsibility on the client. (Easier, but the client won’t probably buy anything from you in future) • . . . • . . .
How about this? UnitTestTestValidatePassword() { //test 6 characters ASSERTEQUAL(ValidatePassword(“”), false); ASSERTEQUAL(ValidatePassword(“12345”), false); //test for capital alphabet and digit. ASSERTEQUAL(ValidatePassword(“myname”), false); ASSERTEQUAL(ValidatePassword(“myname1”), false); ASSERTEQUAL(ValidatePassword(“Myname1”), true); ASSERTEQUAL(ValidatePassword(“A12345”), true); return UnitTest::Success; }
Why Unit Test (cont) • Unit Test provides these facilities. • Warns the developer if its not working. • Works as a good source of documentation for other developers. • Help developers to track down which part of the program is broken easily.
Unit Test for Android App • We can create unit test that will work with even Activities. • For details how to write test case on Activities refer to this site: http://developer.android.com/resources/tutorials/testing/activity_test.html
In Eclipse, select New > Project > Android > Android Test Project
Now select an existing project on which you want to add test.
Now we have to add a test class in the test project. • In Eclipse, open the test project (in our case TrapDetector1Test) if it is not already open. • expand the src/ folder and then find the package icon for com.example.trapdetect.test. Right-click on the package icon and select New > Class:
In the dialog, enter the following: • Name: “TrapDetect1Test”. This becomes the name of your test class. • Superclass:"android.test.ActivityInstrumentationTestCase2<TrapDetector1>". The superclass is parameterized by an Activity class name. The dialog should now look like this:
The next step is to define the test case class. It should have the following basic methods: • A Cosntructor: This defines the constructor for the class. It is required by the Android testing framework. • setUp(): This overrides the JUnit setUp() method. You use it to initialize the environment before each test runs. • testFunc1() – the function that you want to run as your test case. • tetstFunc2() • ….. • ……