Pytest框架学习1--基本规则

学习内容来自 对https://www.cnblogs.com/yoyoketang  自动化大师悠悠的总结
1.pytest运行规则:**查找当前目录及其子目录下以test_*.py或*_test.py文件,找到文件后,在文件中找到以test开头函数并执行。**
2.命令行下执行pytest或py.test或python -m pytest
3.通过pytest -q test_a.py 可以仅执行 test_a.py中以test开头的函数
4.测试类必须以Test开头,并且不能带有 init 方法
5.测试函数以test开头
6.所有的包pakege必须要有__init__.py文件
7.执行某一个py文件下用例  pytest 脚本名称.py
8.-k 按关键字匹配  pytest -k "Class and not two"将不会执行test_sample下的测试脚本和test_two这个方法
9.pytest test_mod.py::TestClass::test_method运行test_mod.py下TestClass下的test_method方法
10.pytest -x 遇到错误时停止测试
11.pytest --maxfail=2错误数量最大等于2时停止测试
12.以pytest方式运行,需要改该工程设置默认的运行器:file->Setting->Tools->Python Integrated Tools->项目名称->Default test runner->选择py.test
   注意是先改项目运行方式,再写代码才能出来
13. 运行结果“.F. ” 点是代表测试通过,F是Fail的意思,1 warnings是用于pytest.main('-q test_class.py')里面参数需要传list,多个参数放list就不会有警告了

目录结构:

测试脚本如下

test_class.py

class TestClass:
    def test_one(self):
        print 'one'
        x = "this"
        assert 'h' in x

    def test_two(self):
        print 'two'
        x = "hello"
        assert hasattr(x, 'check')


if __name__ == '__main__':
    pytest.main(['-q','test_class.py'])

test_sample.py

#!usr/bin/python
# -*- coding: utf-8 -*-
def func(x):
    return x + 1


def test_answer():
    assert func(3) == 5

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值