1 / 14

Unit Test on Android Application

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.

cade
Download Presentation

Unit Test on Android Application

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit Test on Android Application

  2. 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.

  3. 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. }

  4. 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) • . . . • . . .

  5. 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; }

  6. 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.

  7. 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

  8. In Eclipse, select New > Project > Android > Android Test Project

  9. Now select an existing project on which you want to add test.

  10. 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:

  11. A dialog will appear like this.

  12. 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:

  13. 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() • ….. • ……

More Related