pytest系列教程——1、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.

官网地址

中文翻译地址

1、安装

pip install pytest

2、快速上手

新建test.py文件:

#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'])

运行结果:

============================= 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’

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软件测试技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值