Python pytest命令行的作用是什么,如何应用
Admin 2022-05-26 群英技术资讯 808 次浏览
这篇文章主要介绍“Python pytest命令行的作用是什么,如何应用”,有一些人在Python pytest命令行的作用是什么,如何应用的问题上存在疑惑,接下来小编就给大家来介绍一下相关的内容,希望对大家解答有帮助,有这个方面学习需要的朋友就继续往下看吧。用命令行方式调用用例是我们最常用的方式,这方面确实比java的TestNG框架要好用许多,至少不用写xml文件,为了提供定制化运行用例的方式,pytest提供了许多运行命令以供定制化运行某一类测试用例或者某个测试用例等;
在pycharm里写好了测试用例后如何运行呢?pycharm里好像并没有像eclipse里提供TestNG用的插件一样可以一键执行的方式,那么我们可以使用命令行的方式来进行,如下图所示为一个用例文件:

代码如下:
#-*- coding: utf-8 -*-
import pytest
class Test_simple():
@pytest.mark.test
def test_case1(self):
print("testCase1")
tof = True
assert tof
@pytest.mark.normal
@pytest.mark.test
def test_case2(self):
print("testCase2")
tof = False
assert tof
def test_case3(self):
print("testCase3")
assert True
@pytest.mark.test
def setup_class(self):
print("用于test组")
@pytest.mark.normal
def setup_class(self):
print("用于normal组")
如上所示添加了一个名为testSimple的工程,内添加了一些测试用例即Test_simple;
想要运行用例时可以打开下方的Terminal窗口:

会自动切换到当前工程目录下,而后即可使用pytest的命令了,如下对运行结果简单做下说明:

在终端中使用pytest也是和在pycharm中类似,如下以windows系统为例:
先切换到用例所在工程或者目录而后运行pytest即可,如下:

linux系统中也是同样的使用方法,只是如果没有为pytest添加软连接,则需要在pytest前面加上python命令;
全部运行时不需要添加任何后缀,只需要添加命令pytest即可,此时打印的信息比较简单:
E:\pyspace\testSimple>pytest
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items
testcase\Test_simple.py .F. [100%]
=============================================================================================================================== FAILURES ===============================================================================================================================
________________________________________________________________________________________________________________________ Test_simple.test_case2 ________________________________________________________________________________________________________________________
self = <testcase.Test_simple.Test_simple object at 0x00000000038508D0>
@pytest.mark.normal
@pytest.mark.test
def test_case2(self):
print("testCase2")
tof = False
> assert tof
E assert False
testcase\Test_simple.py:18: AssertionError
------------------------------------------------------------------------------------------------------------------------- Captured stdout call -------------------------------------------------------------------------------------------------------------------------
testCase2
================================================================================================================== 1 failed, 2 passed in 0.08 seconds ==================================================================================================================
E:\pyspace\testSimple>
如上图所示,只显示了用例时成功还是失败,至于里边的log则没有打印,那么如果我们想要看运行详细信息怎么办呢?可以加上-v标签,如下:
E:\pyspace\testSimple>pytest -v
========================================================================================================================= 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', 'allure-pytest': '2.6.3'}, 'JAVA_HOME': 'D:\\project\\jdk1.8'}
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items
testcase/Test_simple.py::Test_simple::test_case1 PASSED [ 33%]
testcase/Test_simple.py::Test_simple::test_case2 FAILED [ 66%]
testcase/Test_simple.py::Test_simple::test_case3 PASSED [100%]
=============================================================================================================================== FAILURES ===============================================================================================================================
________________________________________________________________________________________________________________________ Test_simple.test_case2 ________________________________________________________________________________________________________________________
self = <testcase.Test_simple.Test_simple object at 0x000000000382EDA0>
@pytest.mark.normal
@pytest.mark.test
def test_case2(self):
print("testCase2")
tof = False
> assert tof
E assert False
testcase\Test_simple.py:18: AssertionError
------------------------------------------------------------------------------------------------------------------------- Captured stdout call -------------------------------------------------------------------------------------------------------------------------
testCase2
================================================================================================================== 1 failed, 2 passed in 0.08 seconds ==================================================================================================================
E:\pyspace\testSimple>
如上图会把详细信息都打印出来
如果用例中包含多个分组,想要只运行其中一个组,则使用-m "组名"的方式,依然使用如上代码,运行命令和结果如下:
E:\pyspace\testSimple>pytest -s -m "normal"
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items / 2 deselected / 1 selected
testcase\Test_simple.py 用于normal组
testCase2
F
=============================================================================================================================== FAILURES ===============================================================================================================================
________________________________________________________________________________________________________________________ Test_simple.test_case2 ________________________________________________________________________________________________________________________
self = <testcase.Test_simple.Test_simple object at 0x00000000036D27F0>
@pytest.mark.normal
@pytest.mark.test
def test_case2(self):
print("testCase2")
tof = False
> assert tof
E assert False
testcase\Test_simple.py:18: AssertionError
================================================================================================================ 1 failed, 2 deselected in 0.07 seconds ================================================================================================================
E:\pyspace\testSimple>
-k选项允许我们设置表达式来运行某些用例,如下传参就只运行了test_case1和test_case2
E:\pyspace\testSimple>pytest -v -k "case1 or case2"
========================================================================================================================= 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', 'allure-pytest': '2.6.3'}, 'JAVA_HOME': 'D:\\project\\jdk1.8'}
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items / 1 deselected / 2 selected
testcase/Test_simple.py::Test_simple::test_case1 PASSED [ 50%]
testcase/Test_simple.py::Test_simple::test_case2 FAILED [100%]
表达式的写法有许多,可以用全称如test_case1这样也可以去掉test_,除了or外也可以使用not来指定那些用例不跑;
pytest的原本运行规则是每条用例均执行,不管是否有失败,如果我们想在用例运行时遇到失败即停止,则可以使用-x,如下所示,第二条用例失败后则不再运行第三条用例:
E:\pyspace\testSimple>pytest -v -x
========================================================================================================================= 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', 'allure-pytest': '2.6.3'}, 'JAVA_HOME': 'D:\\project\\jdk1.8'}
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items
testcase/Test_simple.py::Test_simple::test_case1 PASSED [ 33%]
testcase/Test_simple.py::Test_simple::test_case2 FAILED [ 66%]
=============================================================================================================================== FAILURES ===============================================================================================================================
________________________________________________________________________________________________________________________ Test_simple.test_case2 ________________________________________________________________________________________________________________________
self = <testcase.Test_simple.Test_simple object at 0x00000000037A9B00>
@pytest.mark.normal
@pytest.mark.test
def test_case2(self):
print("testCase2")
tof = False
> assert tof
E assert False
testcase\Test_simple.py:18: AssertionError
------------------------------------------------------------------------------------------------------------------------- Captured stdout call -------------------------------------------------------------------------------------------------------------------------
testCase2
================================================================================================================== 1 failed, 1 passed in 0.08 seconds ==================================================================================================================
E:\pyspace\testSimple>
指定运行某个py文件,只需要接上文件相对路径即可:
E:\pyspace\testSimple>pytest -v testcase/Test_example.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', 'allure-pytest': '2.6.3'}, 'JAVA_HOME': 'D:\\project\\jdk1.8'}
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 1 item
testcase/Test_example.py::Test_example::test_aaa PASSED [100%]
======================================================================================================================= 1 passed in 0.02 seconds =======================================================================================================================
E:\pyspace\testSimple>
写法为:py文件路径::class名称,范例如下:
E:\pyspace\testSimple>pytest -v testcase/Test_example.py::Test_example2
========================================================================================================================= 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', 'allure-pytest': '2.6.3'}, 'JAVA_HOME': 'D:\\project\\jdk1.8'}
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 1 item
testcase/Test_example.py::Test_example2::test_bbb PASSED [100%]
======================================================================================================================= 1 passed in 0.08 seconds =======================================================================================================================
E:\pyspace\testSimple>
写法为:py文件路径::class名称::method名称,范例如下:
E:\pyspace\testSimple>pytest -v testcase/Test_example.py::Test_example2
========================================================================================================================= 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', 'allure-pytest': '2.6.3'}, 'JAVA_HOME': 'D:\\project\\jdk1.8'}
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 1 item
testcase/Test_example.py::Test_example2::test_bbb PASSED [100%]
======================================================================================================================= 1 passed in 0.08 seconds =======================================================================================================================
E:\pyspace\testSimple>
如上几种也可以组合使用;
pytest还包含许多其他用法,具体用法可以使用pytest --help来查看,如下:

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要为大家介绍了一些在Python中常用的图片处理函数的使用,例如split()、merge()、threshold()、applyColorMap()等,需要的可以参考一下
这篇文章主要介绍了python open读取文件内容时的mode模式解析,Python可以使用open函数来实现文件的打开,关闭,读写操作,本文给大家介绍的非常详细,需要的朋友可以参考下
python的两个单元测试包分别是 doctest 和 unittest,这两个包的使用起来各有长处,适用于不同的场景,这篇文章主要介绍了Python单元测试的两种写法,需要的朋友可以参考下
这篇文章主要为大家详细介绍了如何利用Python语言绘制好看的数据动态图,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起动手尝试一下
如何使用pytorch加载并读取COCO数据集 环境配置基础知识:元祖、字典、数组利用PyTorch读取COCO数据集利用PyTorch读取自己制作的数据集
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008