程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

pytest教程之代码运行方式

发布于2019-08-20 17:35     阅读(1671)     评论(0)     点赞(1)     收藏(2)


前言

pytest用例除了常用的命令行运行方式外,还有另外一种运行方式,即为代码方式,通过pytest.main()来执行,即可抓取此main文件所在的目录和同级下所有目录里的用例;

使用说明

如果要使用命令行方式运行某些用例,则可以通过添加参数的方式来进行

def main(args=None, plugins=None):
    """ return exit code, after performing an in-process test run.

    :arg args: list of command line arguments.

    :arg plugins: list of plugin objects to be auto-registered during
                  initialization.
    """
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

如上为main函数的说明,其有两个参数,第一个参数args为pytest自带的各种参数,可以添加多个,为数组,plugins参数亦为一个数组,可以添加与pytest匹配的各种插件参数;

附加args参数

此处可以添加的参数可以在命令行中用pytest --help来看,每组参数为一个字符串,多个参数组合为一个数组即可,如下用几个基础使用方式来进行说明
譬如添加一个-v参数,pytest.main(["-v"])

E:\software\Python\Python3.7\python.exe E:/pyspace/testSimple/testcase/main.py
============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- E:\software\Python\Python3.7\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.1', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '4.4.1', 'py': '1.8.0', 'pluggy': '0.11.0'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'cov': '2.7.1', 'allure-pytest': '2.6.3'}, 'JAVA_HOME': 'D:\\project\\jdk1.8'}
rootdir: E:\pyspace\testSimple\testcase
plugins: metadata-1.8.0, html-1.20.0, cov-2.7.1, allure-pytest-2.6.3
collecting ... collected 7 items

Test_example.py::Test_example::test_aaa PASSED                           [ 14%]
Test_example.py::Test_example2::test_bbb PASSED                          [ 28%]
Test_simple.py::Test_simple::test_case1 PASSED                           [ 42%]
Test_simple.py::Test_simple::test_case2 FAILED                           [ 57%]
Test_simple.py::Test_simple::test_case3 PASSED                           [ 71%]
cases_test.py::Test_case::test_fff PASSED                                [ 85%]
test_case.py::Test_case::test_ddd PASSED                                 [100%]


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

指定测试方法,main函数就应当这样写:pytest.main(["-v","test_case.py::Test_case::test_ddd"]):

E:\software\Python\Python3.7\python.exe E:/pyspace/testSimple/testcase/main.py
============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- E:\software\Python\Python3.7\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.1', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '4.4.1', 'py': '1.8.0', 'pluggy': '0.11.0'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'cov': '2.7.1', 'allure-pytest': '2.6.3'}, 'JAVA_HOME': 'D:\\project\\jdk1.8'}
rootdir: E:\pyspace\testSimple\testcase
plugins: metadata-1.8.0, html-1.20.0, cov-2.7.1, allure-pytest-2.6.3
collecting ... collected 1 item

test_case.py::Test_case::test_ddd PASSED                                 [100%]

========================== 1 passed in 0.02 seconds ===========================

Process finished with exit code 0

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

使用pytest-html插件:

#-*- coding: utf-8 -*-
import pytest

if __name__ == '__main__':
    pytest.main(args=["-v","test_case.py::Test_case::test_ddd","--html=report/report.html","--self-contained-html"])
  • 1
  • 2
  • 3
  • 4
  • 5

运行结果:

E:\software\Python\Python3.7\python.exe E:/pyspace/testSimple/testcase/main.py
============================= test session starts =============================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- E:\software\Python\Python3.7\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.1', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '4.4.1', 'py': '1.8.0', 'pluggy': '0.11.0'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'cov': '2.7.1', 'allure-pytest': '2.6.3'}, 'JAVA_HOME': 'D:\\project\\jdk1.8'}
rootdir: E:\pyspace\testSimple\testcase
plugins: metadata-1.8.0, html-1.20.0, cov-2.7.1, allure-pytest-2.6.3
collecting ... collected 1 item

test_case.py::Test_case::test_ddd PASSED                                 [100%]

--- generated html file: E:\pyspace\testSimple\testcase\report\report.html ----
========================== 1 passed in 0.04 seconds ===========================

Process finished with exit code 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在这里插入图片描述

附加plugins参数

目前还不清楚用法,待后续研究



所属网站分类: 技术文章 > 博客

作者:fggfg

链接:https://www.pythonheidong.com/blog/article/49478/f19134b37859862a3bf2/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

1 0
收藏该文
已收藏

评论内容:(最多支持255个字符)