If you've finished writing Android apps, you can do a test to learn this method, as follows:
Build Tests with Eclipse
First build the test environment:
-
Download Installation Eclipse ADT Plugin
-
Import or create an Android app project that we want to test.
-
Build a test project that corresponds to the application project test. Build a test project for the import Project: A. In the project browser, right-click on our app and select Android Tools > New Test Project B. In the new Android test Project panel, set the appropriate parameters for our test project, then click Finish
When the above environment is set up, you can enter to create and execute test cases:
Create a test case
Activity testing is written in a structured way. Be sure to place the test code in a separate package, separating it from the code being tested.
By convention, the name of the test package should follow the same naming method as the app package name, followed by ". Tests" after the package name is applied. In the test package that you created, add a Java class for our test case. By convention, the test case name should also follow the same name as the Java or Android class to be tested, and add the suffix "test".
To create a new test case in Eclipse, you can follow these steps:
A. In the Package Explorer, right-click on the src/folder for the project under test,New >package.
B. Set the folder name <你的包名称>.tests (for example, com.example.android.testingfun.tests ) and click Finish.
C. Right-click on the created test package and select New > Calss.
D. Set the file name <你的Activity名称>Test (for example, MyFirstTestActivityTest ) and click Finish.
Set up a test data set (Fixture)
The test dataset contains some of the objects that must be generated before running the test. To build a test data set, you can overwrite the setup () and teardown () methods in our tests. The test automatically executes the setup () method before any other test methods are run. We can use these methods to make the tested code separate from the test initialization and cleanup.
Set up a test dataset in your eclipse:
1. Double-click a test in the package Explorer to open the test case that you wrote before, and then modify the test case to inherit the Activitytestcase subclass. Like what:
public class Myfirsttestactivitytest extends Activityinstrumentationtestcase2<myfirsttestactivity> {
2. Next, add the constructor and Setup () method to the test case and add a variable declaration for the activity we want to test. Like what:
public class myfirsttestactivitytest extends activityinstrumentationtestcase2<myfirsttestactivity> { private myfirsttestactivity mfirsttestactivity; private textview mfirsttesttext; public myfirsttestactivitytest () { super (myfirsttestactivity.class); } @Override protected void setup () throws Exception { super.setup (); mfirsttestactivity = getactivity (); mfirsttesttext = (TextView) mfirsttestactivity .findviewbyid (R.id.my_first_test_text_view) ; }}
The constructor is called by the Test runner to initialize the test class, and the Setup () method is run by the Test runner before the other test methods begin.
Usually in a setUp() method, we should:
-
To setUp() call the parent class constructor, this is required by JUnit.
-
Initialize the state of the test data set, specifically:
-
Define instance variables that hold test data and state
-
Create and save a reference instance of the activity you are testing.
-
Get a reference to any UI component in the activity you want to test.
We can use the Getactivity () method to get a reference to the activity being tested.
Add a test premise
We'd better check that the test data set is set correctly before executing the test, and that the object we want to test is initialized correctly. This way, the test will not fail because of a set error in the test dataset. By convention, the method of validating a test data set is called testPreconditions() .
For example, we might want to add a method like this testPreconditons() :
public void Testpreconditions () {assertnotnull ("mfirsttestactivity is null", mfirsttestactivity); Assertnotnull ("Mfirsttesttext is null", Mfirsttesttext);}
Assertion (assertion, translator Note) method originates from the Junitassert class. In general, we can use assertions to verify that a particular condition is true.
-
If the condition is false, the assertion method throws a Assertionfailederror exception, which is usually reported by the Test Runner. We can add a string to the assertion method as the first parameter and give some contextual details when the assertion fails.
-
If the condition is true, the test passes.
In both cases, runner will continue to run test methods for other test cases.
Add a test method to verify activity
Next, add one or more test methods to verify the activity layout and functionality.
For example, if our activity contains a TextView, you can add the following method to check if it has the correct label text:
public void Testmyfirsttesttextview_labeltext () {final String expected = mfirsttestactivity.getstring (r.str Ing.my_first_test); Final String actual = Mfirsttesttext.gettext (). toString (); Assertequals (expected, actual);}
testmyfirsttesttextview_labeltext () method simply checks if the default text in Layout TextView and strings.xml the same text as defined in the resource.
Note : When naming test methods, we can use underscores to separate the tested content from the test cases. This style makes it easier to distinguish which test cases are.
When you do this type of string comparison, it is recommended to read the expected string from the resource file instead of writing the string in the code to make the comparison hard. This prevents the effect of the test when the string definition in the resource file is modified.
For comparison, both the expected and actual strings are to be used as parameters of the Assertequals () method. If the value is not the same, the assertion throws a Assertionfailederror exception.
If we add a testPreconditions() method, we can put the test method after testpreconditions.
To see a complete test case, refer to Myfirsttestactivitytest.java in the example in this section.
Build and Run Tests
We can run our tests in the package explorer in Eclipse.
Use the following steps to build and run your tests:
-
To connect an Android device, in the device or emulator, open the Settings menu, select Developer Options and make sure that USB debugging is enabled.
-
In the Package Explorer, right-click the test class and select Run as > Android Junit Test.
-
In the Android Device selection dialog box, select the device you just connected to, and then click OK.
-
In the JUnit view, verify that the test passed, with or without errors or failures.
UseGradle versionBuild Tests
-
Connect an android real computer or turn on the Android emulator.
-
Run the following command in the project directory:
./gradlew Build Connectedcheck
Build your environment and test Android apps