简体   繁体   中英

Start JUnit test on button click (Android)

I need to start running my JUnit tests after a button has been clicked on the Main activity, This is so I can gather information from the text fields from an activity and on button press start running the JUnit tests based on values entered. Not to sure how to approach this so far I have the following:

import android.test.SingleLaunchActivityTestCase;
import android.widget.Button;
import android.widget.EditText;
import pso.algo.MainActivity;
import pso.algo.R;

public class Test extends SingleLaunchActivityTestCase<MainActivity>{

    EditText editKilos, editPounds;
    MainActivity activity;
    private String amount = "";


    public Test(String name) {
        super("pso.algo", MainActivity.class);
        setName(name);
    }

    protected void setUp() throws Exception {
        super.setUp();
        // Find views
        activity = getActivity();
        // Test needs to start after button press
        Button button = (Button) activity.findViewById(R.id.button);
        // Get text from view to later convert to integer
        EditText text = (EditText) activity.findViewById(R.id.editText);
        this.amount = text.getText().toString();
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void test(){
        // Test code goes here based on the amount given
    }
}

So far the activity opens but closes before information can be entered/button press. So what I am looking for is to stall the activity/tests until the button on the activity has been pressed.

Thanks!

You shouldn't be interacting with the application while doing unit tests. You should launch the unit tests and let the application run through those tests and report those results back to you. What you are trying to do is nowhere near best practice and I would recommend you read into what unit tests are for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM