官网:https://docs.pytest.org/en/6.2.x/

安装pytest

  1. pip install pytest

基本使用

test_sample.py

  1. """
  2. 自动化测试用例
  3. 文件名 test_ 开头
  4. """
  5. # 定义普通函数
  6. def inc(x):
  7. return x+1
  8. # pytest在运行的时候会自动的将 test_ 开头的函数当作自动化测试用例来执行
  9. def test_answer():
  10. assert inc(x=3) == 10

在命令行中执行 命令 pytest

  1. (venv) C:\Users\zengy\PycharmProjects\apptesting>pytest
  2. ========================================================== test session starts ==========================================================
  3. platform win32 -- Python 3.9.1, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
  4. rootdir: C:\Users\zengy\PycharmProjects\apptesting
  5. collected 1 item
  6. test_sample.py F [100%]
  7. =============================================================== FAILURES ================================================================
  8. ______________________________________________________________ test_answer ______________________________________________________________
  9. def test_answer():
  10. > assert inc(x=3) == 10
  11. E assert 4 == 10
  12. E + where 4 = inc(x=3)
  13. test_sample.py:11: AssertionError
  14. ======================================================== short test summary info ========================================================
  15. FAILED test_sample.py::test_answer - assert 4 == 10
  16. =========================================================== 1 failed in 0.11s ===========================================================

pytest运行

当在命令行执行pytest 命令的时候,运行对应在项目根目录下的 venv\Scripts\pytest.exe 这个文件
image.png
运行过程中, pytest会自动搜索当前运行目录下所有的以 test_ 开头的文件或者是目录, 将这些以 test 开始的文件或目录当作自动化测试用例来执行, 执行时 会将文件中定义的 以test开头的函数 作为自动化测试用例执行。

写测试用例的时候,要遵循一定的规则:

  1. 所有的测试用例都放在 testcases 目录
  2. 所有的测试用例文件名以 test_ 开头

pytest的脚手架

在执行自动化测试用例的时候,执行的先后顺序。

这里有
testcases/test_users/test_login.py

  1. def test_user_login_success():
  2. """
  3. 测试用户登录成功
  4. :return:
  5. """
  6. print('测试用户登录成功')
  7. assert True
  8. def test_user_login_failed():
  9. """
  10. 测试用户登录失败
  11. :return:
  12. """
  13. print('测试用户登录失败')
  14. assert True

testcases/test_users/test_register.py

  1. def test_user_register_success():
  2. """
  3. 测试用户注册成功
  4. :return:
  5. """
  6. print("测试用户注册成功")
  7. assert True
  8. def test_user_register_failed():
  9. """
  10. 测试用户注册失败
  11. :return:
  12. """
  13. print("测试用户注册失败")
  14. assert True

testcases/test_bussiness.py

  1. def test_business_success():
  2. """
  3. 测业务成功
  4. :return:
  5. """
  6. print('测业务成功')
  7. assert True
  8. def test_business_failed():
  9. """
  10. 测试用户登录失败
  11. :return:
  12. """
  13. print('测业务成功')
  14. assert True

在命令中执行

  1. pytest -s -v
  • -s 打印出自动化测试用例中使用 print() 的输出
  • -v 显示出详细的执行日志 会将每个执行的函数名显示出来
  1. (venv) C:\Users\zengy\PycharmProjects\apptesting>pytest -s -v
  2. ========================================================== test session starts ==========================================================
  3. platform win32 -- Python 3.9.1, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- c:\users\zengy\pycharmprojects\apptesting\venv\scripts\python.ex
  4. e
  5. cachedir: .pytest_cache
  6. rootdir: C:\Users\zengy\PycharmProjects\apptesting
  7. collected 6 items
  8. testcases/test_bussiness.py::test_business_success 测业务成功
  9. PASSED
  10. testcases/test_bussiness.py::test_business_failed 测业务成功
  11. PASSED
  12. testcases/test_users/test_login.py::test_user_login_success 测试用户登录成功
  13. PASSED
  14. testcases/test_users/test_login.py::test_user_login_failed 测试用户登录失败
  15. PASSED
  16. testcases/test_users/test_register.py::test_user_register_success 测试用户注册成功
  17. PASSED
  18. testcases/test_users/test_register.py::test_user_register_failed 测试用户注册失败
  19. PASSED

设置conftest.py

项目的根目录下创建 conftest.py 文件,是pytest的配置文件。文件会自动被pytest识别。

  1. import pytest
  2. @pytest.fixture(scope="session",autouse=True)
  3. def session():
  4. print("在所有的测试用例执行之前执行")
  5. yield
  6. print("执行所有的用例执行之后的操作")
  7. @pytest.fixture(scope='module',autouse=True)
  8. def module():
  9. print('每个py文件运行之前的操作')
  10. yield
  11. print('每个py文件运行之后的操作')
  12. @pytest.fixture(scope='class',autouse=True)
  13. def object():
  14. print('每个class类运行之前')
  15. yield
  16. print('每个class类运行之后的操作')
  17. @pytest.fixture(scope='function',autouse=True)
  18. def func():
  19. print('每个函数执行之前')
  20. yield
  21. print('每个函数执行之后')

pytest 支持4个级别的执行顺序。

  • session 所有的测试用例之执行先后
  • module 整个py文件执行先后
  • class 每一个class 测试类执行之前和之后的操作
  • function 每一个函数执行之前之后

在使用pytest脚手架的时候, 主要使用到两个参数

  • scope= “” 表示区域
  • autouse = 是否自动使用。

pytest的数据驱动

在做手工功能测试的时候,比如测试登录操作,除了要测试正常的业务场景之外,还要对异常场景进行测试,比如使用错误的用户名或者错误的密码登录,登录应该失败。
错误的信息可以有不同的组合,测试这一类业务的时候,操作步骤都一样,只是操作的数据不一样。

在自动化测试过程中,可以使用pytest中参数化的功能实现 数据驱动。
testcases/test_ddt.py

  1. import pytest
  2. @pytest.mark.parametrize('test_input,expected',[('2+2',4),('2+3',5),('3+3',6),('2*2',4)])
  3. def test_eval(test_input,expected):
  4. print(test_input,expected)
  5. assert eval(test_input) == expected

使用pytest参数化功能

  • 'test_input,expected' 定义为test函数中传入参数名
  • [('2+2',4),('2+3',5),('3+3',6),('2*2',4)] 测试数据,可以根据自己的测试业务场景添加不同的数据
  • test_input,expected 函数中使用的参数为上述定义

运行,如果要单独执行某个文件, 后面跟上相对路径即可。

  1. pytest testcases\test_ddt.py -s -v

执行结果,因为上面有4个数据,所以运行时有4个测试用例

  1. (venv) C:\Users\zengy\PycharmProjects\apptesting>pytest testcases\test_ddt.py -s -v
  2. ========================================================== test session starts ==========================================================
  3. platform win32 -- Python 3.9.1, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- c:\users\zengy\pycharmprojects\apptesting\venv\scripts\python.ex
  4. e
  5. cachedir: .pytest_cache
  6. rootdir: C:\Users\zengy\PycharmProjects\apptesting
  7. collected 4 items
  8. testcases/test_ddt.py::test_eval[2+2-4] 在所有的测试用例执行之前执行
  9. 每个py文件运行之前的操作
  10. 每个class类运行之前
  11. 每个函数执行之前
  12. 2+2 4
  13. PASSED每个函数执行之后
  14. 每个class类运行之后的操作
  15. testcases/test_ddt.py::test_eval[2+3-5] 每个class类运行之前
  16. 每个函数执行之前
  17. 2+3 5
  18. PASSED每个函数执行之后
  19. 每个class类运行之后的操作
  20. testcases/test_ddt.py::test_eval[3+3-6] 每个class类运行之前
  21. 每个函数执行之前
  22. 3+3 6
  23. PASSED每个函数执行之后
  24. 每个class类运行之后的操作
  25. testcases/test_ddt.py::test_eval[2*2-4] 每个class类运行之前
  26. 每个函数执行之前
  27. 2*2 4
  28. PASSED每个函数执行之后
  29. 每个class类运行之后的操作
  30. 每个py文件运行之后的操作
  31. 执行所有的用例执行之后的操作
  32. =========================================================== 4 passed in 0.04s ===========================================================