First, the project structure
1. Create a new project (be sure to create a project), project name their own definition, such as: yoyo_jiekou
2. Create a new script in the project root directory: run_main.py, to perform all use cases
3. Create the following pakage package in the project:
--case: The test pack placed at the beginning of test, the package interface may put some methods, such as: loginblog (if more interface package, a package may be placed separately)
--common: This package put some common methods, such as: reading methods excel file, reading mysql, oracle, logger.py this package is the input log
--config: cfg.ini here is the configuration file, some parameters such as mailbox: To, From, passwords, etc., readConfig.py to read the configuration file
--logs: where to store log information
--report: where to store the test report
二、run_main
The first step: with load all the test cases discover methods
1.cur_path This parameter is the real path to read the current script, which is the true path of run_main.py
2.caseName = "case" in this case is a test case file storage folder, if not, automatically created. To run the embodiment with other folders, this would change the parameter value caseName
3.rule = "test * .py" This is the matching rule use case script name, the default test match at the beginning of all use cases
Step Two: Generate HTML report
1. Load the step to pass this function, the test report with the parameters of the file name of the default report folder: reportName = "report
2. If there is no report folders does not matter, it can automatically create
The third step: Get the latest test report
Step 1. If the generated test report time-stamped, trying to find the latest on file with the third step
2. If the second step is not stamped, just generate result.html, that this step did not actually use the eggs can be ignored
(Personally I feel that report with a name result.html on the line, a new automatically overwrite old)
Step Four: Send a test report to the mailbox
1. QQ mailbox like this ssl encryption away SMTP_SSL, log in with an authorization code
2. Other mailbox on normal account password, go SMTP
Finally, execute the code
1. read the contents of a mailbox profile here
Three, config configuration
1.cfg.ini open, here to write the contents of the configuration file
2. Read the configuration file readConfig.py
3. Read the contents of the configuration information is passed in the second operation which need to call the mailbox
Three, Logger
1.logger.py this file into the common directory, reads the log file package
2. The log is saved to the logs folder
3. Package Code
# coding:utf-8
import logging, time
import os
# Log_path is the path to store the log
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 automatically creates a
if not os.path.exists(log_path):os.mkdir(log_path)
class Log():
def __init__(self):
# File named
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, wrote for local
fh = logging.FileHandler (self.logname, 'a') # This is the append mode of python2
# Fh = logging.FileHandler (self.logname, 'a', encoding = 'utf-8') # This is the 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 in order to avoid duplication log output
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 ( "--- ---- start of the test.")
log.info ( "Procedure 1,2,3")
log.warning ( "---- ---- end of the test.")
Four, Case use cases put
1. The common interface, the package needs to check a separate process is invoked, such as logging, etc.
Beginning with the embodiment 2.test
Five, logs Log Viewer
1. After the operation logs are collected logs folder named by date
Sixth, generate test reports
1. Here the call is HTMLTestRunner generate html test report
Seven, send a report to the mailbox
1. Run run_main.py will automatically generate a report, and then sent to the mailbox
(Here QQ mailbox to show there is a problem, usually with E-mail, it is normal show)