【Understand the basics of Android testing】

Previous: 【Create and run your first Android application】

1. Preparation

prerequisite

  • Learn the basics of how to navigate to project directories in Android Studio.

Learning Content

  • What is a test?
  • What is automated testing?
  • What are unit tests and instrumentation tests?
  • Where can I find unit test and instrumentation test files in an Android project?

required conditions

  • A computer with Android Studio installed.
  • The project you created in the previous article in this online course .

2. What is a test?

As far as software is concerned, testing is a structured method of checking software to make sure it is functioning properly. An automated test is actually a piece of code that checks another piece of code you've written to make sure it works correctly.

Software testing is very beneficial because it allows you to iron out bugs in your code before releasing it publicly, which is key to ensuring a good user experience.

While manual testing almost always has its place, testing in Android can often be automated. Throughout the Fundamentals of Android Development with Kotlin course, you'll focus on using automated tests to test the functional requirements of your application code and the application itself. In this article, you'll learn the basics about Android testing. In subsequent articles, you'll learn more advanced practices for testing Android apps.

As you become more familiar with Android development and Android application testing, you should make it a regular practice to write tests alongside your application code. If you create a test every time you create a new feature in your app, you can save yourself a lot of work later when your app grows. Doing so also saves you from spending a lot of time manually testing your app, making it a quick and easy way to make sure it's working properly.

3. Introduction to automated testing

An automated test is a piece of code that checks another piece of code you wrote to make sure it works and continues to work as the project grows and changes. Automated testing is an integral part of all software development, and Android development is no exception. So now is the perfect time to introduce it! When you create your first Android application, you may see the main activity in a subfolder of the maindirectory . In srcthe directory , you may have noticed testthe and androidTestdirectories . These two directories are where the test code is written. There are two types of automated testing in Android development: unit testing and instrumentation testing. These two directories represent the two types of tests, respectively.

insert image description here

Find unit test code

Native tests in Android are located in the test directory, and they are usually unit tests. A unit test directly tests a small piece of code to make sure it works correctly. With unit testing, you can test functions, classes, and properties. Local tests are executed on the Java Virtual Machine, which means they can be run in the development environment without the use of a device or emulator. This is a neat way of saying that unit tests run on a computer. Android Studio is ready to automatically run local tests.

Android Studio automatically generates a simple unit test every time you create a new project. The same goes for instrumentation tests. One thing to note is that these tests don't actually perform any related actions. They are used only as placeholders. For now, you just need to know where to find the test files. We will explore the content of these generated tests in depth in subsequent online courses.

To find unit test code:

  1. Open the Birthday Card app from the previous project.
  2. If necessary, select Android from the navigation menu.
  3. Click app > java > com.example.happybirthday (test) > ExampleUnitTest.

insert image description here

Find instrumentation test code

In the Android development environment, instrumentation testing usually refers to interface testing. With instrumentation, you can test the parts of your application that depend on activity and fragment lifecycles, platform APIs, and services.

Unlike unit testing, UI testing doesn't test the code directly, but tests the UI to ensure that the correct UI components are displayed and that when actions are performed in the UI, the UI behaves as expected. Another difference is that all instrumentation tests must be run on a physical device or emulator. In previous online courses, you have already set up the simulator, so this step is already done.

When running instrumented tests on Android, the test code is actually built into its own APK, just like a regular Android app. An APK is a compressed file that contains all the code and files needed to run an app on a device or emulator. The test APK will be installed on the device or emulator along with the regular app APK. Test APK then runs the tests against the app APK.

Before running the test, let's take a look at what the code will do.

To find instrumentation code:

  1. If you are currently in the Android project view, click app > java > com.example.happybirthday (androidTest) > ExampleInstrumentedTest .

insert image description here

  1. If you are currently in the Project view, click HappyBirthday > app > src > androidTest > java > com.example.happybirthday > ExampleInstrumentedTest .

4. Congratulations

You've learned what testing is in Android, and how to find unit and instrumentation tests in Android.

Next: 【Create Birthday Card Application】

Guess you like

Origin blog.csdn.net/Jasonhso/article/details/125919697