首页 > 超详细的 pytest 教程【入门篇】
头像
牛客973033437号
发布于 2022-05-09 20:35
+ 关注

超详细的 pytest 教程【入门篇】


前言

关于自动化测试,这些年经历了太多的坑,有被动的坑,也有自己主动挖的坑,在这里做了一些总结。
主要思考总结下这些年来自动化测试过程中的一些基本的东西,例如何时进行自动化、如何自动化、或是怎么自动化我们的测试工作,接下来我们先对pytest展开一些讲解。
pytest到目前为止还没有翻译得比较好全面地使用文档,很多英文不太好的小伙伴,在学习时看英文文档还是很吃力。本来去年就计划写pytest详细地使用文档的,由于时间关系一直搁置,直到今天才开始写。本文是第一篇,主要介绍pytest的入门使用,后续会分篇针对pytest中的各个功能出详细的使用教程。

一、环境安装

pytest 是 python 中的第三方库,使用之前需要先安装,在命令行中运行以下安装命令 :

	
pip insatll pytest
检查安装是否成功以及安装的版本,命令行命令如下:

	
pytest --version
执行上述命令,能够输出版本信息,那就说明安装成功啦。

二、用例编写

当我们通过 pytest 执行用例时,pytest 会自动递归遍历执行路径下所有的目录,根据 pytest 中默认用例的识别的规则,自动收集测试用例。所有在使用 pytest 编写测试用例之前,我们首先需要了解一下 pytest 收集用例时默认的用例识别规则。

1、默认的用例识别的规则

  • 1、用例文件:所有文件名为 开头 或者 开头的文件会被识别为用例文件。test__test
  • 2:用例类,测试文件中没有每个 Test 开头的类型就是一个测试用例类。
  • 3、测试用例:测试类中每个 test 开头的方法就是一条测试用例,测试文件中每个 test 开头的函数也是一条测试用例,
备注:上述默认的用例查找规则,可在 pytest 的配置文件进行修改(后续章节会详细介绍配置文件的使用) 另外 pytest 兼容 unittest,以 unittest 的用例编写规范写的用例,pytest 都能够识别出来
通过了解上述 pytest 中用例识别的规则,可以知道 pytest 用例编写,能使用函数的形式,也能使用类的形式,那么接下来就分别给大家介绍一下这两种方式编写用例。

2、函数形式编写用例

规则:用例方法名以 test 开头即可

	
# \testcases\test_demo1.py def test_demo(): assert 100 == 100
使用命令 就可以执行测试函数,输出结果如下:pytest

	
C:\testcases>pytest ======================test session starts ====================== platform win32 -- Python 3.7.3, pytest-5.4.2, py-1.8.0, pluggy-0.13.0 rootdir: C:\testcases plugins: testreport-1.1.2 collected 1 item test_demo1.py . [100%] ====================== 1 passed in 0.26s ======================

3、以类的形式编写用例

规则: 测试类命名以 Test 开头,用例方法以 test 开头

	
# test_demo2.py class TestDome: def test_demo1(self): assert 11 == 11 def test_demo(self): assert 22 == 21
命令 运行上述用例,结果如下:pytest

	
====================== test session starts ====================== platform win32 -- Python 3.7.3, pytest-5.4.2, py-1.8.0, pluggy-0.13.0 rootdir: C:\testcases plugins: testreport-1.1.2 collected 2 items test_demo1.py .F [100%] ====================== FAILURES ====================== ___________ TestDome.test_demo ____________ self = <test_demo1.TestDome object at 0x0445F450> def test_demo(self): > assert 22 == 21 E assert 22 == 21 test_demo1.py:25: AssertionError ====================== short test summary info ======================= FAILED test_demo1.py::TestDome::test_demo - assert 22 == 21 ====================== 1 failed, 1 passed in 0.53s ======================
上面的运行结果可以看出来,一条用例执行通过,一条执行失败

三、执行测试

在上面我们使用的是 pytest 这个命令去执行测试用例。关于 pytest 执行测试,有两种方式,一种是命令行通过 pytest 这个命令执行,另外在代码中也可以通过 这个方法来执行测试。接下来就和大家详细地介绍一下 pytest 执行测试的方式和常用的参数pytest.main()

1、执行参数

测试用例

	
# 测试用例 class TestDome: def test_demo1(self): print('----测试用例执行-----------') assert 11 == 11
参数 : 显示测试的详细参数信息-v

	
C:\testcases>pytest -v ========================== test session starts ========================== platform win32 -- Python 3.7.3, pytest-5.4.2, py-1.8.0, pluggy-0.13.0 cachedir: .pytest_cache rootdir: C:\git_project\pytest-report-me-main\testcases plugins: testreport-1.1.2 collected 1 item test_demo1.py::TestDome::test_demo1 PASSED [100%] ========================== 1 passed in 0.27s ==========================
参数 : 显示测试执行的输出信息-s

	
C:\testcases>pytest -s =========================== test session starts =========================== platform win32 -- Python 3.7.3, pytest-5.4.2, py-1.8.0, pluggy-0.13.0 rootdir: C:\testcases plugins: testreport-1.1.2 collected 1 item test_demo1.py::TestDome::test_demo1 ----测试用例执行---输出1-------- ----测试用例执行---输出2-------- PASSED =========================== 1 passed in 0.28s ===========================

2、pytest.main 执行的参数传递

pytest.main 方法是执行测试参数传递方式:
所以把参数放在列表中,每个参数就是列表中的一个元素

	
pytest.main(['-v','-s'])
详细的参数可以使用命令 查看pytest -h

3、指定执行的测试目录

命令pytest 测试目录路径

	
pytest testcase/
pytest 会执行指定目录路径下所有的测试用例

4、指定执行的测试文件

命令pytest 测试文件路径

	
pytest testcase/test_demo1.py
pytest 会执行指定测试文件中所有的测试用例

5、指定执行的测试类

命令pytest 测试文件::测试类

	
pytest testcase/test_demo1.py::TestClass
pytest 会执行指定测试类里面所有的测试用例

6、指定执行的测试用例

命令pytest 测试文件::测试类::测试方法

	
pytest testcase/test_demo1.py::TestClass::test_method
pytest 会执行指定的测试方法
基本的入门就给大家介绍到这里了

写在最后

最后再唠唠一句,如果想以测试为长期发展职业目标,是需要时刻保持学习的,要使自己具备竞争力,无论你现在工作几年,只要行动起来,你就已经占优势了,好啦就到这里了,祝大家2022年能升职加薪,拿到心仪公司的offer,事事顺遂。
这份软件测试学习资源整理起来也不容易,希望大家帮忙「点赞」「收藏」,咱不做白嫖党!!

全部评论

(1) 回帖
加载中...
话题 回帖