Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 在Android Studio中進行單元測試和UI測試

在Android Studio中進行單元測試和UI測試

編輯:關於Android編程

聽說不會單元測試的Android程序員不是一個合格的程序員,尴尬,好吧,雖然學習Android好久,最近在使用AndroidStudio,但不知道AndroidStudio自帶單元測試功能,今天了解了一下AndroidStudio下如何使用單元測試。     代碼中盡量不要使用全局變量,因為單元測試不會獲取到全局變量的值! 除了AndroidStudio自帶的單元測試,還有很多測試工具,需要以後多多學習!   我的簡單計算器代碼
package com.example.zhouhui.mysimplecalculator;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    EditText ContentText;
    Button CButton;
    Button DivButton;
    Button MulButton;
    Button DelButton;
    Button SevenButton;
    Button EightButton;
    Button NineButton;
    Button SubButton;
    Button FourButton;
    Button FiveButton;
    Button SixButton;
    Button AdditionButton;
    Button OneButton;
    Button TwoButton;
    Button ThreeButton;
    Button RemainderButton;
    Button ZeroButton;
    Button EqualButton;

    boolean isClickEqu = false; // 是否點擊=


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ContentText = (EditText) findViewById(R.id.content_text);
        CButton = (Button) findViewById(R.id.c_button);
        DivButton = (Button) findViewById(R.id.div_button);
        MulButton = (Button) findViewById(R.id.mul_button);
        SubButton = (Button) findViewById(R.id.sub_button);
        AdditionButton = (Button) findViewById(R.id.addition_button);
        RemainderButton = (Button) findViewById(R.id.remainder_button);
        DelButton = (Button) findViewById(R.id.del_button);
        SevenButton = (Button) findViewById(R.id.seven_button);
        EightButton = (Button) findViewById(R.id.eight_button);
        NineButton = (Button) findViewById(R.id.nine_button);
        FourButton = (Button) findViewById(R.id.four_button);
        FiveButton = (Button) findViewById(R.id.five_button);
        SixButton = (Button) findViewById(R.id.six_button);
        OneButton = (Button) findViewById(R.id.one_button);
        TwoButton = (Button) findViewById(R.id.two_button);
        ThreeButton = (Button) findViewById(R.id.three_button);
        ZeroButton = (Button) findViewById(R.id.zero_button);
        EqualButton = (Button) findViewById(R.id.equal_button);

        CButton.setOnClickListener(new MyClickListener());
        DivButton.setOnClickListener(new MyClickListener());
        MulButton.setOnClickListener(new MyClickListener());
        DelButton.setOnClickListener(new MyClickListener());
        SevenButton.setOnClickListener(new MyClickListener());
        EightButton.setOnClickListener(new MyClickListener());
        NineButton.setOnClickListener(new MyClickListener());
        SubButton.setOnClickListener(new MyClickListener());
        FourButton.setOnClickListener(new MyClickListener());
        FiveButton.setOnClickListener(new MyClickListener());
        SixButton.setOnClickListener(new MyClickListener());
        AdditionButton.setOnClickListener(new MyClickListener());
        OneButton.setOnClickListener(new MyClickListener());
        TwoButton.setOnClickListener(new MyClickListener());
        ThreeButton.setOnClickListener(new MyClickListener());
        RemainderButton.setOnClickListener(new MyClickListener());
        ZeroButton.setOnClickListener(new MyClickListener());
        EqualButton.setOnClickListener(new MyClickListener());
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    public double compute(String text) {

        double result = 0;
        double num1 = 0;
        double num2 = 0;
        char temp = 0;
        if(text.contains("+")) {
            temp = '+';
        } else if(text.contains("-")) {
            temp = '-';
        } else if(text.contains("*")) {
            temp = '*';
        } else if(text.contains("/")) {
            temp = '/';
        } else if(text.contains("%")) {
            temp = '%';
        }

        try{
            num1 = Double.valueOf(text.substring(0,text.lastIndexOf(temp)));
            num2 = Double.valueOf(text.substring(text.lastIndexOf(temp) + 1));
        }catch (Exception e) {
            num1 = 0;
            num2 = 0;
        }

        if(temp == '+') {
            result = num1 + num2;
        } else if(temp == '-') {
            result = num1 - num2;
        } else if(temp == '*') {
            result = num1 * num2;
        } else if(temp == '/') {
            result = num1 / num2;
        } else if(temp == '%') {
            result = num1 % num2;
        }

        Log.i("MainActivity",result + " = " + num1 + temp + num2);
        return result;
    }

    public void clickNumber(int n) {
        if(isClickEqu) {
            ContentText.setText(String.valueOf(n));
            isClickEqu = false;
        } else {
            String text = ContentText.getText().toString();
            ContentText.setText(text + String.valueOf(n));
        }
    }

    public void clickOp(char temp) {
        String text = ContentText.getText().toString();
        if(temp == '+' || temp == '-' || temp == '*' || temp == '/'
                || temp == '%') {
            if(text == null || text.equals("")
                    || text.contains("+") || text.contains("-")
                    || text.contains("*") || text.contains("/")
                    || text.contains("%")) {
                return;
            }
            isClickEqu = false;
            ContentText.setText(text + temp);
        } else if(temp == '=') {
            if(text == null || text.equals("")) {
                return;
            }
            double result;
            result = compute(text);
            ContentText.setText(String.valueOf(String.valueOf(result)));
            isClickEqu=true;
        } else if(temp == 'd') {
            isClickEqu=false;
            try {
                ContentText.setText(text.substring(0, text.length()-1));
            } catch (Exception e) {
                ContentText.setText("");
            }
        } else if(temp == 'c') {
            isClickEqu=false;
            ContentText.setText("");
        }
    }

    class MyClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.c_button:
                    clickOp('c');
                    break;
                case R.id.del_button:
                    clickOp('d');
                    break;
                case R.id.addition_button:
                    clickOp('+');
                    break;
                case R.id.sub_button:
                    clickOp('-');
                    break;
                case R.id.mul_button:
                    clickOp('*');
                    break;
                case R.id.div_button:
                    clickOp('/');
                    break;
                case R.id.remainder_button:
                    clickOp('%');
                    break;
                case R.id.equal_button:
                    clickOp('=');
                    break;
                case R.id.zero_button:
                    clickNumber(0);
                    break;
                case R.id.one_button:
                    clickNumber(1);
                    break;
                case R.id.two_button:
                    clickNumber(2);
                    break;
                case R.id.three_button:
                    clickNumber(3);
                    break;
                case R.id.four_button:
                    clickNumber(4);
                    break;
                case R.id.five_button:
                    clickNumber(5);
                    break;
                case R.id.six_button:
                    clickNumber(6);
                    break;
                case R.id.seven_button:
                    clickNumber(7);
                    break;
                case R.id.eight_button:
                    clickNumber(8);
                    break;
                case R.id.nine_button:
                    clickNumber(9);
                    break;
                default:
                    break;
            }
        }
    }
}
以及單元測試:
package com.example.zhouhui.mysimplecalculator;

import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.Button;
import android.widget.EditText;

import org.junit.Test;

/**
 * Created by xxxxxxx on 16/7/7.
 */
public class MainActivityTest extends ActivityInstrumentationTestCase2{

    private MainActivity mActivity;
    Instrumentation mInstrumentation;
    EditText ContentText;
    Button CButton;
    Button DivButton;
    Button MulButton;
    Button DelButton;
    Button SevenButton;
    Button EightButton;
    Button NineButton;
    Button SubButton;
    Button FourButton;
    Button FiveButton;
    Button SixButton;
    Button AdditionButton;
    Button OneButton;
    Button TwoButton;
    Button ThreeButton;
    Button RemainderButton;
    Button ZeroButton;
    Button EqualButton;

    public MainActivityTest() {
        super(MainActivity.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        setActivityInitialTouchMode(false);
        mInstrumentation = getInstrumentation();
        //獲取被測的Activity
        mActivity = getActivity();
        //獲取這個活動界面的控件
        ContentText = (EditText) mActivity
                .findViewById(com.example.zhouhui.mysimplecalculator.R.id.content_text);
        CButton = (Button) mActivity.findViewById(R.id.c_button);
        DivButton = (Button) mActivity.findViewById(R.id.div_button);
        MulButton = (Button) mActivity.findViewById(R.id.mul_button);
        SubButton = (Button) mActivity.findViewById(R.id.sub_button);
        AdditionButton = (Button) mActivity.findViewById(R.id.addition_button);
        RemainderButton = (Button) mActivity.findViewById(R.id.remainder_button);
        DelButton = (Button) mActivity.findViewById(R.id.del_button);
        SevenButton = (Button) mActivity.findViewById(R.id.seven_button);
        EightButton = (Button) mActivity.findViewById(R.id.eight_button);
        NineButton = (Button) mActivity.findViewById(R.id.nine_button);
        FourButton = (Button) mActivity.findViewById(R.id.four_button);
        FiveButton = (Button) mActivity.findViewById(R.id.five_button);
        SixButton = (Button) mActivity.findViewById(R.id.six_button);
        OneButton = (Button) mActivity.findViewById(R.id.one_button);
        TwoButton = (Button) mActivity.findViewById(R.id.two_button);
        ThreeButton = (Button) mActivity.findViewById(R.id.three_button);
        ZeroButton = (Button) mActivity.findViewById(R.id.zero_button);
        EqualButton = (Button) mActivity.findViewById(R.id.equal_button);
    }

    @Test
    public void testonCreate() throws Exception {
    }

    @Test
    public void testPreCondition() {
        assertNotNull(mActivity);
        assertNotNull(ContentText);
        assertNotNull(CButton);
        assertNotNull(DivButton);
        assertNotNull(MulButton);
        assertNotNull(SubButton);
        assertNotNull(AdditionButton);
        assertNotNull(RemainderButton);
        assertNotNull(DelButton);
        assertNotNull(SevenButton);
        assertNotNull(EightButton);
        assertNotNull(NineButton);
        assertNotNull(FourButton);
        assertNotNull(FiveButton);
        assertNotNull(SixButton);
        assertNotNull(OneButton);
        assertNotNull(TwoButton);
        assertNotNull(ThreeButton);
        assertNotNull(ZeroButton);
        assertNotNull(EqualButton);
    }

    @Test
    public void testcompute() throws Exception {

        assertEquals(0d, mActivity.compute("0"));
        assertEquals(0d, mActivity.compute("1234"));
        assertEquals(3d, mActivity.compute("1+2"));
        assertEquals(55d,mActivity.compute("11+44"));
        assertEquals(11111111155d,mActivity.compute("11111111111+44"));
        assertEquals(1d, mActivity.compute("2-1"));
        assertEquals(-1d, mActivity.compute("1-2"));
        assertEquals(2d, mActivity.compute("1*2"));
        assertEquals(2d, mActivity.compute("2/1"));
        assertEquals(1.5d, mActivity.compute("3/2"));
        assertEquals(Double.POSITIVE_INFINITY, mActivity.compute("2/0"));
        assertEquals(Double.NaN, mActivity.compute("5%0"));
        assertEquals(1d, mActivity.compute("3%2"));
        assertEquals(0d, mActivity.compute("3%1"));
    }

    @Test
    public void testclickNumber() throws Exception {

    }

    @Test
    public void testclickOp() throws Exception {

    }

}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved