前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pytest系列教程——1、pytest快速入门

pytest系列教程——1、pytest快速入门

作者头像
胡八万
发布2022-05-16 12:01:43
2K0
发布2022-05-16 12:01:43
举报
文章被收录于专栏:软件测试技术软件测试技术

写在前面

pytest零零散散接触的不少,总是感觉缺少系统的学习。后续,我会专门整理pytest系列教程,内容绝对充实。希望大家有兴趣可以跟着我一起敲其中的案例代码,共同成长~

pytest快速入门

简介

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

官网地址 https://docs.pytest.org/en/7.1.x/

中文翻译地址 https://learning-pytest.readthedocs.io/zh/latest/doc/intro/getting-started.html

1、安装

pip install pytest

2、快速上手

新建test.py文件:

代码语言:javascript
复制
#test.py
import pytest

def inc(x):
    return x+1

def testAnswerWrong():
    assert inc(3) == 5

def testAnswerRight():
    assert inc(3) == 4

if __name__ =="__main__":
    pytest.main(['test.py'])

运行结果:

代码语言:javascript
复制
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.2.5, py-1.10.0, pluggy-0.13.1
plugins: allure-pytest-2.9.42, Faker-8.4.0, forked-1.3.0, html-2.0.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.0, xdist-2.3.0, seleniumbase-1.63.11
collected 2 items

test.py F.                                                               [100%]

================================== FAILURES ===================================
_______________________________ testAnswerWrong _______________________________

    def testAnswerWrong():
>       assert inc(3) == 5
E       assert 4 == 5
E        +  where 4 = inc(3)

test.py:11: AssertionError
=========================== short test summary info ===========================
FAILED test.py::testAnswerWrong - assert 4 == 5
========================= 1 failed, 1 passed in 0.21s =========================

***Repl Closed***

注解

  • pytest 使用 . 标识测试成功(PASSED)
  • pytest 使用 F 标识测试失败(FAILED)

3、编写原则

用Pytest写用例时候,一定要按照下面的规则去写,否则不符合规则的测试用例是不会执行的

  • 文件名以 test_*.py 文件和 *_test.py
  • 以test_开头的函数
  • 以Test开头的类,不能包含__init__方法
  • 以test_开头的类里面的方法
  • 所有的包 package 必须要有__init__.py文件

4、运行方式

以前文test.py测试文件举例

(1) 主函数方式运行:

指定运行文件:pytest.main(['-s','test.py'])

注意:如果py文件是以test_开头或者以_test结尾则可以使用pytest.main()运行。因为pytest.main()会运行当前目录下所有以test_开头或者以_test结尾的文件。

(2) 命令行方式运行

点开Pycharm左下角,在Terminal打开当面目录下的命令行窗口,或者windows用户也可直接打开CMD命令行窗口,输入命令执行:pytest test.py

注意:如果py文件是以test_开头或者以_test结尾则可以使用pytest命令运行,因为pytest会运行当前目录下所有以test_开头或者以_test结尾的文件。

5、运行参数说明

  • -s显示打印内容

如:pytest test.py -s

等价于:pytest.main(['-s','test.py'])

  • ::指定测试用例运行

运行函数:如:pytest pytest-demo.py::test_01

等价于:pytest.main(['-s','test.py::test01'])

运行类中方法:如:pytest test.py::TestCase::test_03

等价于:pytest.main(['-s', 'test.py::TestCase::test_03'])

  • –html=路径/report.html生成xml/html格式测试报告(需要先安装pytest-html)

如:pytest pytest-demp.py --html-./report.html

等价于:pytest.main(['-s','test.py','–html=./report.html'])

  • –maxfail=1出现1个失败就终止测试

如:pytest pytest-demo.py --maxfail=1

等价于:pytest.main([’-s’,'pytest-demo.py’,’–maxfail=1’])

  • -npytest-xdist多线程运行(需要先安装pytest-xdist)

如:pytest test.py -n 2

等价于:pytest.main(['-s','pytest-demo.py','-n=2'])

  • -x 遇到错误时停止测试 如:pytest test.py -x
  • --maxfail=num,当用例错误个数达到指定数量时,停止测试 如:pytest test.py —maxfail=3
  • -k 匹配用例名称 ,执行测试用例名称包含关键字的所有用例 如:pytest test.py -k 关键字 排除某些用例 如:pytest test.py -k 'not 关键字' 或者关系的某些用例 如:pytest test.py -k '关键字A or 关键字B'
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 软件测试技术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 写在前面
  • pytest零零散散接触的不少,总是感觉缺少系统的学习。后续,我会专门整理pytest系列教程,内容绝对充实。希望大家有兴趣可以跟着我一起敲其中的案例代码,共同成长~
  • pytest快速入门
    • 简介
      • 1、安装
        • 2、快速上手
          • 3、编写原则
            • 4、运行方式
              • 5、运行参数说明
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档