pytest简单使用

介绍与快速入门

详细介绍官网:https://docs.pytest.org/en/latest/
安装:pip install pytest
  • pytest:是什么?
    • 和unitest一样是个框架
    • 简单
    • 易读
    • 支持参数化
    • 支持运行由Nosr,unittest编写的测试用例case
    • 具有很多第三方插件,并且可以自定义扩展
    • 支持重复执行失败的case
    • 方便的和持续集成工具集成
  • pytest与unitest的区别
    • pytset
      • 直接使用
      • 参数化使用自带的装饰器
      • 插件丰富:支持失败自动重新执行
      • pytest兼容unitest框架
    • unitest
    • 测试必须继承unitest.TestCase类
    • 参数化需要依赖三方库
    • 不支持失败自动重新执行
  • 基本使用
    • unitest前置和后置
      • 通过setup每个用例执行前,tearDown每个用例执行之后
      • 通过setUpClass类里面所有用例执行前执行,tearDownClass类里面所有用例执行后执行
    • pytest前置和后置
      • 函数级别:setup、tearDown
        • 用于测试方法的始末
        • 运行一次测试用例会执行一次setup和tearDown
      • 类级别:setup_class、teardowm_class
        • 运行于测试类的始末
        • 一个测试内只运行一次setup_class和teadown_class
  • 默认运行规则
    • 测试文件以test_.py开头或_test.py结尾
      • pytest会执行当前目录下的所有test_.py及_test.py格式的文件
    • 测试类以Test开头,并且不能带有_init_方法
    • 测试函数以test_开头
  • 自定义运行规则
    • 文件名:pytest.ini
    • 【pytest】
    • addopts=-s
    • 当前目录下的scripts文件夹 -可自己定义
    • testpaths=testcase
    • 当前目录下的scripts文件夹下,以test_开头,以.py结尾的所有文件 -可自己定义
    • Pythonfiles=test*.py
    • 当前目录下的scripts文件夹下,以test_开头,以.py结尾的所有文件,以Test_开头的类 -可自己定义
    • Pythonclasses=Test*
    • 前目录下的scripts文件夹下,以test_开头,以.py结尾的所有文件,以Test_开头的类内,以test_开头的方法 -可自定义
    • Pythonfunctions=test*
  • 断言
    • 判断XX为真
      • assert xx
    • 判断XX不为真
      • assert not xx
    • 判断b包含a
      • assert a in b
    • 判断a等于b
      • assert a==b
    • 判断a不等于b
      • assert a!=b
  • mark的使用
    • pytest提供了标记机制,允许使用mark对测试函数做标记
      @pytest.mark.test01
      def test_b(self):
      pass

      • 一个测试函数可以有多个标记
      • 一个mark也可以标记多个函数
      • 运行参数pytest -m test01
      • 运行多个参数pytest -m "test01 or test02"
  • 跳过测试
    • skip
      • 标记skip表示跳过该测试用例,运行不执行
      • skip(reason=None)
    • skipif
      • 条件判断验证是否忽略不执行
      • 判断条件为表达式skipif(condition,reason=None)
  • pytest数据参数化
    • 传入单个参数,@pytest.mark.parametrize(argnames,argvalues)
    • argnames:参数名
    • argvalues:参数对应的值,类型必须为可迭代类型,一般使用list
    • 传多个参数,@pytest.mark.parametrize(("name","password"),[("xiaoming","123456")("xiaohong","456789")])
    • list的每一个元素都是一个元组,元组里的每一个元素和按照参数顺序一一对应的
  • 失败重试
    • 应用场景
      • 当失败后尝试再次运行
    • 安装:pip install pytest-rerunfailures
    • 使用
      • 在配置文件中的命令行参数中增加 --reruns n
      • 如果你期望加上出错重试的等待时间,--reruns-delay
  • 出现乱码时使用这个代码进行转码
    req = requests.get("https://www.baidu.com/")
    if req.encoding == 'ISO-8859-1':
    encodings = requests.utils.get_encodings_from_content(req.text)
    if encodings:
    encoding = encodings[0]
    else:
    encoding = req.apparent_encoding
    global encode_content
    encode_content = req.content.decode(encoding, 'replace') #如果设置为replace,则会用?取代非法字符;
    print(encode_content)
    print(req.status_code)
    assert req.status_code==200
    with open('test.html','w',encoding='utf-8') as f:
    f.write(encode_content)
  • pytest.main(['-s','--html=report/test_x.html', 'test_pytest.py']) 执行测试文件
    • -s控制台显示
    • --html=report/test_x.html 测试用例地址
    • test_pytest.py 执行文件,可以指定方法
  • 命令行模式
    • pytest 文件路径/测试文件名
    • 例如:pytest ./test_abc.py
  • 运行模块中的指定用例
    • pytest test_mod.py::test_func
  • 运行模块中的指定方法
    -pytest test_mod.py::TestClass::test_method

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *