一、Pytest 框架入门

(使用规范、命令选项、测试来源、环境变量)

1.   使用规范:

文件名以test_*.py文件和*_test.py

以test_开头的函数

以Test开头的类

以test_开头的方法

所有的包pakege必须要有__init__.py文件


2.   执行用例规则:

1.执行某个目录下所有的用例

pytest 目录名/

2.执行某一个py文件下用例

pytest 脚本名称.py

3.-k 按关键字匹配

pytest -k "MyClass and not method"

4.按节点运行

每个收集的测试都分配了一个唯一的nodeid,它由模块文件名和后跟说明符组成来自参数化的类名,函数名和参数,由:: characters分隔。

运行.py模块里面的某个函数

pytest test_mod.py::test_func

运行.py模块里面,测试类里面的某个方法

pytest test_mod.py::TestClass::test_method

5.标记表达式

pytest -m slow

将运行用@pytest.mark.slow装饰器修饰的所有测试。后面章节会讲自定义标记mark的功能

-x 遇到错误时停止测试

pytest -x test_class.py

--maxfail=num

当用例错误个数达到指定数量时,停止测试

pytest --maxfail=3


3.在python代码中使用pytest执行测试

1.    直接调用

Pytest.main()

2.    传参调用

Pytest.main("-s", "mytest.py")


4.常用的命令行选项详解

1. --collect-only选项

使用--collect-only选项可以展示在给定的配置下哪些测试用例会被运行。此选项可以让你在执行测试前,非常方便地查看选中的用例是否符合预期。

(venv) D:\PyLib\CodemaoApiTest>pytest --collect-only

============================================================================================= test session starts ==============================================================================================

platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0

rootdir: D:\PyLib\CodemaoApiTest

plugins: allure-pytest-2.7.1, forked-1.0.2, html-1.21.1, metadata-1.8.0, xdist-1.29.0

collected 12 items                                                                                                                                                                                             

<Package D:\PyLib\CodemaoApiTest\tests>

  <Module test_ban_and_recover_login.py>

    <Function test_ban_login_one>

    <Function test_ban_and_recover_login_two>

  <Module test_ban_and_recover_publish.py>

    <Function test_ban_one>

    <Function test_ban_two>

  <Module test_ban_comment.py>

    <Function test_ban_one>

    <Function test_ban_two>

    <Function test_ban_three>

    <Function test_ban_four>

  <Module test_delete_comments.py>

    <Function test_delete_comments_one>

  <Module test_delete_works.py>

    <Function test_delete_works_one>

  <Module test_recover_comment.py>

    <Function test_recover_one>

    <Function test_recover_two>

========================================================================================= no tests ran in 0.27 seconds ========================================================================================


2.-k选项

-k选项允许你使用表达式指定希望运行的用例。如果某测试名是唯一的,或者多个测试名的前缀或后缀相同,那么可以使用表达式来快速定位。假设希望选中有关publish和comment的用例,那么

(venv) D:\PyLib\CodemaoApiTest>pytest -k "comment or publish" --collect-only

============================================================================================= test session starts ==============================================================================================

platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0

rootdir: D:\PyLib\CodemaoApiTest

plugins: allure-pytest-2.7.1, forked-1.0.2, html-1.21.1, metadata-1.8.0, xdist-1.29.0

collected 12 items / 3 deselected / 9 selected                                                                                                                                                                 

<Package D:\PyLib\CodemaoApiTest\tests>

  <Module test_ban_and_recover_publish.py>

    <Function test_ban_one>

    <Function test_ban_two>

  <Module test_ban_comment.py>

    <Function test_ban_one>

    <Function test_ban_two>

    <Function test_ban_three>

    <Function test_ban_four>

  <Module test_delete_comments.py>

    <Function test_delete_comments_one>

  <Module test_recover_comment.py>

    <Function test_recover_one>

    <Function test_recover_two>

========================================================================================= 3 deselected in 0.12 seconds =========================================================================================


(venv) D:\PyLib\CodemaoApiTest>pytest -k "one and not ban" --collect-only

============================================================================================= test session starts ==============================================================================================

platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0

rootdir: D:\PyLib\CodemaoApiTest

plugins: allure-pytest-2.7.1, forked-1.0.2, html-1.21.1, metadata-1.8.0, xdist-1.29.0

collected 12 items / 9 deselected / 3 selected                                                                                                                                                                 

<Package D:\PyLib\CodemaoApiTest\tests>

  <Module test_delete_comments.py>

    <Function test_delete_comments_one>

  <Module test_delete_works.py>

    <Function test_delete_works_one>

  <Module test_recover_comment.py>

    <Function test_recover_one>

========================================================================================= 9 deselected in 0.12 seconds =========================================================================================



3.-m选项

标记(marker)用于标记测试并分组,以便快速选中并运行。

(venv) D:\PyLib\CodemaoApiTest>pytest -m smoke

============================================================================================= test session starts ==============================================================================================

platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0

rootdir: D:\PyLib\CodemaoApiTest, inifile: pytest.ini

plugins: allure-pytest-2.7.1, forked-1.0.2, html-1.21.1, metadata-1.8.0, xdist-1.29.0

collected 12 items / 11 deselected / 1 selected                                                                                                                                                               

tests\test_recover_comment.py .                                                                                                                                                                          [100%]

=================================================================================== 1 passed, 11 deselected in 13.47 seconds ==================================================================================


4.-x选项

首次失败后停止执行

5.-v(--verbose)选项

(venv) D:\PyLib\NemoDataFactory\tests>pytest -v test_add.py

============================================================================================= test session starts ==============================================================================================

platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 -- d:\pylib\nemodatafactory\venv\scripts\python.exe

cachedir: .pytest_cache

metadata: {'Python': '3.6.5rc1', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '5.0.1', 'py': '1.8.0', 'pluggy': '0.12.0'}, 'Plugins': {'allure-pytest': '2.7.1', 'html': '1.21.1', 'metadata':

'1.8.0'}, 'JAVA_HOME': 'D:\\software\\Java'}

rootdir: D:\PyLib\NemoDataFactory\tests

plugins: allure-pytest-2.7.1, html-1.21.1, metadata-1.8.0

collected 24 items                                                                                                                                                                                             

test_add.py::test_add_bcm[{'name': 测试所有声音播放}] PASSED                                                                                                                                              [  4%]

test_add.py::test_add_bcm[{'name': 重构后得声音模块}] PASSED                                                                                                                                              [  8%]

test_add.py::test_add_bcm[{'name': 测试说你好}] PASSED                                                                                                                                                    [ 12%]

test_add.py::test_add_bcm[{'name': 测试侦测颜色}] PASSED                                                                                                                                                  [ 16%]

test_add.py::test_add_bcm[{'name': 测试侦测屏幕边缘}] PASSED                                                                                                                                              [ 20%]

test_add.py::test_add_bcm[{'name': 测试计时器}] PASSED                                                                                                                                  #                  [ 25%]

test_add.py::test_add_bcm[{'name': 测试手指}] PASSED                                                                                                                                                      [ 29%]

test_add.py::test_add_bcm[{'name': 测试画笔}] PASSED                                                                                                                                                      [ 33%]

test_add.py::test_add_bcm[{'name': 测试变量运算}] PASSED                                                                                                                                                  [ 37%]

test_add.py::test_add_bcm[{'name': 音乐播放器嘟嘟}] PASSED                                                                                                                                                [ 41%]

test_add.py::test_add_bcm[{'name': 追击飞电鼠}] PASSED                                                                                                                                                    [ 45%]

test_add.py::test_add_bcm[{'name': 5000积木数}] PASSED                                                                                                                                                    [ 50%]

test_add.py::test_add_bcm[{'name': 测试克隆类积木}] PASSED                                                                                                                                                [ 54%]

test_add.py::test_add_bcm[{'name': 测试控制类积木}] PASSED                                                                                                                                                [ 58%]

test_add.py::test_add_bcm[{'name': 测试动作类积木}] PASSED                                                                                                                                                [ 62%]

test_add.py::test_add_bcm[{'name': 编程猫快跑}] PASSED                                                                                                                                                    [ 66%]

test_add.py::test_add_bcm[{'name': 测试侦测类}] PASSED                                                                                                                                                    [ 70%]

test_add.py::test_add_bcm[{'name': 测试水平和重力}] PASSED                                                                                                                                                [ 75%]

test_add.py::test_add_bcm[{'name': 外观积木汇总}] PASSED                                                                                                                                                  [ 79%]

test_add.py::test_add_bcm[{'name': 测试当角色被点击}] PASSED                                                                                                                                              [ 83%]

test_add.py::test_add_bcm[{'name': 测试外观类}] PASSED                                                                                                                                                    [ 87%]

test_add.py::test_add_bcm[{'name': 测试x分量}] PASSED                                                                                                                                                    [ 91%]

test_add.py::test_add_bcm[{'name': 测试当积木}] PASSED                                                                                                                                                    [ 95%]

test_add.py::test_add_bcm[{'name': (创作页内运行)测试复制积木能否克隆}] PASSED                                                                                                                            [100%]

========================================================================================== 24 passed in 32.01 seconds =========================================================================================




6.-q(--quiet)选项

与-v相反,会简化输出信息。

(venv) D:\PyLib\NemoDataFactory\tests>pytest -q test_add.py

........................                                                                                                                                                                                  [100%]

24 passed in 31.47 seconds


7.-s选项

允许终端在测试时输出任何符合标准输出流信息(例如print语句)

8.-l(--showlocals)选项

使用-l选项,失败的测试用例由于被堆栈追踪,所以局部变量及其值都会显示出来。

(venv) D:\PyLib\NemoDataFactory\tests>pytest -l test_add.py

============================================================================================= test session starts ==============================================================================================

platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0

rootdir: D:\PyLib\NemoDataFactory\tests

plugins: allure-pytest-2.7.1, html-1.21.1, metadata-1.8.0

collected 24 items                                                                                                                                                                                             

test_add.py ........................                                                                                                                                                                      [100%]

========================================================================================== 24 passed in 30.74 seconds =========================================================================================


9.--tb=style选项

--tb=no屏蔽全部回溯信息

--tb=line可以告诉我们错误的位置

--tb=short显示的回溯信息比上面两种模式更详细

--tb=long输出最为详尽的回溯信息

--tb=auto默认值,如果有多个测试用例失败,仅打印第一个和最后一个用例的回溯信息(格式与long模式的一致)

--tb=native只输出python标准库的回溯信息,不显示额外信息


10.--duratioins=N选项

--durations=N会显示最慢的N个阶段(包括每个测试用例的call、setup、teardown),耗时越长越靠前。如果使用--durations=0,则会将所以阶段按耗时从长到短排序后显示。

(venv) D:\PyLib\CodemaoApiTest>pytest tests --durations=0

============================================================================================= test session starts ==============================================================================================

platform win32 -- Python 3.6.5rc1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0

rootdir: D:\PyLib\CodemaoApiTest, inifile: pytest.ini

plugins: allure-pytest-2.7.1, forked-1.0.2, html-1.21.1, metadata-1.8.0, xdist-1.29.0

collected 12 items                                                                                                                                                                                             

tests\test_ban_and_recover_login.py ..                                                                                                                                                                    [ 16%]

tests\test_ban_and_recover_publish.py ..                                                                                                                                                                  [ 33%]

tests\test_ban_comment.py ....                                                                                                                                                                            [ 66%]

tests\test_delete_comments.py .                                                                                                                                                                          [ 75%]

tests\test_delete_works.py .                                                                                                                                                                              [ 83%]

tests\test_recover_comment.py ..                                                                                                                                                                          [100%]

============================================================================================ slowest test durations ============================================================================================

37.84s call    tests/test_delete_comments.py::test_delete_comments_one

15.91s call    tests/test_delete_works.py::test_delete_works_one

12.94s call    tests/test_recover_comment.py::test_recover_one

9.03s call    tests/test_ban_and_recover_login.py::test_ban_and_recover_login_two

8.56s call    tests/test_recover_comment.py::test_recover_two

6.10s call    tests/test_ban_comment.py::test_ban_two

6.05s call    tests/test_ban_comment.py::test_ban_four

5.21s teardown tests/test_ban_and_recover_login.py::test_ban_and_recover_login_two

3.92s call    tests/test_ban_and_recover_publish.py::test_ban_two

3.75s setup    tests/test_ban_and_recover_login.py::test_ban_and_recover_login_two

3.32s setup    tests/test_ban_and_recover_login.py::test_ban_login_one

2.74s setup    tests/test_ban_comment.py::test_ban_two

2.71s setup    tests/test_recover_comment.py::test_recover_one

2.62s setup    tests/test_delete_works.py::test_delete_works_one

2.50s setup    tests/test_ban_comment.py::test_ban_four

2.46s teardown tests/test_recover_comment.py::test_recover_one

2.45s setup    tests/test_ban_comment.py::test_ban_three

2.44s setup    tests/test_delete_comments.py::test_delete_comments_one

2.42s setup    tests/test_ban_comment.py::test_ban_one

2.41s teardown tests/test_ban_comment.py::test_ban_one

2.37s setup    tests/test_recover_comment.py::test_recover_two

2.31s teardown tests/test_ban_and_recover_publish.py::test_ban_two

2.30s teardown tests/test_delete_comments.py::test_delete_comments_one

2.29s teardown tests/test_ban_comment.py::test_ban_two

2.28s teardown tests/test_delete_works.py::test_delete_works_one

2.25s teardown tests/test_ban_and_recover_login.py::test_ban_login_one

2.24s teardown tests/test_recover_comment.py::test_recover_two

2.22s teardown tests/test_ban_comment.py::test_ban_three

2.20s teardown tests/test_ban_comment.py::test_ban_four

2.03s teardown tests/test_ban_and_recover_publish.py::test_ban_one

1.54s call    tests/test_ban_and_recover_login.py::test_ban_login_one

1.52s setup    tests/test_ban_and_recover_publish.py::test_ban_two

1.47s setup    tests/test_ban_and_recover_publish.py::test_ban_one

1.31s call    tests/test_ban_and_recover_publish.py::test_ban_one

0.70s call    tests/test_ban_comment.py::test_ban_one

0.66s call    tests/test_ban_comment.py::test_ban_three

========================================================================================= 12 passed in 165.19 seconds =========================================================================================


11.帮助选项

pytest --version 查看pytest版本

pytest --fixtures 查看内置参数

pytest -h| --help 帮助文档

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,997评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,603评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,359评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,309评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,346评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,258评论 1 300
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,122评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,970评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,403评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,596评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,769评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,464评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,075评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,705评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,848评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,831评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,678评论 2 354

推荐阅读更多精彩内容