【单元测试框架】pytest学习(一)框架介绍

pytest 优点: 

  更加灵活、容易上手

  支持参数化

  测试用例的skip和xfail,自动失败重试等处理

  能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium、appnium等自动化测试、接口自动化测试(python+requests)

  支持很多第三方插件、可以自动以扩展;pytest-allure报告、pytest-xdist(多cpu分发)

  可以很好地和jenkins集合

用例的识别与运行:

  测试文件:  test_*.py  *_test.py

  用例识别:Test*类包含的所有test_*的方法【注:类不能带有__init__方法,如果加了,会认为它是一个有功能的类,不是一个测试类】;不在class中的所有test_*方法

  pytest也可以执行unittst框架写的用例和方法

安装和更新:

  pip install pytest

  pip install -U pytest

  pytest --version

执行:

  终端执行命令 :pytest   #会把当前目录下所有test_*文件进行执行,使用的是pytest解释器 

  python 解释器运行 python test_iii.py---【pytest.main(['test_pytestdemo.py']);pytest.main(['test_pytestdemo.py::TestDemo1121',"-v"]);pytest.main(['test_pytestdemo.py::TestDemo1121::test_demo112103',"-v"])】

  pytest --help

  -v:增加详细信息,执行文件::执行方法     用例执行结果

  -k:执行匹配的测试用例,是表达式,不是完成的用例名字;pytest.main(['test_pytestdemo.py',"-k","test_demo112101 or test_demo112103","-v"])

参数化:@pytest.mark.parametrize('a,b',[(2,3),(1,1)])

装饰器:@pytest.fixture()  ,当执行用例时需要这个方法可以直接传进去

例:

import pytest
@pytest.fixture()
def basefix():
    print("公用方法")
@pytest.mark.parametrize('a,b',[(2,3),(1,1)])
def test_demo112101(a,b,basefix):
    print("test_demo112101")
    assert a == b

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

 

import pytest
@pytest.mark.parametrize('a,b',[(2,3),(1,1)])
def test_demo112101(a,b):
    print("test_demo112101")
    assert a == b

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

============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- E:\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: E:pythonProject\Base_python
collecting ... collected 2 items

test_pytestdemo.py::test_demo112101[2-3] FAILED [ 50%]
test_pytestdemo.py::test_demo112101[1-1] PASSED [100%]

================================== FAILURES ===================================
____________________________ test_demo112101[2-3] _____________________________

a = 2, b = 3

@pytest.mark.parametrize('a,b',[(2,3),(1,1)])
def test_demo112101(a,b):
print("test_demo112101")
> assert a == b
E assert 2 == 3
E +2
E -3

test_pytestdemo.py:5: AssertionError
---------------------------- Captured stdout call -----------------------------
test_demo112101
=========================== short test summary info ===========================
FAILED test_pytestdemo.py::test_demo112101[2-3] - assert 2 == 3
========================= 1 failed, 1 passed in 0.05s =========================

import pytest
def test_demo112101():
    print("test_demo112101")
if __name__ == "__main__":
    pytest.main(['test_pytestdemo.py'])

============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: E:\Mitena\pythonProject\Base_python
collected 1 item

test_pytestdemo.py . [100%]

============================== 1 passed in 0.02s ==============================

if __name__ == "__main__":
    pytest.main(['test_pytestdemo.py',"-v"])
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- E:\Mitena\pythonProject\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: E:\Mitena\pythonProject\Base_python
collecting ... collected 1 item

test_pytestdemo.py::test_demo112101 PASSED                               [100%]

============================== 1 passed in 0.01s ==============================
import pytest
def test_demo112101():
    print("test_demo112101")
class TestDemo1121:
    def test_demo112102(self):
        print("test_demo112102")
    def test_demo112103(self):
        print("test_demo112102")
if __name__ == "__main__":
    pytest.main(['test_pytestdemo.py::TestDemo1121',"-v"])
    pytest.main(['test_pytestdemo.py::TestDemo1121::test_demo112103',"-v"])
import pytest
def test_demo112101():
    print("test_demo112101")
class TestDemo1121:
    def test_demo112102(self):
        print("test_demo112102")
    def test_demo112103(self):
        print("test_demo112102")
if __name__ == "__main__":
    pytest.main(['test_pytestdemo.py',"-k","test_demo112102","-v"])

============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- E:\Mitena\pythonProject\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: E:\Mitena\pythonProject\Base_python
collecting ... collected 3 items / 2 deselected / 1 selected

test_pytestdemo.py::TestDemo1121::test_demo112102 PASSED                 [100%]

======================= 1 passed, 2 deselected in 0.02s =======================
import pytest
def test_demo112101():
    print("test_demo112101")
class TestDemo1121:
    def test_demo112102(self):
        print("test_demo112102")
    def test_demo112103(self):
        print("test_demo112102")
    def test_demo1121031(self):
        print("test_demo112102")
    def test_demo112104(self):
        print("test_demo112102")
if __name__ == "__main__":
    pytest.main(['test_pytestdemo.py',"-k","test_demo112101 or test_demo112103","-v"])

============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- E:\Mitena\pythonProject\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: E:\Mitena\pythonProject\Base_python
collecting ... collected 5 items / 2 deselected / 3 selected

test_pytestdemo.py::test_demo112101 PASSED                               [ 33%]
test_pytestdemo.py::TestDemo1121::test_demo112103 PASSED                 [ 66%]
test_pytestdemo.py::TestDemo1121::test_demo1121031 PASSED                [100%]

======================= 3 passed, 2 deselected in 0.03s =======================

 

 

 

编辑pytest解释器

【单元测试框架】pytest学习(一)框架介绍

 

上一篇:VirtualBox安装Minix3(转载)


下一篇:郑乐意