Python+requests Interface Automation complete project design Source

Source: Internet
Author: User

Objective

There are a lot of small partners arguing to complete the project source code, the complete project belongs to the company's internal codes, this is not shared, illegal work ethic, even if others share, but also only applicable to the company's internal business.

So the code of use case still have to write to each one, I can only share the project framework, can only help you to this.

I. Structure of the project

1. Create a new project (make sure the project is created), the project name is defined by itself, such as: Yoyo_jiekou

2. Create a new script in the project with the directory: run_main.py, used to execute all use cases

3. Create the following pakage packages under the project:

--case: This package puts test cases at the beginning of test, and can also put some methods of encapsulating interfaces, such as: Loginblog (if the interface is more encapsulated, you can put a package separately)

--common: This package put some common methods, such as: Read the Excel file method, read MySQL, oracle,logger.py This is the input of the package log

--config:cfg.ini Here are the configuration files, such as some parameters of the mailbox: recipient, sender, password, etc., readconfig.py used to read the configuration file

--logs: Log information is stored here

--report: Store test report here

Second, Run_main

First step: Load all test cases with the Discover method

1.cur_path This parameter reads the actual path of the current script, which is the true path of the run_main.py

2.casename= The case is the folder where the test cases are stored and, if not, created automatically. If you want to run a use case for another folder, change the Casename parameter value

3.rule= "test*.py" This is the rule that matches the name of the use case script, which by default matches all use cases beginning with test

Step two: Generate an HTML report

1. Pass the parameters of the previous step to the use case into this function, the file name of the test Report folder: Reportname= "reports

2. It doesn't matter if you don't have this report folder, you can create it automatically.

Step three: Get the latest test report

1. If the test report generated in the second step has a timestamp, the third step is to find the latest file

2. If the second step does not timestamp, just generate result.html, then this step is actually no eggs, you can ignore

(personally think the report with a name result.html on the line, the new auto-overwrite the old)

Fourth step: Send test report to mailbox

1. Like QQ mailbox This SSL encryption go smtp_ssl, login with authorization code

2. Other mailboxes on the normal account password login, go to SMTP

Final code Execution

1. The contents of the mailbox here read the configuration file

Third, config configuration

1.cfg.ini Open, here write the configuration file contents

2. Read the configuration file with readconfig.py

3. Read the contents of the second step in the incoming call to the mailbox configuration information

Third, Logger

1.logger.py this file into the common directory, the encapsulated log file reads

2. Save the log to the logs folder

3. Encapsulating code

# Coding:utf-8
Import logging, time
Import OS
# Log_path is the path where the log is stored
Cur_path = Os.path.dirname (Os.path.realpath (__file__))
Log_path = Os.path.join (Os.path.dirname (Cur_path), ' logs ')
# If the logs folder does not exist, it will automatically create a
If not os.path.exists (Log_path): Os.mkdir (Log_path)

Class Log ():
def __init__ (self):
# Name of the file
Self.logname = Os.path.join (Log_path, '%s.log '%time.strftime ('%y_%m_%d '))
Self.logger = Logging.getlogger ()
Self.logger.setLevel (logging. DEBUG)
# Log Output format
Self.formatter = logging. Formatter (' [% (asctime) s]-% (filename) s]-% (levelname) s:% (message) s ')

def __console (self, level, message):
# Create a Filehandler for writing to local
FH = logging. Filehandler (Self.logname, ' a ') # Append mode this is Python2.
# fh = logging. Filehandler (Self.logname, ' a ', encoding= ' Utf-8 ') # this is Python3.
Fh.setlevel (logging. DEBUG)
Fh.setformatter (Self.formatter)
Self.logger.addHandler (FH)

# Create a Streamhandler for output to the console
ch = logging. Streamhandler ()
Ch.setlevel (logging. DEBUG)
Ch.setformatter (Self.formatter)
Self.logger.addHandler (CH)

if level = = ' Info ':
Self.logger.info (Message)
elif level = = ' Debug ':
Self.logger.debug (Message)
elif level = = ' Warning ':
Self.logger.warning (Message)
elif level = = ' ERROR ':
Self.logger.error (Message)
# These two lines of code are designed to avoid duplicate log output problems
Self.logger.removeHandler (CH)
Self.logger.removeHandler (FH)
# Close Open files
Fh.close ()

def debug (self, message):
Self.__console (' Debug ', message)

def info (self, message):
Self.__console (' info ', message)

Def warning (self, message):
Self.__console (' warning ', message)

def error (self, message):
Self.__console (' ERROR ', message)

if __name__ = = "__main__":
Log = log ()
Log.info ("---test start----")
Log.info ("Operation Steps")
Log.warning ("----test end----")

Iv. Case-Use cases

1. Common interface, need to check the call of the separate package processing, such as login, etc.

Use cases beginning with 2.test

Five, logs log view

1. After running the log will be collected to the logs folder named after the date

VI. Generate Test Report

1. Here is a test report that Htmltestrunner generates HTML

Vii. Send report to Mailbox

1. Run run_main.py will automatically generate the report and then send it to the mailbox.

(Here QQ mailbox display is a problem, the general use of enterprise mailbox, it can be displayed normally)

(source code can be purchased through the following links, 7.2 chapters can be obtained)

---------------------------------Python interface automation has been written-------------------------

Book Purchase address https://yuedu.baidu.com/ebook/585ab168302b3169a45177232f60ddccda38e695

Python+requests Interface Automation complete project design Source

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.