目录

pytest基础知识

pytest基础知识

pytest知识了解

pytest的基础知识了解:

(包含设置断点,pdb,获取最慢的10个用例的执行耗时)

pytest-pytest.main()运行测试用例,pytest参数:

https://i-blog.csdnimg.cn/blog_migrate/a46bef0446a4e7deef71ee886ef46631.png

报错 usage: runAll.py [options] [file_or_dir] [file_or_dir] […]  ,是由于没有安装pytest-html

https://i-blog.csdnimg.cn/blog_migrate/61a450ad277b02dc7ef3a04b44489710.png

pytest  的使用

pytest中标签名的使用 :

Pytest装饰器   @pytest.mark.parametrize 的使用:

@pytest.mark.xfail  :

@pytest.mark.skipif() https://i-blog.csdnimg.cn/blog_migrate/0b34680e92b9ab74fe3912382c13d1e3.png

pytest 中的 fixture的使用

pytest 中的 fixture的使用(1):

pytest 的fixture的使用 (2):

将固件参数化:

注意:

  1. 需要安装 pytest-html

若出现了 report.html 运行的0 test, 则说明是文件的命名不对

pytest的命名规则:默认从当前目录中搜集测试用例,即在哪个目录下运行 pytest 命令,则中哪个目录中搜索

-符合命令规则 test_*.py 或者 *_test.py 的文件

-以 test_ 开头的函数名

-以 Test 开头的测试类(没有__init__函数)当中,以 test_ 开头的函数

3.  @pytest.fixture() 以及 @pytest.mark.usefixture() 的用法

当function级别的 fixture 没有设置  autouse的时候,  执行完类级别的fixture,会直接执行 类中的函数

https://i-blog.csdnimg.cn/blog_migrate/555e7949b72d604bc89ab9a6445e6b4f.png

当function级别的 fixture 有设置  autouse的时候,  执行完类级别的fixture,会调用函数级别的fixture,再去执行类中的函数

(autouse: 如果为True,则fixture func将为所有测试用例激活可以看到它。如果为False(默认值),则需要显式激活fixture)

  1. 上述的案例中,fixture函数 和 测试用例写在同一文件内,如果存在多个py 文件调用的话,则需要建一个 confest文件 ,test_xxx.py 测试文件中无需 import  conftest, pytest 会自动搜索同级目录中的 conftest.py 文件

  2. 需要获得返回参数的时候,不可使用 @pytest.mark.usefixtures(),需要直接传参 ,因为fixture中返回的数据默认在fixture名字里面存储

https://i-blog.csdnimg.cn/blog_migrate/6cf03be45552c772e9ccb83e37ab8e5e.png

pyte st 的标签的使用 比如 @pytest.mark.demo :

Pytest:conftest.py在登录接口上的应用

注意: 登录接口: headers 中需要传参qtoken的值 :  ‘qtoken’:‘VE9N35WLpFOYrEpOnrXC457xO8U2’

pytest 的 多进程运行cases  以及  重试运行cases ​

py test 在命令行(terminal)运行:

pytest  test_skip.py          #运行test_skip.py文件

pytest -v test_skip.py            #查看详细的运行信息

pytest -m demo testCase/test_Search.py    # 运行 被标记为demo的 用例,比如说有的用例未完成,则可标记为finished,unfinished ,来方便调试

pytest使用命令行来运行 测试用例:

pytest  allure的教程

pytest  allure的安装教程

安装allure报错提示JAVA_HOME is set to an invalid directory解决办法:

对应的安装 java jdk的教程:

安装jdk出现could not open ‘D:\software\JAVA\lib\amd64\jvm.cfg’ 的解决办法

allure 的用例描述使用 :

生成allure报告:

方法1:

if name == ‘main’:

pytest.main([’-s’, ‘-q’, ‘–alluredir’, ‘./allure’])

os.system(‘allure -c ./allure’)

os.system(‘allure serve ./allure-report’)

方法2:

pytest  -n auto –alluredir=yourdir                                 #  -n auto:可以自动检测到系统的CPU核数 使用分布式运行用例  ;   –alluredir=yourdir    #只需添加 –alluredir 选项,并提供指向应存储结果的文件夹的路径   例如:pytest -n auto –alluredir .\report    或者 pytest -n auto –alluredir=report , report是你要放置的文件夹名称

allure serve report       #report 是已生成json , txt 数据的文件夹

方法3:

pytest -sq –alluredir=report     # 执行 pytest,指定 allure 结果目录为report文件夹

allure generate -c -o ./allure-report    report #根据report文件夹中的文件, 生成 allure 的 html 报告 放在 allure-report文件夹下

allure open ./allure-report     # 打开 allure 报告

https://i-blog.csdnimg.cn/blog_migrate/de5d7b1d6a3a1a61cf41050bdbb7e373.png

清除之前生成的json,txt文件,生成新的文件

pytest test_2.py  –alluredir=. /report   –clean-alluredir   #report为存放json,txt文件的文件夹 , 注意clean 后面接的是 - ,不是=

allure命令行

yaml文件:

yaml文件的使用

yaml 校验器