前言:最近熬夜太多,老是失眠,那就努力更新博客,让大家多多学习,自己也梳理知识点

1、pytest的安装(pytest不允许测试类出现初始化方法,不然整个类就不会被当成测试类

pip install pytest

安装pytest不用pytest_pytest


2、查看pytest版本号

pytest -V

安装pytest不用pytest_python_02


3、pytestkuk框架的简介

安装pytest不用pytest_jenkins_03


4、pip install pytest-sugar(对运行过程进行界面美化)

pip install pytest-rerunfailures(重新运行出错的测试用例)

pip install pytest-xdist(多任务同时并发的执行测试用例)

pip install pytest-assume(添加断言,同一条测试用例每一条的断言都会反馈出来)

pip install pytest-html(生成美观的HTML测试报告)

安装pytest不用pytest_单元测试_04


5、测试用例的识别和运行

安装pytest不用pytest_python_05


6、命令栏详细的使用介绍

安装pytest不用pytest_jenkins_06


pytest 文件名(运行该文件下以test命名的所有测试用例)

安装pytest不用pytest_单元测试_07


pytest -v -s 文件名(会输出详细信息)

安装pytest不用pytest_安装pytest不用pytest_08


pytest --maxfail=2文件名(当运行错误的用例达到设置的次数就停止,2可以改为其他数字)

安装pytest不用pytest_jenkins_09


7、pytest执行-失败重新运行

安装pytest不用pytest_jenkins_10


pytest -v -s --reruns 3 --reruns-delay 2 文件名(失败用例重新运行3次,间隔2秒)

安装pytest不用pytest_pytest_11


8、pytest执行-多条断言有失败也都运行

安装pytest不用pytest_安装pytest不用pytest_12


pytest data1.py::TestDemo::test_four(运行效果如下图)

安装pytest不用pytest_单元测试_13


安装pytest不用pytest_pytest_14


9、使用pycharm运行pytest

安装pytest不用pytest_pytest_15


安装pytest不用pytest_python_16


10、pytest框架结构

安装pytest不用pytest_python_17


代码例子:

#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @File : data2.py
# @Time : 2020/3/28 15:49 
# @Author : 李文良

import pytest

def setup_module():
    print("这是一个setup_module方法")

def teardown_module():
    print("这是一个teardown_module方法")

def setup_function():
    print("setup_function")

def teardown_function():
    print("teardown_function")

def test_login():
    print("这是一个外部的方法")

class TestDemo():

    def setup_class(self):
        print("setup_class")

    def setup_method(self):
        print("setup_method")

    def setup(self):
        print("setup")

    def teardown_class(self):
        print("teardown_class")

    def teardown_method(self):
        print("teardown_method")

    def teardown(self):
        print("teardown")

    def test_one(self):
        print("请开始执行 test_one 方法")
        x = "this"
        assert "t" in x
        # pytest.assume("t" in x)
        # pytest.assume(1 == 2)

    def test_two(self):
        print("请开始执行 test_two 方法")
        x = "this"
        assert "h" in x

    def test_three(self):
        print("请开始执行 test_three 方法")
        x = "this"
        assert "i" in x

    def test_four(self):
        print("请开始执行 test_four 方法")
        x = "this"
        assert "i" in x
        # pytest.assume("1" in x)
        # pytest.assume("1" not in x)
        # pytest.assume("s" in x)
        # pytest.assume("s" not in x)

    def test_five(self):
        print("请开始执行 test_five 方法")
        x = "this"
        assert "s" in x

if __name__ == "__main__":
    pytest.main("-v -s")
    # pytest.main(['-v','-s'])

执行结果:

安装pytest不用pytest_python_18


11、pytest-fixture的用法

安装pytest不用pytest_pytest_19


安装pytest不用pytest_单元测试_20


例子:

安装pytest不用pytest_python_21


安装pytest不用pytest_jenkins_22


12、公共模块conftest的应用

安装pytest不用pytest_jenkins_23


安装pytest不用pytest_python_24


13、yield的应用,用来进行数据回收

安装pytest不用pytest_安装pytest不用pytest_25


例子:scope="module"是可以修改的,默认function

安装pytest不用pytest_安装pytest不用pytest_26


14、不想原有测试方法有任何改动,或全部都自动实现自动应用,我们使用fixture中参数autouse=Ture实现

安装pytest不用pytest_jenkins_27


autouse=Ture默认是False,可以自己更改成Ture

安装pytest不用pytest_安装pytest不用pytest_28


安装pytest不用pytest_安装pytest不用pytest_29


15、fixture带参数传递

安装pytest不用pytest_pytest_30


安装pytest不用pytest_python_31


fixture固件函数需要传参,如果@pytest.fixture()里面没有传参,可以通过@pytest.mark.parametrize()给固件函数传参,即indirect=True。并且固件函数的返回值作为Test函数的参数,不写的话传参的函数是不会调用的

安装pytest不用pytest_python_32


16、某些场景跳过个别用例执行,这时我们需要用到@pytest.mark.skip和@pytest.mark.xfail

安装pytest不用pytest_安装pytest不用pytest_33


安装pytest不用pytest_jenkins_34


17、对用例进行标记

安装pytest不用pytest_pytest_35


例子:

安装pytest不用pytest_安装pytest不用pytest_36


去除标签警告:

安装pytest不用pytest_安装pytest不用pytest_37


18、多线程并行与分布式执行用例

安装pytest不用pytest_jenkins_38


19、生成htm报告

安装pytest不用pytest_jenkins_39


待更新,营养跟不上了