Pytest框架实战--1--安装--断言assertpy--运行方式

Pytest教程

安装pytest

pip install pytest

创建一个以test开头的py文件(或者test结尾)

pytest会自动寻找以test开头或者结尾的py,在当前路径下面

test_01.py

# fun1为我们测试的函数:传一个参数自动+1
def fun1(x):
    """
    假如我们开发的时候不小心把1写成了0.1
    :param x:
    :return:
    """
    return x + 0.1


def test_fun1():
    assert fun1(10) == 11

我们在pycharm执行右击运行

image-20220705222201993

输出结果:

============================= test session starts =============================
collecting ... collected 1 item

test_01.py::test_fun1 FAILED                                             [100%]
test_01.py:10 (test_fun1)
10.1 != 11

Expected :11
Actual   :10.1
<Click to see difference>

def test_fun1():
>       assert fun1(10) == 11
E       assert 10.1 == 11
E        +  where 10.1 = fun1(10)

test_01.py:12: AssertionError

优化一下我们的断言

我们继续创建一个test_02.py

from assertpy import assert_that  # 记得pip install 一下


# fun1为我们测试的函数:传一个参数自动+1
def fun1(x):
    """
    假如我们开发的时候不小心把1写成了0.1
    :param x:
    :return:
    """
    return x + 0.1


def test_fun1():
    assert fun1(10) == 11
    
def test_fun2():
    assert_that(fun1(10)).is_equal_to(11)

注意:我们的测试用例名称千万不能重复

我们这里只看test_fun2的输出结果:

============================= test session starts =============================
collecting ... collected 1 item

test_02.py::test_fun2 FAILED                                             [100%]
test_02.py:13 (test_fun2)
def test_fun2():
>       assert_that(fun1(10)).is_equal_to(11)
E       AssertionError: Expected <10.1> to be equal to <11>, but was not.

test_02.py:15: AssertionError

我们继续在test_02.py文件中增加一个测试用例

def test_fun3():
    assert_that(fun1(10)).is_instance_of(int)

输出结果:

test_02.py::test_fun3 FAILED                                             [100%]
test_02.py:17 (test_fun3)
def test_fun3():
>       assert_that(fun1(10)).is_instance_of(int)
E       AssertionError: Expected <10.1:float> to be instance of class <int>, but was not.

test_02.py:19: AssertionError

至于为什么选择assertpy第三方断言库,在你写了很多断言之后你就会发现他是真的好用~

有关assertpy学习 我们可以先看看GitHub:assertpy,后续我们再专门说说它

常用运行用例的方式

  1. 直接在pycharm中右击运行所有用例

    image-20220705222201993

  2. pycharm中选择某一个用例运行(注意:左上角的配置)

    image-20220705224614237

  3. 在终端运行(PowerShell)

    pytest  .\test_02.py
    
  4. 使用python运行,这个需要在test.py中添加一下代码

    if __name__ == '__main__':
        pytest.main(['./test_03.py'])
    

    然后在终端执行:

    python .\test_03.py
    

重点讲解一下第四种运行的方式,因为我们以后的项目中都是这样运行的(因为我们的自动化代码需要在一个全新的机器上运行,这台机器可能都没有pip 没有pytest更可能没有pycharm).创建test_03.py

from assertpy import assert_that
import pytest


# fun1为我们测试的函数:传一个参数自动+1
def fun1(x):
    """
    假如我们开发的时候不小心把1写成了0.1
    :param x:
    :return:
    """
    return x + 0.1


def test_fun4():
    print('这是失败的用例,这个print会打印')
    assert_that(fun1(10)).is_equal_to(11)


def test_fun5():
    print('这是成功的用例,这个print不会打印')
    assert_that(fun1(10)).is_instance_of(float)


if __name__ == '__main__':
    pytest.main(['./test_02_01.py::test_fun5'])  # 这里注意不要直接写:pytest.main(['./']) 他会运行全部用例

打开终端输入:

python .\test_03.py

输出:

注意:这里没有我的print内容,然而我们的test_fun5中有print内容啊

今天就到这里,明天计划写一些关于在pytest中的print

posted @ 2022-07-07 23:56  Tarzen  阅读(198)  评论(0)    收藏  举报