当前位置:网站首页>An article to teach you a quick start and basic explanation of Pytest, be sure to read

An article to teach you a quick start and basic explanation of Pytest, be sure to read

2022-08-10 22:00:00 Rejoice in the testing world

前言

  • 目前有两种纯测试的测试框架,pytest和unittest

  • unittest应该是广为人知,而且也是老框架了,很多人都用来做自动化,无论是UI还是接口

  • pytest是基于unittest开发的另一款更高级更好用的单元测试框架

  • Go out for an interview,It's good to talk to others,pytestis significantly higher than that of unittest

为什么要用Pytest

pytest 的官方网站介绍,它具有如下特点:

  • 非常容易上手,入门简单,文档丰富,文档中有很多实例可以参考
  • 能够支持简单的单元测试和复杂的功能测试
  • 支持参数化
  • 执行测试过程中可以将某些测试跳过(skip),或者对某些预期失败的case标记成失败
  • 支持重复执行(rerun)失败的 case
  • 支持运行由 nose, unittest 编写的测试 case
  • 可生成 html 报告
  • Convenient sustainable integration tool jenkins 集成
  • 可支持执行部分用例
  • 具有很多第三方插件,并且可以自定义扩展

在这里插入图片描述

安装Pytest

cmd运行

pip install -U pytestpip3 install pytest -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

查看版本

pytest --version

快速开始

#!/usr/bin/env python# -*- coding: utf-8 -*-"""__title__  =__Time__   = 2020-04-06 12:33__Author__ = 小菠萝测试笔记__Blog__   = https://www.cnblogs.com/poloyy/"""def func(x):    return x + 1def test_answer():    assert func(3) == 5class TestClass:    def test_one(self):        x = "this"        assert "h" in x    def test_two(self):        x = "hello"        assert hasattr(x, "check")

然后,cmd进入当前文件目录,直接执行

pytest

在这里插入图片描述

知识点

如果只执行 pytest ,会查找当前目录及其子目录下以 test_*.py 或 *_test.py 文件,找到文件后,在文件中找到以 test 开头函数并执行
如果只想执行某个文件,可以 pytest start.py
加上-q,就是显示简单的结果: pytest -q start.py

Pytest用例的设计原则

用Pytest写用例时候,一定要按照下面的规则去写,否则不符合规则的测试用例是不会执行的

文件名以 test_.py 文件和test.py
以 test
开头的函数
以 Test 开头的类,不能包含 init 方法
以 test_ 开头的类里面的方法
所有的包 package 必须要有__init__.py 文件

Pytest执行用例规则

注意,The following are all incmd中执行pytest命令

1、All use cases in a directory

pytest

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

pytest 脚本名称.py

3、运行start.py 模块里面的某个函数,或者某个类,method in a class

加v和不加-v都可以,加-v的话,打印的信息更详细

pytest -v 08_mark.py::TestClass::test_methodpytest 08_mark.py::TestClass::test_methodpytest start.py::test_answer

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

pytest start.py::TestClass::test_two

在这里插入图片描述

5、-m 标记表达式(后续讲解)

 pytest -m login

将运行用 @pytest.mark.login 装饰器修饰的所有测试,I'll expand on the markup later

6、-q 简单打印,只打印测试用例的执行结果

 pytest -q start.py

7、-s 详细打印

 pytest -s start.py

8、-x 遇到错误时停止测试

pytest start.py -x

9、—maxfail=num,当用例错误个数达到指定数量时,停止测试

pytest start.py --maxfail=1

10、-k 匹配用例名称

执行测试用例名称包含http的所有用例

pytest -s -k http start.py

11、-k Exclude some use cases based on the use case name

1 pytest -s -k "not http" start.py

12、-k 同时匹配不同的用例名称

pytest -s -k "method or weibo" start.py

Pycharm运行Pytest

平时写代码,we are all therePycharm写的,How could it be used all the timecmdLet's run the use case,现在我们就来看看在Pycharm中如何运行Pytest

1、首先,我们先要去settings里面设置单元测试框架为Pytest

2、如果是nosetests的话,Right click and run yespythonOh the script works

3、如果设置了unittest则是以unittest框架去运行

注意

pytest 是可以兼容 unittest 脚本的,之前写的 unittest 用例也能用 pytest 框架去运行

最后: You can get a copy yourself at the bottom216页软件测试工程师面试宝典文档资料【免费的】.以及相对应的视频学习教程免费分享!,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等.

喜欢软件测试的小伙伴们,如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一 键三连哦!

软件测试工程师自学教程:

这才是2022最精细的自动化测试自学教程,我把它刷了无数遍才上岸字节跳动,做到涨薪20K【值得自学软件测试的人刷】

接口性能测试 — 软件测试人必会618实战场景分析

软件测试工程师月薪2W以上薪资必学技能 — Python接口自动化框架封装.

美团面试真题_高级测试25K岗位面试 — 软件测试人都应该看看

测试开发之全面剖析自动化测试平台 — 软件测试人的必经之路

软件测试必会_Jmeter大厂实战 — 仅6步可实现接口自动化测试

Jmeter实战讲解案例 — 软件测试人必会

在这里插入图片描述

在这里插入图片描述

原网站

版权声明
本文为[Rejoice in the testing world]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208102110073346.html