pytest_mysql_pytest扫盲1--pytest运行规则

1、pytest安装: pip install -U pytest -i https://pypi.tuna.tsinghua.edu.cn/simple

2、pytest运行方式:

不带参数:  pytest

指定模块:  pytest  .py文件名

指定目录:  pytest dir\

关键字过滤: pytest -k "MyClass and not method"

关键字包括 文件名、类名、方法名

节点id:    py模块名::类名::方法名 或者 py模块名::函数名    pytest test_xxx.py::TestXxx::func_xxx

标签表达式: 执行被装饰器 @pytest.mark.smoke 装饰的所有测试用例    pytest -m smoke

从包里面运行:  从pkg.test包所在位置查找和执行用例    pytest --pyargs pkg.test

停止测试:

-x 遇到错误时停止测试  pytest-x test_xxx.py

当用例错误个数达到指定数量时,停止测试  ytest –maxfail=1

3、pytest运行规则:

pytest 匹配 当前目录下以 test_*.py 或者 *_test.py 命名的所有文件

如果文件中存在以 test_ 开头的函数,则运行所有 test_ 开头的函数

如果文件中存在以 Test_ 开头的类(没有__init__函数),则匹配 Test_ 类中以 test_ 开头的方法

4、编写一个名为 demo_1.py 的文件

def my_func(x):

return x

def test_my_func():

assert my_func('hello world!') == 'hello world'

class TestMethod:

def test_1(self):

a = 'hello world!'

assert 'hello' in a

def test_2(self):

x = 'hello world!'

assert hasattr(x, 'helloWorld')

def others(self):

b = 3

assert b == 4

1)直接执行 pytest,运行结果未找到可执行用例,因为文件名为 demo_1,不符合pytest运行的规则

8e748e8aed10db47cd2519ad5a58b617.png

2)指定文件名,执行 pytest demo_1.py,用例执行成功,只执行 test_开头的函数,others()方法未执行

fa5d3a544767ad41bf67454801266cdf.png

5、测试用例设计规则:

以test_*.py 或者 *_test.py 的文件

以 test 开头的函数名

以Test 开头的类(没有__init__函数),以test_开头的函数

6、unittest 框架代码兼容,修改 pycharm 测试脚本的默认执行器为 pytest,再次执行时,会出现 pytest in xx.py 的执行方式。

149c5f541436e4cfd8be8cc4ea192a9a.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值