Python教程之pytest命令行方式运行用例
作者:df0128 发布时间:2022-08-01 22:08:06
前言
用命令行方式调用用例是我们最常用的方式,这方面确实比java的TestNG框架要好用许多,至少不用写xml文件,为了提供定制化运行用例的方式,pytest提供了许多运行命令以供定制化运行某一类测试用例或者某个测试用例等;
pycharm里命令行运行用例
在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
在终端中使用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>
打印详情-v
如上图所示,只显示了用例时成功还是失败,至于里边的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
-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
来指定那些用例不跑;
遇到失败即停止运行-x
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文件
指定运行某个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>
指定运行某个class
写法为: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
来查看,如下:
来源:https://blog.csdn.net/df0128/article/details/91043150


猜你喜欢
- 转自: http://www.qqread.com/mysql/z442108305.html对于程序开发人员而言,目前使用最流行的两种后台
- 一、前言今天学习视频时课后作业是找出1000以内既是素数又是回文数的数,写代码这个很容易,结果一运行遇到了bug,输出结果跟预期不一样,调试
- 之前在用预训练的ResNet的模型进行迁移训练时,是固定除最后一层的前面层权重,然后把全连接层输出改为自己需要的数目,进行最后一层的训练,那
- 场景:有一个多层嵌套的列表如:[[23],[3,3],[22,22],1,123,[[123,a],2]] 拆分成:def splitlis
- 代码如下所示:scole = input("input your scole:")if scole>90: &nb
- 目录1、简介2、正文2.1 where子句位置2.2 操作符2.3 空值null1、简介当我们需要获取数据库表数据的特定子集时,可以使用wh
- 昨天又翻了下前段时间WD内部培训的幻灯片,发现了kejun推荐的一篇好文:Javascript Closures,看了之后受益匪浅。这篇文章
- 一、运算符算术运算符:+ - * / 可以在select 语句中使用连接运算符:|| select deptno|| dname from
- Python实现八大排序算法,具体内容如下1、插入排序描述插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的、个
- python可以简单优美,也很有趣,下面是收集的例子:1.一句话开始一个http的文件服务器:$ python -m SimpleHTTPS
- 一、效果展示话不多说先上效果为了更有意境我加了个完美的背景来衬托出月饼的好看我的月饼画的不圆的原因是我故意的,为什么呢?因为月有阴晴圆缺啊!
- 使用软件:MySQLMigrationTool 提示数据过大,无法导入。修改my.cnf文件的max_allowed_packet = 10
- 参考阅读:https://www.jb51.net/article/169285.htm代码:set objTTS = createobje
- 文章主要讲术了一些SQL Server新的Bug,帮您认识这些被忽略的SQL Server注入技巧。1.关于Openrowset和Opend
- 我们有时候,看到几k的日志文件,一大堆,一个一个打开又很麻烦,少看几个,又担心遗漏,这个时候,如果有一个可以合并所有文本文件的工具就好了。下
- 本文是小编针对js保留两位小数这个大家经常遇到的经典问题整理了在各种情况下的函数写法以及遇到问题的分析,以下是全部内容:一、我们首先从经典的
- asp创建pdf文件代码,详见以下代码:<%Option ExplicitSub CheckXlDriver()&
- 本文实例讲述了Python创建系统目录的方法。分享给大家供大家参考。具体如下:Python2 mkdir在没有上级目录时创建会失败.该方法可
- 扩展名在写Python程序时我们常见的扩展名是py, pyc,其实还有其他几种扩展名。下面是几种扩展名的用法。pypy就是最基本的源码扩展名
- 经常碰到的一个问题是limit的offset太高,如:limit 100000,20,这样系统会查询100020条,然后把前面的