前言

pytest可以支持自定义标记,自定义标记可以把一个web项目划分多个模块,然后指定模块名称执行。app自动化的时候,如果想android和ios公用一套代码时,
也可以使用标记功能,标明哪些是ios用例,哪些是android的,运行代码时候指定mark名称运行就可以

mark标记

1.以下用例,标记test_send_http()为webtest

# content of test_server.py

import pytest

@pytest.mark.webtest
def test_send_http():
pass # perform some webtest test for your app def test_something_quick():
pass def test_another():
pass class TestClass:
def test_method(self):
pass if __name__ == "__main__":
pytest.main(["-s", "test_server.py", "-m=webtest"])

只运行用webtest标记的测试,cmd运行的时候,加个-m 参数,指定参数值webtest

$ pytest -v -m webtest
============================= test session starts =============================
platform win32 -- Python 3.6., pytest-3.6., py-1.5., pluggy-0.6.
rootdir: E:\YOYO\se, inifile:
plugins: metadata-1.7., html-1.19.
collected items / deselected test_server.py . =================== passed, deselected in 0.10 seconds ====================

如果不想执行标记webtest的用例,那就用"not webtest"

$ pytest -v -m "not webtest"
import pytest

@pytest.mark.webtest
def test_send_http():
pass # perform some webtest test for your app
def test_something_quick():
pass
def test_another():
pass
class TestClass:
def test_method(self):
pass if __name__ == "__main__":
pytest.main(["-s", "test_server.py", "-m='not webtest'"])

运行结果

============================= test session starts =============================
platform win32 -- Python 3.6., pytest-3.6., py-1.5., pluggy-0.6.
rootdir: E:\YOYO\se, inifile:
plugins: metadata-1.7., html-1.19.
collected items test_server.py .... ========================== passed in 0.06 seconds ===========================

-v 指定的函数节点id

如果想指定运行某个.py模块下,类里面的一个用例,如:TestClass里面test_method用例
每个test_开头(或_test结尾)的用例,函数(或方法)的名称就是用例的节点id,指定节点id运行用-v 参数

$ pytest -v test_server.py::TestClass::test_method

pycharm运行代码

if __name__ == "__main__":
pytest.main(["-v", "test_server.py::TestClass::test_method"])

运行结果

============================= test session starts =============================
platform win32 -- Python 3.6., pytest-3.6., py-1.5., pluggy-0.6. -- E:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.0', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.6.3', 'py': '1.5.4', 'pluggy': '0.6.0'}, 'Plugins': {'metadata': '1.7.0', 'html': '1.19.0'}, 'JAVA_HOME': 'D:\\java\\jdk17'}
rootdir: E:\YOYO\se, inifile:
plugins: metadata-1.7., html-1.19.
collecting ... collected item test_server.py::TestClass::test_method PASSED [%] ========================== passed in 0.06 seconds ===========================

当然也能选择运行整个class

$ pytest -v test_server.py::TestClass

也能选择多个节点运行,多个节点中间空格隔开

$ pytest -v test_server.py::TestClass test_server.py::test_send_http

pycharm运行参考

if __name__ == "__main__":
pytest.main(["-v", "test_server.py::TestClass", "test_server.py::test_send_http"])

-k 匹配用例名称

可以使用-k命令行选项指定在匹配用例名称的表达式

$ pytest -v -k http
$ pytest -v -k http # running with the above defined example module
=========================== test session starts ============================
platform linux -- Python .x.y, pytest-.x.y, py-.x.y, pluggy-.x.y -- $PYTHON_
˓→PREFIX/bin/python3.
cachedir: .pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile:
collecting ... collected items / deselected
test_server.py::test_send_http PASSED [%]
================== passed, deselected in 0.12 seconds ==================

您也可以运行所有的测试,根据用例名称排除掉某些用例:

$ pytest -k "not send_http" -v
=========================== test session starts ============================
platform linux -- Python .x.y, pytest-.x.y, py-.x.y, pluggy-.x.y -- $PYTHON_
˓→PREFIX/bin/python3.
cachedir: .pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile:
collecting ... collected items / deselected
test_server.py::test_something_quick PASSED [ %]
test_server.py::test_another PASSED [ %]
test_server.py::TestClass::test_method PASSED [%]
================== passed, deselected in 0.12 seconds ==================

也可以同时选择匹配 “http” 和“quick”

$ pytest -k "http or quick" -v
=========================== test session starts ============================
platform linux -- Python .x.y, pytest-.x.y, py-.x.y, pluggy-.x.y -- $PYTHON_
˓→PREFIX/bin/python3.
cachedir: .pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile:
collecting ... collected items / deselected
test_server.py::test_send_http PASSED [ %]
test_server.py::test_something_quick PASSED [%]
================== passed, deselected in 0.12 seconds ==================

自定义标记mark的更多相关文章

  1. pytest九:使用自定义标记 mark

    pytest 可以支持自定义标记,自定义标记可以把一个 web 项目划分多个模块,然后指定模块名称执行.app 自动化的时候,如果想android 和 ios 公用一套代码时,也可以使用标记功能,标明 ...

  2. pytest文档15-使用自定义标记mark

    前言 pytest可以支持自定义标记,自定义标记可以把一个web项目划分多个模块,然后指定模块名称执行.app自动化的时候,如果想android和ios公用一套代码时, 也可以使用标记功能,标明哪些是 ...

  3. pytest_使用自定义标记mark

    前言 pytest可以支持自定义标记,自定义标记可以把一个web项目划分多个模块,然后指定模块名称执行.app自动化的时候,如果想android和ios公用一套代码时,也可以使用标记功能,标明哪些是i ...

  4. Pytest使用自定义标记mark只执行部分用例

    • 场景:只执行符合要求的某一部分用例 可以把一个web项目划分多个模块,然后指定模块名称执行. App自动化时,如果想Android和IOS公用一套代码时,也可以使用标记功能,标明哪些是IOS 的用 ...

  5. Pytest系列(8) - 使用自定义标记mark

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 pytest 可以支持自定义 ...

  6. Pytest系列(八) - 自定义标记mark的使用

    一.前言: pytest 支持自定义标记,自定义标记方便用例模块划分,也可以理解方便管理,配合标记参数 -m使用 二.示例代码 # -*- coding: utf-8 -*- # @Time : 20 ...

  7. pytest 14 使用自定义标记mark

    标记失败用到的情况是,本身就知道这是失败的例子,所以,不用让他运行,直接跳过.或者是依赖于某个方法,某个方式失败的话,用例直接标记成失败. 标记失败有两种方法,一种是方法内部,一种是方法外部.内部用p ...

  8. pytest 13 使用自定义标记mark

    前言: pytest可以规定那些要跑,那些不跑,跑特定的哪些?比如以下的这个例子: #!/usr/bin/env/python # -*-coding:utf-8-*- import pytest @ ...

  9. Pytest学习笔记6-自定义标记mark

    前言 在pytest中,我们可以使用mark进行用例的自定义标记,通过不同的标记实现不同的运行策略 比如我们可以标记哪些用例是生产环境执行的,哪些用例是测试环境执行的,在运行代码的时候指定对应的mar ...

随机推荐

  1. Linux设置静态IP后出现的几种问题

    一.设置静态IP后无法重启网卡 如下图所示 原因分析:control process exited with error code.控制进程存在错误代码. 解决方案:可以检查网卡配置文件是否修改错误. ...

  2. 【vue】iView-admin2.0动态菜单路由【版2】

    依照iView-admin2.0动态菜单路由[版1] 归纳几个节点动态路由获取方式2 ——> easymock假数据 ——> 数据转组件处理.addRoutes ——> localS ...

  3. linux各路径(目录)的解释

    目录 /bin 存放二进制可执行文件(ls,cat,mkdir等),常用命令一般都在这里. /etc 存放系统管理和配置文件 /home 存放所有用户文件的根目录,是用户主目录的基点, 比如用户use ...

  4. Centos 7 SSh--端口号的更改

    前言:开启某服务或软件的端口,要从该服务或软件监听的端口(多以修改配置文件为主),SeLinux和防火墙(FireWall)的安全策略下手.如果使用阿里云,腾讯等第三方服务器还需要对管理控制台的安全组 ...

  5. 牛客练习赛47 A DongDong破密码 (异或性质,递推)

    链接:https://ac.nowcoder.com/acm/contest/904/A 来源:牛客网 DongDong破密码 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1310 ...

  6. 【GDOI2017模拟12.9】最近公共祖先

    题目 分析 首先,将这些节点按dfs序建一棵线段树. 因为按dfs序,所以在同一子树上的节点会放在线段树相邻的位置. 发现,对于一个位置x,它的权值只会对以x为根的子树造成影响. 当修改x时,用w[x ...

  7. layui中从子窗口传递数据到父窗口,第三个子弹层的值传给第二个弹层

    最近做一个项目的需要多个弹层,每个弹层中还需要数据传递, 经过测试,以下方法三个弹层才有效,如果只是有两个弹层,请用其它方法 大概如图,看图自己应该明白 如何在在b页面选择好的值传给a页面的问题,这个 ...

  8. 【shell】sed处理多行合并

    有这么一个题 文件格式 table=t1 name owner address table=t2 id text col1 comment col5 table=t3 prod_name price ...

  9. Zabbix MySQL监控模板添加

    zabbix自带的mysql监控指标很少,所以需要新增一些监控项 1.下载知数堂维护的percona-monitoring-plugin-zabbix代码 # cd /opt # wget https ...

  10. IDEA mapping箭头要怎么样设置哈(Free MyBatis插件)

    效果如下图: 当我们点击箭头的时候,会快速切换到我们相关联的类位置,就不用再像以前一样还要去找 而 Free MyBatis是一款让我们操作更加方便的插件,你值得拥有哦~~~ idea 选择 File ...