用例设计原则

  • 文件名以test_*.py文件和*_test.py
  • 以test_开头的函数
  • 以Test开头的类
  • 以test_开头的方法
  • 所有的包pakege必须要有__init__.py文件

help帮助

1.查看pytest命令行参数,可以用pytest -h 或pytest --help查看

C:\Users\admin>pytest -h
usage: pytest [options] [file_or_dir] [file_or_dir] [...] positional arguments:
file_or_dir general:
-k EXPRESSION only run tests which match the given substring
expression. An expression is a python evaluatable
expression where all names are substring-matched
against test names and their parent classes. Example:
-k 'test_method or test_other' matches all test
functions and classes whose name contains
'test_method' or 'test_other', while -k 'not
test_method' matches those that don't contain
'test_method' in their names. Additionally keywords
are matched to classes and functions containing extra
names in their 'extra_keyword_matches' set, as well as
functions which have names assigned directly to them.
-m MARKEXPR only run tests matching given mark expression.
example: -m 'mark1 and not mark2'.
--markers show markers (builtin, plugin and per-project ones).
-x, --exitfirst exit instantly on first error or failed test reporting:
-v, --verbose increase verbosity.
-q, --quiet decrease verbosity.
--verbosity=VERBOSE set verbosity
只贴了一部分

按以下目录写用例

D:YOYO\
__init__.py test_class.py
# content of test_class.py
class TestClass:
def test_one(self):
x = "this"
assert 'h' in x def test_two(self):
x = "hello"
assert hasattr(x, 'check') def test_three(self):
a = "hello"
b = "hello world"
assert a in b test_sample.py
# content of test_sample.py
def func(x):
return x +1 def test_answer():
assert func(3)==5

python -m

cmd执行pytest用例有三种方法,以下三种方法都可以,一般推荐第一个

  • pytest

  • py.test

  • python -m pytest

如果不带参数,在某个文件夹下执行时,它会查找该文件夹下所有的符合条件的用例(查看用例设计原则)

执行用例规则

1.执行某个目录下所有的用例

pytest 文件名/

2.执行某一个py文件下用例

pytest 脚本名称.py

3.-k 按关键字匹配

pytest -k "MyClass and not method"

这将运行包含与给定字符串表达式匹配的名称的测试,其中包括Python
使用文件名,类名和函数名作为变量的运算符。 上面的例子将运行
TestMyClass.test_something但不运行TestMyClass.test_method_simple

4.按节点运行

每个收集的测试都分配了一个唯一的nodeid,它由模块文件名和后跟说明符组成
来自参数化的类名,函数名和参数,由:: characters分隔。

运行.py模块里面的某个函数

pytest test_mod.py::test_func

运行.py模块里面,测试类里面的某个方法

pytest test_mod.py::TestClass::test_method

5.标记表达式

pytest -m slow

将运行用@ pytest.mark.slow装饰器修饰的所有测试。

6.从包里面运行

pytest --pyargs pkg.testing

这将导入pkg.testing并使用其文件系统位置来查找和运行测试。

-x 遇到错误时停止测试

pytest -x test_class.py

从运行结果可以看出,本来有3个用例,第二个用例失败后就没继续往下执行了

D:\YOYO>pytest -x test_class.py
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: D:\YOYO, inifile:
collected 3 items test_class.py .F ================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________ self = <YOYO.test_class.TestClass object at 0x0000000003A29780> def test_two(self):
x = "hello"
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check') test_class.py:11: AssertionError
===================== 1 failed, 1 passed in 0.05 seconds ======================

--maxfail=num

pytest --maxfail=1

当用例错误个数达到指定数量时,停止测试

D:\YOYO>pytest --maxfail=1
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: D:\YOYO, inifile:
collected 4 items test_class.py .F ================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________ self = <YOYO.test_class.TestClass object at 0x0000000003A3D080> def test_two(self):
x = "hello"
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check') test_class.py:11: AssertionError
===================== 1 failed, 1 passed in 0.06 seconds ======================

pytest_02-用例运行规则的更多相关文章

  1. pytest文档2-用例运行规则

    用例设计原则 文件名以test_*.py文件和*_test.py 以test_开头的函数 以Test开头的类 以test_开头的方法 所有的包pakege必须要有__init__.py文件 help帮 ...

  2. Pytest单元测试框架-测试用例运行规则

    1.Pytest测试用例运行规则 在pytest单元测试框架下面执行用例,需要满足以下几个特点: 1. 文件名以test_*.py开头或者*_test.py 2. 测试类.测试函数以test开头 3. ...

  3. spark单机部署及样例运行

    spark单机运行部署 环境预装 需要预先下载jdk和spark.机器使用centos6.6(推荐).然后依次运行 [root@spark-master root]# cd /root #安装必要的软 ...

  4. TestNG监听器实现用例运行失败自动截图、重运行功能

    注: 以下内容引自 http://blog.csdn.net/sunnyyou2011/article/details/45894089 (此非原出处,亦为转载,但博主未注明原出处) 使用Testng ...

  5. Python守护进程和脚本单例运行

    Python 守护进程 守护进程简介 进程运行有时候需要脱离当前运行环境,尤其是Linux和Unix环境中需要脱离Terminal运行,这个时候就要用到守护进程.守护进程可以脱离当前环境要素来执行,这 ...

  6. HttpRunner学习11--指定用例运行次数

    前言 在HttpRunner中,一般情况下,我们写的用例脚本都是每次运行一次,如果我们想要指定用例运行的次数,可以通过 times 关键字来实现. 测试场景 在这里,我们以访问 TesterHome ...

  7. DevExpress Winform使用单例运行程序方法和非DevExpress使用Mutex实现程序单实例运行且运行则激活窗体的方法

    原文:DevExpress Winform使用单例运行程序方法和非DevExpress使用Mutex实现程序单实例运行且运行则激活窗体的方法 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA ...

  8. pytest_用例运行级别_模块级

    ''' pytest 参数说明 https://www.jianshu.com/p/7a7432340f02 -x test_fixt_model.py 遇到错误时,停止运行 用-v运行(-v显示运行 ...

  9. pytest文档40-pytest.ini配置用例查找规则(面试题)

    前言 面试题:pytest如何执行不是test开头的用例?如执行 xxx_*.py这种文件的用例. pytest.ini 配置文件可以修改用例的匹配规则. pytest命令行参数 cmd打开输入pyt ...

随机推荐

  1. shell 判断字符串包含的5种方法

    strA="long string" strB="string" result=$(echo $strA | grep "${strB}") ...

  2. SpringBoot上传文件到本服务器 目录与jar包同级问题

    目录 前言 原因 实现 不要忘记 最后的封装 Follow up   前言 看标题好像很简单的样子,但是针对使用jar包发布SpringBoot项目就不一样了.当你使用tomcat发布项目的时候,上传 ...

  3. 模拟25A 题解

    A. Lighthouse m的范围极小,显然的容斥. 总的方案数,减去受任意一个限制的方案数,加回受两个限制的方案数. 就能得到受所有限制的的方案数. 将选择的一些边所指向的点放在同一个联通块里. ...

  4. 《Genius》

    Einstein is genius.         爱因斯坦是天才,这是一个毫无疑问的命题.他在专利局空闲之余写下的相对论,可以让我们学习很久一段时间,而且还不一定能学懂. 这部十集的电视连续剧是 ...

  5. android中SELINUX规则分析和语法简介

    1. SELINUX是可以理解为一种android上面的安全机制,是有美国国家安全局和一些公司设计的一个针对linux的安全加强系统我们可以通过配置SELINUX的相关policy,来定制自己的手机的 ...

  6. linux驱动由浅入深系列:高通sensor架构实例分析之二(驱动代码结构)【转】

    本文转载自:https://blog.csdn.net/radianceblau/article/details/73498303 本系列导航: linux驱动由浅入深系列:高通sensor架构实例分 ...

  7. mac opencv 提示摄像头权限问题

    通常在iOS开发下,我们的app需要在Info.plist文件中配置所需要的各种限制:如摄像头权限: 本次我们在mac下创建了一个command line 程序,并且设定是c++开发,并配置了open ...

  8. vue路由的异步加载(懒加载)方法

    vue路由的异步加载(懒加载)方法. javascriptvue.jsvue-router  阅读约 2 分钟 vue本身不多介绍.直接说问题,因为vue的所有路由都是加载在一个app.js里的,如果 ...

  9. C# Area区域配置,修改默认路由

    1.右键项目新建文件夹 Areas 2.先把项目分类包好,建两个文件夹,放Controller和View,Model也可以放在这里 因为项目启动默认打开的是Home/Index ,我把它放在了Webs ...

  10. shell基础知识之 stdin,stdout,stderr和文件描述符

    stdin,stdout,stderr stdin=0 stdout=1 stderr=2 使用tee来传递内容,把stdout 作为stdin 传到下个命令 root@172-18-21-195:/ ...