Android unit test case

Android is a Linux-based free and open source operating system for mobile devices such as smartphones and tablets, led and developed by Google and the Open Handset Alliance. There is no unified Chinese name, and more people in mainland China use “Android” or “Anzhi”. The Android operating system was originally developed by Andy Rubin and mainly supports mobile phones.

The original meaning of the word Android refers to "robot" and also Google on November 5, 2007.

Android unit test case

Android logo related pictures

The name of the announced open source mobile operating system based on the Linux platform consisting of the operating system, middleware, user interface and application software.

The word Android first appeared in the science fiction "L'ève future" published by the French writer Auguste Villiers de l'Isle-Adam in 1886. He named the machine like a human being as Android.

The Android logo was designed by Ascender and was born in 2010. The design was inspired by the graphical symbols on the door of the men's and women's toilets. [1] So Brock drew a simple robot whose torso resembled the shape of a tin can. There are two antennas on the head, and the Android robot is born. The text uses a font called "Droid" made by Ascender. Android is a full-body green robot, and green is also a symbol of Android. The color is drawn using PMS 376C and RGB hex #A4C639, which is the brand symbol of the Android operating system. Sometimes they also use a plain text logo.

How to do unit testing in Android studio. In the Android development project, the test operation is often performed, and the simulator is run again and again, which wastes a lot of time and reduces the work efficiency. Although the latest Android studio provides the instance run function to improve the compilation of Android studio. Speed, but we still need to understand the unit testing function of Android studio, which can provide us with functional testing, so if the test data is useful in the project, you can first test the unit, if you can output the data normally, Then go to the UI to execute, which will improve some work efficiency.

What is unit testing:

Before explaining how to do unit testing in Android studio, let's first spread the basics, that is, what is unit testing. Here I will quote the description of unit testing in Encyclopedia:

Refers to the inspection and verification of the smallest testable unit in the software. For the meaning of the unit in the unit test, in general, it is necessary to determine the specific meaning according to the actual situation, such as the unit in C language refers to a function, the unit in Java refers to a class, and the graphical software can refer to a window or a menu. Wait. In general, a unit is the smallest measured functional module that is artificially specified. Unit testing is the lowest level of testing activity to be performed during the software development process, and the individual units of the software are tested in isolation from the rest of the program.

Simply speaking, unit testing is the splitting of a software function into N smallest non-detachable unit function points. The test against the unit function points is unit testing.

What does unit testing do:

Tests in Android are generally divided into: functional tests, ui tests, unit tests, etc.;

Since the app needs to run the Android runtime environment, and our Android unit test generally can not provide the running environment, so generally like functional testing, UI testing, etc. need to be carried out in the simulator or on the real machine, but some functional requirements do not require Android. The function of the environment, if you also use Android studio to recompile and run, then the time is too long. Generally speaking, an apk file is compiled, installed, and running for one or two minutes is common. Three or four minutes is also possible. In order to test a simple function, it takes a long time to recompile and run, and the price is too low.

Therefore, unit testing is mainly functional testing, mainly used to test some functional requirements; such as network requests, such as data storage.

Android studio support for unit testing:

Support for unit testing has been added to the new Android studio; as shown:

Android unit test case

Write test cases in this directory.

Can unit tests test those contents?

What needs to be explained here is that the unit test of Android studio is just to simulate the Android development environment, but it is not a real Android development environment, so you can't test the UI function, you can't test the functions that need hardware support (such as Bluetooth, wifi, etc.), you can't test the app. Jump and so on, so what can it test?

Test some data features, such as loading network data

Test SharedPerferences, test databases, test functions, etc.

Tool type testing, such as verification time, conversion format, regular verification, etc.

Simple unit test use case:

Let's take a look at the way test cases are written:

/**

* InstrumentaTIon test, which will execute on an Android device.

*

* @see "a href="http://d.Android.com/tools/tesTIng""TesTIng documentaTIon"/a

*/

@MediumTest

@RunWith(AndroidJUnit4.class)

Public class ExampleInstrumentationTest {

@Test

Public void useAppContext() throws Exception {

// Context of the app under test.

Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("uuch.com.Android_activityanim", appContext.getPackageName());

}

}12345678910111213141516

This is the default unit test class created by the project. It can be seen that it is not much different from the normal Class class. It just calls the corresponding test API. Now we will customize our own unit test class.

Write a custom test case class:

Implement test case method

/**

* Created by aaron on 16/7/11.

* Custom unit test class

*/

@MediumTest

@RunWith(AndroidJUnit4.class)

Public class MTest {

@Test

Public void test1() {

// Context of the app under test.

Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("uuch.com.Android_activityanim", appContext.getPackageName());

Log.i ("tag", "$$$$$$$$$$$$");

assertEquals("result:", 123, 100 + 33);

}

}12345678910111213141516171819

have to be aware of is

Test case classes require annotations: @MediumTest and @RunWith(AndroidJUnit4.class)

The test case method we wrote needs to add an annotation named Test, otherwise the test method will not be found.

For example, if we remove the annotation test:

Android unit test case

If you execute it again, you will not find an executable test function.

There is still a problem, we can find that our function is public, if we set our test function to private goods? Modify test function

/**

* Unit test, test function

*/

@Test

Private void test2() {

Log.i ("tag", "$$$$$$$$$$$$");

assertEquals("result:", 123, 100 + 33);

}12345678

After execution, you can find:

Android unit test case

The error is reported, the error description is also very detailed, saying that our test function needs to be set to Public, so we need to pay attention to two points when writing the test function:

Test function needs to be public

Test function needs to add @Test annotation

How to execute test cases

Right click directly in the source code

After writing, how to run it?

Android unit test case

You can select the name of the method you want to test, and then right-click to bring up the operation prompt box. This is to select the run method name. You can execute the test method at this time.

The test case provides us with the system environment objects that may be needed during the test.

Android unit test case

For example: application, context, etc.; is it convenient to write unit tests again in the future?

Execute test cases in the Android studio menu

- Select run-edit configuration

Android unit test case

- Add Android tests use case

Android unit test case

- Configure the tests method

Android unit test case

Click ok, this time the test area has just appeared in the run area.

Android unit test case

A simple example of a simple unit test:

Having said that, let's take a development example of strength.

scene

There is such a situation, we need to use a regular expression to verify a string during the development process, but we want to verify this regular expression before recompiling Apk, you can also run the project directly, but it is too slow, there are What simple way to verify? At this time we can use our unit test.

coding

/**

* Unit test, test function

*/

@Test

Public void test2() {

Boolean result = "18210741899".matches("\\d{11}");

Log.i("tag", "#####:" + result);

/**

* Verify email

*/

assertEquals("result:", result, true);

}123456789101112

carried out

Android unit test case

This way we can verify the correctness of the regular expression without launching our app. The function assertEquals is an assertion function, used to judge whether the result is correct, you can see the running result: 1 test passed, indicating that our function is executed correctly, that is, result is true, so our regular expression is correct.

to sum up:

So after a series of operations, we introduced the steps of unit testing in Android studio, how? Very simple, O(∩_∩)O haha~

Android studio supports unit testing by default, you can write test cases under AndroidTest under module

The test case provides an API for getting the Context, which can be used to get the Context object.

The test case method needs to be indicated by the annotation @Test, otherwise it will report an error and the test method cannot be found.

The test method needs to be defined as public, otherwise an error is reported.

There are two ways to execute the test method, you can right-click directly in the source code, you can also configure the test method in Android studio

Execution unit testing will re-execute the compilation, packaging, and installation of apk. The advantage is that it eliminates the need to manually open a page to perform an operation.

Posh Plus

Posh Plus,Posh Plus Xl Pod Vape,Posh Plus Disposable Vape,Posh Plus Disposable Pod Device

Shenzhen Zpal Technology Co.,Ltd , https://www.zpal-vape.com

This entry was posted in on