Which is more suitable for automated testing, Python or Java?

Hello everyone. I have been engaged in software testing for many years, and I have worked hard all the way from various major companies. Now I am fortunate to work for ByteDance. I also slowly climbed from functional testing to automated testing. So on this issue, I still have a little right to speak.

I remember that when I transitioned from functional preparation to automated testing at the beginning, like many “not deeply involved” classmates, I also faced the selective barrier of the preferred language. When asking industry leaders whether to choose Python or Java, the answers you can basically get are nothing more than: “What do you want to do”, “What do you know”, “How do you want to do it”.

Later, I thought I had figured out these three questions, but the bosses gave a lecture like General Fan: “Hey, thank God, be pragmatic, I advise you, first figure out your own job, and automate this Understand the concept first. Your testing foundation is not solid, and you are not familiar with the technology stack. If you talk about the plenary meeting, you will lose it when you do it. You can’t even evaluate the common function points. When the time comes, you ask me how to write it?”

With these questions in mind, I figured out this “preferred language” from deep to shallow with an ignorant heart.

Software product automation testing flow chart

If we want to understand the superficial problem of “automation preferred language” on the iceberg, we should actually drill under the iceberg to understand two other questions: 1. Why do we use automation in testing? 2. What is the current mainstream enterprise use? method to automate. After understanding the two questions under the iceberg, and then talking about the questions on the iceberg, you can know the answer from the deep to the shallow..

Therefore, the sharing content will be a little longer. The theme structure of my next article will revolve around these three questions:

1) Why use automated testing?

2) What methods are currently used by mainstream companies to implement automated testing

3) Which one is more suitable for automated testing, Python or Java?

Well, the article officially begins….

1. Why use automation for software testing?

Before answering this question, we must first understand the concept of automation!

1. What is automated testing?

The so-called automated testing refers to the process of using other software independent of the software under test to automatically execute tests, compare actual results with expectations, and generate test reports.

After the testing process has been determined,
Test automation can automate some repetitive but necessary testing tasks. It is also possible to accomplish tests that would be nearly impossible to do manually. For continuous delivery and continuous integration development methods,
Test automation is critical.

2. Why use automation for testing work?

The working principle of automated testing is to roughly replace manual testing, but what exactly is it replacing?

Let’s take traditional manual testing as an example. We write a case with preconditions, steps, and expected results, and then perform manual operations to compare the actual results with the expected results.

Automation is when computers replace our process of automatically comparing expected results with actual results.

Let’s use a calculator as an example:

[Downloading images in this format is not currently supported]

Let’s calculate the addition, 1 + 1 = 2

Manually: a input 1 b input 1, the expected result is 2, after execution 2=2 test passed

Program: a=1 b=1 Judgment: 2==a + b After execution, the 2=2 test passes

congratulations! Now you have a calculator that only calculates 1 + 1 = 2 correctly! But you can’t guarantee that it will be right if you change it!

So one day, by chance, a=1 was changed to a=2. At this time, the execution result judgment: 2==a + b after execution, 2==3 test failed

So we introduced the concept of parameterization.

When a, b, c are added, they correspond to the first group: 1, 1, 2 Now a=1 b=1 c=2 Judgment: c==a + b After execution, the 2=2 test passes

At this time, we have passed the basis of manual testing. We consider our test points. From the perspective of traditional manual testing, we divide numbers into integers, floating-point types, and 0 through equivalence class division. According to the boundary value, it is divided into, for example, the longest is 3 digits, which is the result = 999

This way we will change our test parameters

Group 2 1.1 , 1.2 , 2.3 Bring parameters into a + b=c Execute c==a + b c==2.3 After execution 2.3==2.3 Test passed

The third group 1, 0, 1 brings the parameters into a + b=c, execute c==a + b c==1, after execution, 1==1, the test passes

Group 4 100, 200, 300 Bring parameters into a + b=c Execute c==a + b c==300 After execution 300==300 Test passed

The above are all normal scenes, now some abnormal scenes are divided on the basis of the original

Subdivide the second group of data and add two floating-point numbers to advance one bit 1.89, 1.33, 3.22 Bring the parameters into a + b=c and execute c==a + b c==3.22 After execution 3.3.2199999999999998== 3.22 If the test fails, it will cause precision problems, which can be repaired by R&D, such as retaining 2 decimal places.

Of course, we can also subdivide the fourth group of data to make it exceed the boundary value of 999, 1, and exceed the boundary. Combining the second group of subdivisions and the fourth group of subdivisions are floating-point, and let the result exceed the boundary of 555.55, 444.55 , the operations of addition, subtraction, multiplication, and division beyond the boundary are verified again, through parameter input, computer output, and result comparison. Thus, an additive automation use case set is obtained, and on this basis, we can also have other operation use case sets.

This is the most primitive way of testing. Whether you are testing the interface or the web, mobile, or UI, in the final analysis, after the requester, the return value is compared with the expected value. So almost every language can do automated testing.

Since almost every language can be used for automated testing, which one is more suitable for automated testing, Python or Java?

We use the programmer’s first sentence Hello World! Example:

1. This is java‘s HelloWorld:

public class HelloWorld {
 public static void main(String args[]) {
 System.out.println("Hello World!");
 }
}

2. This is python HelloWorld

print("HelloWorld!")

The python language is relatively concise, and it is faster to develop. The java language is also very concise, but it is more complicated than python. Obviously python’s simplicity is not for nothing.

2. What methods are currently used by mainstream companies to implement automated testing

With the continuous accumulation of business, cases gradually increase, and personnel are constantly adjusted, we cannot delete past cases hastily.

However, it is impossible to launch a function that is simple enough and independent enough, but every time the automation is executed, it will return to the pain points of all cases.

So the long-awaited test framework came out.

The test framework makes automated testing easier to implement and deal with the existing problems of automated test scripts: such as exception handling and scene recovery, to make up for the shortcomings of the test script itself or special test requirements, and the test is easy to maintain.

Interface model diagram of automated testing framework

1. Commonly heard test framework in Java

1) JUnit

Junit allows you to write corresponding unit test programs for Java code. You can use JUnit for unit and integration testing, and it supports various features of Java 8.

2 ) REST Assured

Test and validate various REST services in Java, Java Realm brings linguistic simplicity. It is a very good REST API integration testing tool.

3 ) Selenium

Selenium should be the most common tool in Java UI testing. It allows you to test JSP pages, and even allows you to write web applications to accept various tests.

4 ) TestNG

TestNG is a test framework developed from JUnit and NUnit, but it introduces many new functions, such as: annotations can run tests of various available strategies in an arbitrarily large thread pool, which can make up for the gap between JUnit and TestNG gap between.

2. Python common testing framework:

1) Unittest

A useful feature of unittest is the setUp() and tearDown() methods, which provide functions for preparing and finishing the test, which are very suitable for use when the test object requires a complex execution environment. When the setUp() method is defined in the class, the test program will call this method before executing each test item; similarly, the tearDown() method will also be called after all test items are executed.

2) Pytest

At present, Pytest is commonly used in the industry. Pytest is simple and flexible, easy to use, and supports parameterization.
It can support simple unit tests and complex functional tests, and can also be used for automated testing such as Selenium/Appnium and interface automated testing (Pytest + Requests). Pytest has many third-party plug-ins and can be customized for extensions.

3. How do companies choose?

I don’t say maybe you don’t know, actually. . . . You can also write some simple automation scripts using JavaScript or jQuery~

However, many large companies are developed in Java, and most of the automated test development tools also use Java language, which makes communication easier and compatible with ideas. At the same time, for mobile terminal automation, Java is also very suitable for test engineers who master Java language tools!

Of course, many large factories, including some small and medium-sized enterprises, and entrepreneurial teams also use Python for testing.
Automate testing with Python + Pytest;
Use Python + Selenium: get UI automation testing, compatibility testing;
Use Python Request: get the interface test done;
Use Python Locust to get performance testing done;
Use Python Scapy: get security performance testing;
Build the company’s own automated testing platform through Python + mysql + Django/Flask.

For ByteDance, my company on the mobile side, as an example, We will use the self-developed Shoots solution to do full-platform automated testing, and we will also use Airtest to do a mobile-side cross-platform UI automation testing framework (applicable for games and apps) and these are all developed based on the Python language.
In the case of data-driven testing, in some temporary projects or short-term tasks, we also often use the glue language of Python to write some scripts, generate test data, assist test tasks, and reduce labor costs.

3. Which one is more suitable for automated testing, Python or Java?

1. Let’s talk about Python first

Python does not have a very complicated structure, and there is little preparation in the early stage. It does not have high requirements for the special quality of personnel, nor does it have high requirements for computer configuration. For students who have not received professional computer training to get started Within your fingertips.

Especially the Pytest framework, its extension is also better:
Such as Pytest-selenium (integrated selenium),
Pytest-html (perfect html test report generation),
Pytest-rerunfailures (failure case repeated execution),
The skip and xfail processing of test cases such as pytest-xdist (multi-CPU distribution) can be well integrated with jenkins,
Of course, the report framework – allure also supports Pytest.
In the end you will get a test report as shown in Jenkins:

Summary: For test engineers, Python is easy to learn, has a huge and abundant ecology, and is relatively perfect and easy to use for existing automated testing frameworks (such as selenium, appium, etc.). Tests can be easily developed on the framework and can be customized Optimized test environment.

2. Let’s talk about JAVA

Although Java can also do automated testing, it is not so easy to learn compared to Python. But if a test engineer wants to do simple development on the framework and do test development work, he must master the Java language and learn more things.

3. Share my sincere suggestion

In the final analysis, Python and Java are a development tool and a language, and their ideas are interoperable.

The essence of our pursuit is to improve test efficiency, improve test quality, reduce test cost, and reduce maintenance cost.

Language itself is neither good nor bad. How to choose?

Choose what you are familiar with or what everyone around you is using, so that resources are more advantageous. Because the richer the resources, the lower the cost of learning. After all, we are not in a closed environment with stuffy acoustic technology.

You can start with Python, and after you have some basics, you can also choose, and then rely on Java to learn more computer principles.

It is easy to get started with the basics, and easy to get started without the basics. At present, Python is enough for testing.

Fourth, write at the end

Focusing on learning ideas, the precipitation of test foundations, the cultivation of test strategies and test ideas is more effective for automated testing. When I was working, I basically encapsulated a set of my own test framework. What everyone cares more about is the discussion and practice of using xxxx’s technical solutions~~

The following is the supporting information. For friends who do [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse also accompanied me through the most difficult journey. I hope it can help you too!

How to get it for free:

Join my software testing exchange group: 110685036 to get it for free~ (Academic exchanges with fellow leaders, and there will be live broadcasts to share technical knowledge points every night)

Software testing interview applet

The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!

The following interview question sections are covered:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics

How to get it:

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge Java skill treeHomepageOverview 118777 people are studying systematically