Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • TDD to test android activity

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 210
    Comment on it

    Test driven development is basically a procedure to achieve actual functionality or goal. This is also called in short TDD. This process contains following steps that are :

     

        Write test cases
        Run test cases
        Implement codes to pass test
        Run all tests again
        Refactor if test fails

     

    A TDD is also called as RGR process that means Red Green Refactor process.

    Red - While writing codes we are in Red state.

    Green - If all tests pass then we are in green state.

    Refactor - If any of test case fails then it goes to refactor state.

     

    Example : Sum of two number

    Test case : we are expecting that sum of two number should be 8 otherwise test will failed.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.devjiva.unittesting1.MainActivity">
    
        <EditText
            android:layout_width="wrap_content"
            android:id="@+id/et1"
            android:inputType="number"
            android:layout_height="wrap_content"
            android:hint="Enter first value" />
    
        <EditText
            android:layout_width="wrap_content"
            android:id="@+id/et2"
            android:inputType="number"
            android:layout_height="wrap_content"
            android:hint="Enter first value" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnAdd"
            android:text="Add" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_marginTop="20dp"
            android:id="@+id/tvShow"
            android:layout_height="wrap_content"
            android:hint="Sum is :: " />
    
    </LinearLayout>
    

    MainActivity class to perform sum operation :

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final EditText et1 = (EditText)findViewById(R.id.et1);
            final EditText et2 = (EditText)findViewById(R.id.et2);
            Button btnAdd = (Button)findViewById(R.id.btnAdd);
            final TextView tvShow = (TextView)findViewById(R.id.tvShow);
    
            et1.setText("5");
            et2.setText("3");
    
            btnAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int sum = Integer.parseInt(et1.getText().toString())+Integer.parseInt(et2.getText().toString());
                    tvShow.setText(sum+"");
                }
            });
    
        }
    }
    

    Application test class that will check for test cases:

    public class ApplicationTest extends ActivityInstrumentationTestCase2<MainActivity> {
    
        private TextView result;
        private MainActivity mainActivity;
    
        public ApplicationTest() {
            super("com.devjiva.unittesting1", MainActivity.class);
        }
    
        protected void setUp() throws Exception {
            super.setUp();
    
            mainActivity = this.getActivity();
            result = (TextView) mainActivity.findViewById(R.id.tvShow);
    
        }
    
        public void tearDown() throws Exception {
            // Code that you wish to run after each test
        }
    
        public void testAdd() {
    
            mainActivity = this.getActivity();
            Button button = (Button) mainActivity.findViewById(R.id.btnAdd);
            TouchUtils.clickView(this, button);
            assertEquals("8",result.getText().toString());
    
        }
    
    }

    After running test case we will get green light in Run tab of android studio that means code implementation are right otherwise check the code and refactor again if light goes red.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: