Python+Requests+PyTest+Excel+Allure interface automated testing practice

Table of Contents

This article mainly introduces the actual practice of Python + Requess + PyTest + Excel + Allure interface automated testing. The article introduces it in great detail through sample code. It has certain reference learning value for everyone’s study or work. Friends who need it will follow the editor below. Let’s study together

Unittest is the unit testing framework that comes with the Python standard library. Unittest is sometimes also called PyUnit. Just like JUnit is the standard unit testing framework for the Java language, Unittest is the standard unit testing framework for the Python language.

Pytest is another third-party unit testing library for Python. It is designed to make unit testing easier and can also be extended to support complex functional testing at the application level.

Comparison between the two:

Pytest project actual combat:

The first step is to build the project framework (create the Gwyc_Api_Script_Pytest project directory)

Create subdirectories in sequence as follows: base: stores some of the lowest-level method encapsulation, protocols, request sending, etc. common: stores some public methods. config: stores configuration files. data: stores test data. log: store logs. report: stores reports. tests: stores test cases. utils: stores public classes. readme: used for documentation. requirements.txt: used to record all dependent packages and their version numbers to facilitate environment deployment, and can be automatically generated and installed through the pip command.

The second step is to encapsulate the request method (create method.py under the base directory)

There are two methods of encapsulation:

The first method: directly call the request method under the requests library and define all the parameters that need to be used, which are divided into actual parameters and line parameters. Parameters must be passed when calling actual parameters. Line parameters can be given default values. When calling, you can Reassignment can also use default values. This method requires less code and does not require judgment. The request will automatically send the request to the server based on the parameters passed in.

The second type: encapsulate each request method in the form of a function, and call requests respectively to send the request. Take get and post as an example: put and delete requests are encapsulated in the same way. After each request method is encapsulated, a main method is defined. Directly calling the main method will automatically judge and call each request function based on the request method. You can also specify the request method here. It is also possible to summarize and encapsulate and directly call each function to send requests. This method of encapsulation involves a lot of code and does not make good use of the requests library, so it is recommended to use the first method.

The third step is to encapsulate the method of reading files (create public.py in the common directory):

1. You need to use Python’s os library here. The os library is the Python standard library and contains hundreds of functions. Commonly used ones include path operations, process management, environment parameters, etc.

2. The acquisition directory and the files in the directory are respectively encapsulated, and can be used by directly calling and passing in the corresponding parameters. As shown below:

Step 4: Prepare data (create data.xlsx in the data directory)

Write the interface into the excel table, write all the fields required by the interface into excel, and then read them directly.

Now I have also found a lot of test friends and created a communication group to share technology, sharing a lot of technical documents and video tutorials we collected.
If you don’t want to experience the feeling of not being able to find resources when studying on your own, having no one to answer your questions, and persisting for a few days before giving up.
You can join us to communicate. And there are many technical experts who have made certain achievements in automation, performance, security, test development, etc.
Share their experience, and also share many live lectures and technical salons
You can learn for free! Focus on it! Open source! ! !
QQ group number: 110685036

Step 5: Encapsulate the file reading method (create operationExcel.py under the utils directory)

Define a file class and define a method to get the Sheet table, and define a method to get all the data and store it in the list.

Step 6: Encapsulate the login method (create login.py under the common directory)

Obtain the token and provide it to the interface. Use the @pytest.fixture decorator to execute the login method before executing all use cases to obtain the token and return it.

Step 7: Define global variables for the table header, which can be defined in the operationExcel.py file under the utils directory

Step 8, encapsulate the use case (create test_gwyc_api_all.py under the tests directory)

You need to use the @pytest.mark.parametrize() decorator to encapsulate the use cases, and call getExceldatas() to store the read use cases into the decorator. “data” is an alias. As shown in the figure below, the request headers and parameters are judged to be empty respectively, and the token is inserted into the headers, so that each interface can use the token returned by login without having to call to obtain the token every time.

Step 9: Use allure to generate a test report.

Allure is a plug-in package for Pytest. You need to download and install it, and configure the path of the bin directory in allure to the PATH environment variable to use the report directly.

Store the generated json file in the directory where the use case is located. After execution, a report directory will be generated under the tests directory, which contains the results directory and the html directory. The former stores the json file, and the latter stores the html report generated after reading.

Step 10, encapsulate log method (create log.py under the log directory)

The reference role played by logs in automated testing is relatively small. The test report shall prevail. You can simply configure it according to the corresponding rules and call it directly.

This concludes this article about the practical implementation of Python + Requests + PyTest + Excel + Allure interface automated testing. For more related Requests PyTest Excel Allure automated testing content, please search Script House’s previous articles or continue browsing the related topics below. The article hopes that everyone will support the editor in the future!