python 测试用例 自动生成_pythonpytest自动测试框架生成测试报告,PythonPytest,自动化...

Time will tell.

本章节来说说如何生成测试报告准备测试代码如下:

#coding: utf-8

import pytest

@pytest.fixture()

def login():

print '输入账号、密码登录'

def test_step_1(login):

print '用例步骤1:登录之后其它动作111'

def test_step_2(): #不需要登录

print '用例步骤2: 不需要登录, 操作222'

def test_step_3(login):

print '用例步骤3:登录之后其它动作333'

一、生成JunitXML 格式的测试报告

JunitXML 报告是一种很常用的测试报告,比如可以和 Jenkins 进行集成,在 Jenkins 的 GUI 上显示 Pytest 的运行结果,非常便利。

运行完

case

后可以到

report

路径下去查看相应的 xml 文件,也可以在 PyCharm 打开该xml文件查看。

执行命令:

pytest -v test_smtpsimple.py --junitxml=log.xml

结果:

(wda_python) bash-3.2$ pytest -v test_smtpsimple.py --junitxml=log.xml

========================================================== test session starts ===========================================================

platform darwin -- Python 2.7.15, pytest-4.1.0, py-1.7.0, pluggy-0.8.0 -- /Users/jackey/Documents/iOS/code/iOS-Auto/MyPyEnv/wda_python/bin/python2.7

cachedir: .pytest_cache

rootdir: /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test, inifile:

collected 3 items

test_smtpsimple.py::test_step_1 PASSED [ 33%]

test_smtpsimple.py::test_step_2 PASSED [ 66%]

test_smtpsimple.py::test_step_3 PASSED [100%]

---------------------------- generated xml file: /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test/log.xml ----------------------------

======================================================== 3 passed in 0.02 seconds ========================================================

(wda_python) bash-3.2$

生存的 log.xml

输入账号、密码登录

用例步骤1:登录之后其它动作111

用例步骤2: 不需要登录, 操作222

输入账号、密码登录

用例步骤3:登录之后其它动作333

二、生成 result log 格式的测试报告

(wda_python) bash-3.2$ pytest -s -v test_smtpsimple.py --resultlog=log.txt

========================================================== test session starts ===========================================================

platform darwin -- Python 2.7.15, pytest-4.1.0, py-1.7.0, pluggy-0.8.0 -- /Users/jackey/Documents/iOS/code/iOS-Auto/MyPyEnv/wda_python/bin/python2.7

cachedir: .pytest_cache

rootdir: /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test, inifile:

collected 3 items

test_smtpsimple.py::test_step_1 输入账号、密码登录

用例步骤1:登录之后其它动作111

PASSED

test_smtpsimple.py::test_step_2 用例步骤2: 不需要登录, 操作222

PASSED

test_smtpsimple.py::test_step_3 输入账号、密码登录

用例步骤3:登录之后其它动作333

PASSED

============================================================ warnings summary ============================================================

/Users/jackey/Documents/iOS/code/iOS-Auto/MyPyEnv/wda_python/lib/python2.7/site-packages/_pytest/resultlog.py:39

/Users/jackey/Documents/iOS/code/iOS-Auto/MyPyEnv/wda_python/lib/python2.7/site-packages/_pytest/resultlog.py:39: PytestDeprecationWarning: --result-log is deprecated and scheduled for removal in pytest 5.0.

See https://docs.pytest.org/en/latest/deprecations.html#result-log-result-log for more information.

_issue_warning_captured(RESULT_LOG, config.hook, stacklevel=2)

-- Docs: https://docs.pytest.org/en/latest/warnings.html

================================================== 3 passed, 1 warnings in 0.01 seconds ==================================================

(wda_python) bash-3.2$

现在会提示过期, 生存的 Log.txt

. test_smtpsimple.py::test_step_1

. test_smtpsimple.py::test_step_2

. test_smtpsimple.py::test_step_3

可改成这样:

pytest -s -v test_smtpsimple.py > log.txt

生存的 log.txt 为:

============================= test session starts ==============================

platform darwin -- Python 2.7.15, pytest-4.1.0, py-1.7.0, pluggy-0.8.0 -- /Users/jackey/Documents/iOS/code/iOS-Auto/MyPyEnv/wda_python/bin/python2.7

cachedir: .pytest_cache

rootdir: /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test, inifile:

collecting ... collected 3 items

test_smtpsimple.py::test_step_1 输入账号、密码登录

用例步骤1:登录之后其它动作111

PASSED

test_smtpsimple.py::test_step_2 用例步骤2: 不需要登录, 操作222

PASSED

test_smtpsimple.py::test_step_3 输入账号、密码登录

用例步骤3:登录之后其它动作333

PASSED

=========================== 3 passed in 0.01 seconds ===========================

三、生成Html格式的测试报告

html 格式的测试报告在浏览器观看效果很好,还可以把这些测试报告放在 Web 服务器上。

首先,需要安装

pytest-html

插件。

pip install pytest-html

使用指令:

pytest -s -v test_smtpsimple.py --html=log.html

结果:

(wda_python) bash-3.2$ pytest -s -v test_smtpsimple.py --html=log.html

========================================================== test session starts ===========================================================

platform darwin -- Python 2.7.15, pytest-4.1.0, py-1.7.0, pluggy-0.8.0 -- /Users/jackey/Documents/iOS/code/iOS-Auto/MyPyEnv/wda_python/bin/python2.7

cachedir: .pytest_cache

metadata: {'Python': '2.7.15', 'Platform': 'Darwin-17.0.0-x86_64-i386-64bit', 'Packages': {'py': '1.7.0', 'pytest': '4.1.0', 'pluggy': '0.8.0'}, 'Plugins': {'html': '1.20.0', 'metadata': '1.8.0'}}

rootdir: /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test, inifile:

plugins: metadata-1.8.0, html-1.20.0

collected 3 items

test_smtpsimple.py::test_step_1 输入账号、密码登录

用例步骤1:登录之后其它动作111

PASSED

test_smtpsimple.py::test_step_2 用例步骤2: 不需要登录, 操作222

PASSED

test_smtpsimple.py::test_step_3 输入账号、密码登录

用例步骤3:登录之后其它动作333

PASSED

--------------------------- generated html file: /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test/log.html ---------------------------

======================================================== 3 passed in 0.03 seconds ========================================================

(wda_python) bash-3.2$

生存的log.html用浏览器打开:

python 测试用例 自动生成_pythonpytest自动测试框架生成测试报告,PythonPytest,自动化..._第1张图片

python 测试用例 自动生成_pythonpytest自动测试框架生成测试报告,PythonPytest,自动化..._第2张图片

好了,关于以上内容就分享到这,如果你对更多内容、及Python实例练习题、面试题、自动化软件测试感兴趣的话可以加入我们175317069一起学习喔。群里会有各项学习资源发放,更有行业深潜多年的技术人分析讲解。

最后祝愿你能成为一名优秀的软件测试工程师!

欢迎【评论】、【点赞】、【关注】~

Time will tell.(时间会证明一切)

你可能感兴趣的:(python,测试用例,自动生成)