Technology Sharing

【python appium】Automated testing

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

The python+appium automated testing series is coming to an end. Let’s make a summary of this blog.

First of all, I would like to explain that many companies may not use APP automated testing, but it is also required by most recruitment information for automated testing engineers and senior testing engineers. Therefore, in order to get better treatment, we still need to spend time to master it. After all, no one can resist money.

Next, let’s summarize the ideas of APP UI automation testing.

1. Development language selection
The programming languages ​​commonly used for automated testing are: Python, Java, Javascript, Ruby, C#, PHP, etc. Generally, we will choose a programming language that we are familiar with to write automated scripts, but for those who have basically no programming foundation (or those who focus on automated testing), it is recommended to learn to use Python.

Compared with other languages, Python has the following advantages for automated testing:

For beginners, Python has concise syntax, strong readability and is easy to learn.

There are powerful third-party libraries, making script writing more efficient

The Pytest automated testing framework in Python is one of the most popular testing frameworks currently

2. UI testing framework selection
After choosing the programming language, we need to choose a UI testing framework. Currently, the more mainstream or widely used APP UI automated testing frameworks include Appium, Airtest, etc.

1,Appium
Open Source

Appium server supports running on Windows, macOS, and Linux

Supports multiple mainstream programming languages, such as Python, Java, Javascript, Ruby, etc.

Supports Android, iOS, and H5 automated testing (integrated with UiAutomator and UiAutomation frameworks)

The environment is more complicated to build

2,Airtest
Developed by NetEase (official documentation), only supports Python language

Provides a powerful IDE that integrates the two test frameworks of Airtest and Poco, which is very friendly to novices

Integrate airtest, a UI automation testing framework based on image recognition, so you can write automation scripts even if you don’t understand code

Cross-platform, capable of automating testing of Windows, Android, and iOS applications

More friendly to game testing

Airtest and Poco are essentially two third-party libraries of Python. The test environment is not as complicated as Appium.

3. Select the framework
Choose the appropriate framework according to the actual situation.

NetEase's Airtest (Airtest official website) can be preferred in the following situations:

No programming knowledge

Game Testing

The test scenario is relatively simple (Appium is relatively cumbersome)

Alipay applet automation (the blogger currently uses the Poco framework integrated with Airtest to do Alipay applet automation testing)

Appium is recommended in the following situations:

The test scenario is relatively complex (such as requiring batch testing of multiple mobile phones)

The test script language is not Python (Airtest currently only supports Python)

3. Unit testing framework selection
Different from the UI testing framework mentioned above, the unit testing framework is used to load test cases, execute test cases, assert, output test results, etc.

Different development languages ​​have their own corresponding unit testing frameworks, such as JUnit, TestNG, Robot, etc. for Java, unittest, pytest, etc. for Python. The blogger mainly uses Python for automated testing, so here I will only explain the Python unit testing framework.

Python's mainstream unit testing framework:

unittest, Python's own unit testing framework

pytest, developed based on unittest, is easy to use, has more detailed information, and has many plug-ins

Robot framework, a keyword-driven testing framework based on Python, has an interface, built-in reports and logs, complete functions, clear and beautiful

It is strongly recommended to use pytest, which is powerful and easy to use. The official documentation is in English. For Chinese, please refer to the Pytest User Manual.

4. Test environment construction
1. Test computer selection
If possible, it is recommended to use a Mac machine specifically for UI automation testing, because iOS app UI automation can only be performed on Mac machines.

2. Test phone selection
When you first start debugging the test script, you can choose an emulator. For Android emulators, you can choose Gernymotion or Noxemulator.

After the entire test project is developed, it is recommended to switch to running automated scripts on a real machine, which is closer to the actual user scenario.

3. Development language environment
It is recommended to install Python 3 or later. The official update and maintenance of Python 2 has been stopped.

4. UI automation test framework environment construction
Building Appium in Windows environment is a little more complicated than building Appium in Mac. I will share it in a blog later.

If you choose the Airtest framework, please refer to the official website.

5. Script Writing
1. Write a test demo
In order to verify whether the entire test environment is successfully built and whether general examples can be run, we usually need to write a simple test script and execute it first. If it passes, it means that the entire environment is successfully built.

2. Determine the directory structure
It is recommended to use the Page Object design pattern to develop APP UI automation testing projects.

3. Select the test report
Different development languages ​​and unit test frameworks can use different test reports. In Python, the unittest framework uses HTMLTestRunner or BSTestrunner to generate test reports. The Pytest framework uses pytest-html to generate reports or customize test reports through allure.

After completing the above steps, continue to add test cases to the test project.

6. Jenkins Integration
After completing the entire app UI automated testing project according to the above steps and running it stably for a period of time, you can consider integrating Jenkins

VII. Conclusion
In summary, APP UI automation can be implemented in the following ways:

1,python + appium + unittest + HTMLTestRunner

2,python + appium + pytest + pytest-html / allure

3,python + Airtest(AirtestIDE)

4,python + Poco + pytest + pytest-html / allure

You can choose according to the actual situation.

Finally, I would like to thank everyone who read my article carefully. It is always necessary to exchange gifts. Although it is not something very valuable, you can take it directly if you need it:

These materials should be the most comprehensive and complete preparation warehouse for friends who are engaged in [software testing]. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can help you too!