美文网首页
Pytest测试框架

Pytest测试框架

作者: info_gu | 来源:发表于2020-09-09 13:08 被阅读0次

    理论:


    image.png

    测试用例的识别与运行:


    image.png

    安装:pip install pytest

    https://docs.pytest.org/en/stable/

    创建文件(直接运行是没有输出的因为使用的默认的unittest测试运行):


    image.png

    如何修改:


    image.png

    再次运行该文件:


    image.png

    如果需要再次使用python解释器则,需要添加一个python解释器


    image.png

    添加完后运行则使用python解释器:


    image.png

    pytest运行方式:

    • 第一种使用Python解释器:
    import pytest
    
    
    def inc(x):
        return x + 1
    
    
    def test_answer():
        assert inc(3) == 5
    
    if __name__ == '__main__':
        pytest.main(['test_demo01.py'])
    
    • 第二种使用pytest解释器:
      在tools里面设置后,直接运行

    • 第三种:控制台pytest 文件名 -v

    image.png

    c参数:

    • -k (test_b是方法,-k主要是执行指定的测试用例)


      image.png
    image.png

    pytest参数化:

    外部文件或者外部数据

    第一种外部数据:

    • 未参数化前:
    def test_answer():
        assert inc(3) == 5
    
    • 参数化后
    import pytest
    
    
    def inc(x):
        return x + 1
    
    @pytest.mark.parametrize('a,b',[
        (2,2),
        (3,4)
    ])
    def test_answer(a,b):
        assert inc(a) == b
    
    if __name__ == '__main__':
        pytest.main(['test_demo01.py'])
    

    pytest如何setup和teardown

    fixtrue:

    • test_login是需要在test_inc之前执行所以声明fixtrue后,传参数到test_inc中即可
    import pytest
    @pytest.fixture()
    def test_login():
        print("登录成功")
    
    def test_inc(test_login):
        print("test fuc")
    
    • 被fixtrue的方法带返回值
    import pytest
    @pytest.fixture()
    def test_login():
        return  "登录成功"
    
    def test_inc(test_login):
        print(test_login)
        print("test fuc")
    

    pytest命令参数详解:

    image.png

    相关文章

      网友评论

          本文标题:Pytest测试框架

          本文链接:https://www.haomeiwen.com/subject/sqreektx.html