pytest简述--pytest.ini、allure、logging、assert的使用

1.assert

  • assert xx 判断xx为真
  • assert not xx 判断xx不为真
  • assert a in b 判断b包含a
  • assert a == b 判断a等于b
  • assert a != b 判断a不等于b

2.pytest.ini

cmd执行:pytest --help #查看pytest.ini  

pytest详细使用:https://cloud.tencent.com/developer/article/1640840

"""
pytest测试:
测试文件以test_开头(以_test结尾也可以)
测试类以Test开头,并且不能带有 init 方法
测试函数以test_开头
断言使用基本的assert即可
"""

3.allure(官网地址:https://docs.qameta.io/allure/#_installing_a_commandline

环境依赖:java8+、jdk1.8

brew install allure
pip install pytest
pip install allure-pytest 

终端中执行:

allure
Usage: allure [options] [command] [command options]
  Options:
    --help
      Print commandline help.
    -q, --quiet
      Switch on the quiet mode.
      Default: false
    -v, --verbose
      Switch on the verbose mode.
      Default: false
    --version
      Print commandline version.
      Default: false
  Commands:
    generate      Generate the report
      Usage: generate [options] The directories with allure results
        Options:
          -c, --clean
            Clean Allure report directory before generating a new one.
            Default: false
          --config
            Allure commandline config path. If specified overrides values from 
            --profile and --configDirectory.
          --configDirectory
            Allure commandline configurations directory. By default uses 
            ALLURE_HOME directory.
          --profile
            Allure commandline configuration profile.
          -o, --report-dir, --output
            The directory to generate Allure report into.
            Default: allure-report
    serve      Serve the report
      Usage: serve [options] The directories with allure results
        Options:
          --config
            Allure commandline config path. If specified overrides values from 
            --profile and --configDirectory.
          --configDirectory
            Allure commandline configurations directory. By default uses 
            ALLURE_HOME directory.
          -h, --host
            This host will be used to start web server for the report.
          -p, --port
            This port will be used to start web server for the report.
            Default: 0
          --profile
            Allure commandline configuration profile. 
    open      Open generated report
      Usage: open [options] The report directory
        Options:
          -h, --host
            This host will be used to start web server for the report.
          -p, --port
            This port will be used to start web server for the report.
            Default: 0
    plugin      Generate the report
      Usage: plugin [options]
        Options:
          --config
            Allure commandline config path. If specified overrides values from 
            --profile and --configDirectory.
          --configDirectory
            Allure commandline configurations directory. By default uses 
            ALLURE_HOME directory.
          --profile
            Allure commandline configuration profile.

4.logging

logging模块的详细使用:https://www.cnblogs.com/nancyzhu/p/8551506.html

https://blog.csdn.net/Runner1st/article/details/96481954

1)修改pytest.ini文件

pytest是从pytest.ini中读取log_cli配置的,默认是关闭的

[pytest]
log_cli = 1
log_cli_level = INFO
log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
log_cli_date_format=%Y-%m-%d %H:%M:%S

2)用pytest -o方式重写,这个功能在pytest 3.4之后才实现,如下

pytest testcases.py -o log_cli=true -o log_cli_level=INFO  

 logging模块使用:

logging.basicConfig(level=logging.INFO, filemode='a', filename='logger.log',
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
def test_a():
    log.info('info message')
    log.debug('debug message')
    log.warning('warning message')
    log.error('error message')
    log.critical('critical message')
    assert 1, "pass"
allure+logging实例:
currentPath = os.path.dirname(os.path.abspath(__file__))
date = time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime())
"""
filemode='a',##模式,有w和a,w就是写模式,每次都会重新写日志,覆盖之前的日志
#a是追加模式,默认如果不写的话,就是追加模式
"""
if __name__ == '__main__': 
# 使用pytest生成报告:pytest --html=report.html #
使用allure生成报告
report_path = os.path.join(currentPath, 'results', 'reports', str(date))
allure_report_path = os.path.join(currentPath, 'results', 'allurereport', str(date))
test_folder = os.path.join(currentPath, 'testcases')
pytest.main([test_folder, '--alluredir=%s' % (report_path), '-o log_cli=true']) #-o log_cli_level=INFO
os.system('/usr/local/Cellar/allure/2.13.5/bin/allure generate %s -o %s/html --clean' % (report_path, allure_report_path)) # 替换为本地的 allure 安装路径

posted @ 2020-08-17 14:30  拂晓lu  阅读(1279)  评论(0编辑  收藏  举报