【python自动化】pytest系列(上)

【python自动化】pytest系列(上)

本系列一共会有四篇文章,上篇,中篇,下篇,终篇。

「本章知识点」


  • 【python自动化】pytest系列(上)

  • 1、Pytest安装

  • 2、Pytest注意事项

    • Pytest Exit Code 含义清单

    • 常见用法

  • 3、Pytest快速入门

    • (1)方法执行测试

    • (2)类执行测试

    • (3)文件执行测试

    • (4)用例的执行顺序

  • 4、下节内容预告


官网地址:https://docs.pytest.org/en/latest/contents.html

Pytest官方介绍:

The pytest framework makes it easy to write small, readable tests, and can scale to support complex functional testing for applications and libraries.

Pytest安装要求:Python 3.7+

1、Pytest安装

pip install pytest

「验证安装」

pytest --version #会显示当前已安装的版本

2、Pytest注意事项

1、所有的单测文件名都需要满足test_*.py格式或*_test.py格式。

2、在单测文件中,测试类以Test开头,并且不能带有 「init」 方法(注意:定义class时,需要以T开头,不然pytest是不会去运行该class的)

3、在单测类中,可以包含一个或多个test_开头的函数。

4、在执行pytest命令时,会自动从当前目录及子目录中寻找符合上述约束的测试函数来执行。

在pycharm运行,默认是使用Autodetect方式进行测试,这里我们可以更改为pytest

settings –> Tools –>  Python Integrated Tools –> Testing –> Default test runne

【python自动化】pytest系列(上)


Pytest Exit Code 含义清单

Exit code 0 所有用例执行完毕,全部通过
Exit code 1 所有用例执行完毕,存在Failed的测试用例
Exit code 2 用户中断了测试的执行
Exit code 3 测试执行过程发生了内部错误
Exit code 4 pytest 命令行使用错误
Exit code 5 未采集到可用测试用例文件

常见用法

  • 运行测试:在项目根目录下执行”pytest”命令即可运行所有测试用例。
  • 选择测试:使用”-k”参数可以根据用例名称进行过滤,例如”pytest -k test_login”只会运行名称包含”test_login”的测试用例。
  • 生成报告:使用”-r”参数可以选择输出报告的级别,例如”-rA”会输出所有测试用例的结果,”-rP”会输出测试用例的进度。
  • 覆盖率检查:使用”–cov”参数可以生成测试用例的覆盖率报告,例如”pytest –cov=myapp”会生成myapp代码库的覆盖率报告。

3、Pytest快速入门

(1)方法执行测试

新建test_a.py文件,编写如下代码:

def test_001():
    print("pytest执行的第一个测试用例")

「右键执行」,启动项显示「Pytest」

【python自动化】pytest系列(上)


「运行结果如下:」

【python自动化】pytest系列(上)


(2)类执行测试

「新建test_b.py」文件,编写如下代码:

class TestB():
    def test_b_001(self):
        print("我是TestB下的test_b_001")

    def test_b_002(self):
        print("我是TestB下的test_b_002")

执行结果如下:

Testing started at 11:19 ...
Launching pytest with arguments D:/L_Learning/MyLearningCode/ApiTestProject/test_b.py --no-header --no-summary -q in D:L_LearningMyLearningCodeApiTestProject

============================= test session starts =============================
collecting ... collected 2 items

test_b.py::TestB::test_b_001 PASSED                                      [ 50%]我是TestB下的test_b_001

test_b.py::TestB::test_b_002 PASSED                                      [100%]我是TestB下的test_b_002


============================== 2 passed in 0.01s ==============================

Process finished with exit code 0

「由上可知,类只要是符合Test开头,pytest都会收集下来并执行。」

(3)文件执行测试

「新建main.py文件」,编写代码如下:

import pytest

pytest.main()

当前项目目录如下:

【python自动化】pytest系列(上)


「在main.py文件里面右键运行」,执行结果如下:

============================= test session starts =============================
platform win32 -- Python 3.8.8, pytest-7.2.1, pluggy-1.0.0
rootdir: D:L_LearningMyLearningCodeApiTestProject
collected 3 items

test_a.py .                                                              [ 33%]
test_b.py ..                                                             [100%]

============================== 3 passed in 0.01s ==============================

Process finished with exit code 0

「由上可知,文件只要是符合Test开头,pytest都会收集下来并执行。」

到此,你算是简单入门pytest的使用啦。

(4)用例的执行顺序

  • 文件是默认按字母顺序去执行的,按照ASCLL码排序(小写英文—>大写英文—>0-9数字)。
  • 文件内部的用例按照从上往下执行。
  • setup_module->setup_claas->setup_function->testcase->teardown_function->teardown_claas->teardown_module
  • 可以通过第三方插件pytest-ordering实现自定义用例执行顺序

4、下节内容预告

  • 断言assert
  • 命令行运行
  • pytest-allure报告
  • 相关插件

pytest教程系列之前录制了视频,不过本人普通话不是很好,就不放出来了。


原文始发于微信公众号(梦无矶测开实录):【python自动化】pytest系列(上)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/245793.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!