Python automated testing — don’t rush to type code

Time:2022-5-2

When learning Python automation, do you install the environment and start typing the code directly to try to run the program?
The first thing to be clear is the test process! Automated testing is to write code and script to make the software run automatically, find defects and replace some manual testing. The essence of automated testing is also testing.
The test process is generally as follows:
Familiar with requirements — analyze requirements and design test cases — execute tests — output test results
Then automated testing must also follow the testing process, so let’s put aside the problem of how to write code and learn the framework and ideas of automated testing first~~

Automation framework generally includes use case management module, automatic execution controller, report generation module and log module, which complement each other

Step 1: Environment
win10+python3+pycharm
HTMLTestRunner. py ; Installation of selenium and other modules

I won’t elaborate on that~~
In short, ensure that the commissioning environment is OK before continuing the actual part, otherwise the foundation will be unstable and face collapse at any time.

Step 2: frame construction

Build an automation framework by yourself and plan the directory of the project
Before writing, we need to figure out what functions the script needs to achieve, especially when some test data needs to be modified, so as to avoid a large number of correcting the script and improve the reuse rate of the script.

Ultimate purpose:
Preparation of test cases, test execution, generation of test results and reports, test execution failure, log and screenshot records;

To sum up, the automation framework needs to include use case management module, automatic execution controller, report generation module and log module, which complement each other.

The following figure shows the test framework I built according to this idea:

Python automated testing -- don't rush to type code

Automation framework diagram

Briefly describe the function of each module:

Common: store some common methods
Data: store various test data required in the test process
Logs: save the logs required for execution
Picture: save screenshot file information
Report: store test results and test reports
test_ Case: store and write specific test cases
Config: configuration information operation
–Conf.ini configuration file
–readconf. Py reads the contents of the configuration file
run_ main. Py: main program

Next, I’ll post the code screenshot of my main program (comments are added at each step), and other contents need to be added according to the actual project.

Python automated testing -- don't rush to type code

run_main.py

–The above figure mainly defines the storage path of test reports, test results and test logs

Python automated testing -- don't rush to type code

run_main.py

–The above figure mainly shows the process of using unittest automation framework to execute use cases
(define test cases with “test_” (method of naming at the beginning)

Today, let’s have a general understanding of the construction of the framework. Later, we will find a project for actual combat