从0到1精通自动化测试,pytest自动化测试框架,配置文件pytest.ini(十三)

这篇具有很好参考价值的文章主要介绍了从0到1精通自动化测试,pytest自动化测试框架,配置文件pytest.ini(十三)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、前言

pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行

二、ini配置文件

  1. pytest里面有些文件是非test文件
  2. pytest.ini pytest的主配置文件,可以改变pytest的默认行为
  3. conftest.py 测试用例的一些fixture配置
  4. _init_.py 识别该文件夹为python的package包
  5. tox.ini 与pytest.ini类似,用tox工具时候才有用
  6. setup.cfg 也是ini格式文件,影响setup.py的行为

ini文件基本格式

# 保存为pytest.ini文件

[pytest]

addopts = -rsxX
xfail_strict = ture

使用pytest —help指令可以查看pytest.ini的设置选项

—rsxX 表示pytest报告所有测试用例被跳过、预计失败、预计失败但实际被通过的原因

三、mark标记

如下案例,使用了2个标签:webtest和hello,使用mark标记功能对于以后分类测试非常有用处

# content of test_mark.py
import pytest

@pytest.mark.webtest
def test_send_http():
    print("mark web test")

def test_something_quick():
    pass

def test_another():
    pass

@pytest.mark.hello
class TestClass:
    def test_01(self):
        print("hello :")

    def test_02(self):
        print("hello world!")

if __name__ == "__main__":
    pytest.main(["-v", "test_mark.py", "-m=hello"])

运行结果

============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0 -- D:\soft\python3.6\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.0', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '3.6.3', 'py': '1.5.4', 'pluggy': '0.6.0'}, 'Plugins': {'metadata': '1.7.0', 'html': '1.19.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'D:\\soft\\jdk18\\jdk18v'}
rootdir: D:\MOMO, inifile:
plugins: metadata-1.7.0, html-1.19.0, allure-adaptor-1.7.10
collecting ... collected 5 items / 3 deselected

test_mark.py::TestClass::test_01 PASSED                                  [ 50%]
test_mark.py::TestClass::test_02 PASSED                                  [100%]

=================== 2 passed, 3 deselected in 0.11 seconds ====================

有时候标签多了,不容易记住,为了方便后续执行指令的时候能准确使用mark的标签,可以写入到pytest.ini文件

# pytest.ini
[pytest]

markers =
  webtest:  Run the webtest case
  hello: Run the hello case

标记好之后,可以使用pytest —markers查看到

$ pytest —markers
D:\MOMO>pytest --markers
@pytest.mark.webtest:  Run the webtest case

@pytest.mark.hello: Run the hello case

@pytest.mark.skip(reason=None): skip the given test function with an optional re
ason. Example: skip(reason="no way of currently testing this") skips the test.

@pytest.mark.skipif(condition): skip the given test function if eval(condition)
results in a True value.  Evaluation happens within the module global context. E
xample: skipif('sys.platform == "win32"') skips the test if we are on the win32
platform. see http://pytest.org/latest/skipping.html

@pytest.mark.xfail(condition, reason=None, run=True, raises=None, strict=False):
 mark the test function as an expected failure if eval(condition) has a True val
ue. Optionally specify a reason for better reporting and run=False if you don't
even want to execute the test function. If only specific exception(s) are expect
ed, you can list them in raises, and if the test fails in other ways, it will be
 reported as a true failure. See http://pytest.org/latest/skipping.html

@pytest.mark.parametrize(argnames, argvalues): call a test function multiple tim
es passing in different arguments in turn. argvalues generally needs to be a lis
t of values if argnames specifies only one name or a list of tuples of values if
 argnames specifies multiple names. Example: @parametrize('arg1', [1,2]) would l
ead to two calls of the decorated test function, one with arg1=1 and another wit
h arg1=2.see http://pytest.org/latest/parametrize.html for more info and example
s.

@pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing
 all of the specified fixtures. see http://pytest.org/latest/fixture.html#usefix
tures

@pytest.mark.tryfirst: mark a hook implementation function such that the plugin
machinery will try to call it first/as early as possible.

@pytest.mark.trylast: mark a hook implementation function such that the plugin m
achinery will try to call it last/as late as possible.

最上面两个就是刚才写入到pytest.ini的配置了

四、禁用xpass

设置xfail_strict = true可以让那些标记为@pytest.mark.xfail但实际通过的测试用例被报告为失败

什么叫标记为@pytest.mark.xfail但实际通过,这个比较绕脑,看以下案例

# content of test_xpass.py
import pytest

def test_hello():
    print("hello world!")
    assert 1

@pytest.mark.xfail()
def test_momo1():
    a = "hello"
    b = "hello world"
    assert a == b

@pytest.mark.xfail()
def test_momo2():
    a = "hello"
    b = "hello world"
    assert a != b

if __name__ == "__main__":
    pytest.main(["-v", "test_xpass.py"])

测试结果

collecting ... collected 3 items

test_xpass.py::test_hello PASSED    [ 33%]
test_xpass.py::test_momo1 xfail     [ 66%]
test_xpass.py::test_momo2 XPASS     [100%]

=============== 1 passed, 1 xfailed, 1 xpassed in 0.27 seconds ================

test_momo1和test_momo2这2个用例一个是a == b一个是a != b,两个都标记失败了,我们希望两个用例不用执行全部显示xfail。实际上最后一个却显示xpass.为了让两个都显示xfail,那就加个配置
xfail_strict = true

# pytest.ini
[pytest]

markers =
  webtest:  Run the webtest case
  hello: Run the hello case

xfail_strict = true

再次运行,结果就变成

collecting ... collected 3 items

test_xpass.py::test_hello PASSED        [ 33%]
test_xpass.py::test_momo1 xfail         [ 66%]
test_xpass.py::test_momo2 FAILED        [100%]

================================== FAILURES ===================================
_________________________________ test_momo2 __________________________________
[XPASS(strict)] 
================ 1 failed, 1 passed, 1 xfailed in 0.05 seconds ================

这样标记为xpasx的就被强制性变成failed的结果

五、配置文件如何放

一般一个工程下方一个pytest.ini文件就可以了,放到顶层文件夹下

从0到1精通自动化测试,pytest自动化测试框架,配置文件pytest.ini(十三)

六、addopts

addopts参数可以更改默认命令行选项,这个当我们在cmd输入指令去执行用例的时候,会用到,比如我想测试完生成报告,指令比较长

$ pytest -v —rerun 1 —html=report.html —self-contained-html

 每次输入这么多,不太好记住,于是可以加到pytest.ini里

# pytest.ini
[pytest]

markers =
  webtest:  Run the webtest case
  hello: Run the hello case

xfail_strict = true

addopts = -v --rerun 1 --html=report.html --self-contained-html

这样我下次打开cmd,直接输入pytest,它就能默认带上这些参数了

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

【软件测试技术交流(资料分享)】:320231853(备注C)http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=wvdQ7J4AdIhjWqHqp-aGkIYde-wLap1P&authKey=4Zvhap%2BH44eyRbqYp4nbq21NHkZHo8kyPWSvyY50ssokKTX0rEYT15ZCaKuJE0Zc&noverify=0&group_code=320231853从0到1精通自动化测试,pytest自动化测试框架,配置文件pytest.ini(十三)生命不息,奋斗不止。每一份努力都不会被辜负,只要坚持不懈,终究会有回报。珍惜时间,追求梦想。不忘初心,砥砺前行。你的未来,由你掌握!

生命短暂,时间宝贵,我们无法预知未来会发生什么,但我们可以掌握当下。珍惜每一天,努力奋斗,让自己变得更加强大和优秀。坚定信念,执着追求,成功终将属于你!

只有不断地挑战自己,才能不断地超越自己。坚持追求梦想,勇敢前行,你就会发现奋斗的过程是如此美好而值得。相信自己,你一定可以做到!文章来源地址https://www.toymoban.com/news/detail-514678.html

到了这里,关于从0到1精通自动化测试,pytest自动化测试框架,配置文件pytest.ini(十三)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 从0到1精通自动化测试,pytest自动化测试框架,使用自定义标记mark(十一)

    pytest可以支持自定义标记,自定义标记可以把一个web项目划分多个模块,然后指定模块名称执行 app自动化的时候,如果想android和ios公用一套代码时,也可以使用标记功能,标明哪些是ios用例,哪些是android的,运行代码时候指定mark名称运行就可以 1.以下用例,标记test_send_h

    2024年02月11日
    浏览(55)
  • 从0到1精通自动化测试,pytest自动化测试框架,测试用例setup和teardown(三)

    目录 一、前言 二、用例运行级别 三、函数式 1、setup_function / teardown_function 2、setup_module / teardown_module 四、类和方法 五、函数和类混合 学过 unittest 的都知道里面用前置和后置 setup 和 teardown 非常好用,在每次用例开始前和结束后都去执行一次 当然还有更高级一点的 setupCla

    2024年02月09日
    浏览(33)
  • 从0到1精通自动化测试,pytest自动化测试框架,fixture之autouse=True(十二)

    平常写自动化用例会写一些前置的fixture操作,用例需要用到就直接传该函数的参数名称就行了。当用例很多的时候,每次都传这个参数,会比较麻烦 fixture里面有个参数autouse,默认是Fasle没开启的,可以设置为True开启自动使用fixture功能,这样用例就不用每次都去传参了 调用

    2024年02月11日
    浏览(29)
  • 从0到1精通自动化测试,pytest自动化测试框架,allure2生成html报告(史上最详细)(九)

    allure是一个report框架,支持java的Junit/testng等框架,当然也可以支持python的pytest框架,也可以集成到Jenkins上展示高大上的报告界面。 环境准备: python3.6 windows环境 pycharm pytest-allure-adaptor allure2.7.0 java1.8 pip安装 pytest-allure-adaptor,github地址:https://github.com/allure-framework/allure-pytest 如

    2024年02月11日
    浏览(30)
  • 超详细从入门到精通,pytest自动化测试框架实战-fixture多样玩法(九)

    在编写测试用例,都会涉及到用例执行之前的环境准备工作,和用例执行之后的环境清理工作。 代码版的测试用例也不例外。 pytest自动化测试框架:https://www.bilibili.com/video/BV18K411m7FH/ 在自动化测试框架当中,我们也需要编写: 用例执行之前的环境准备工作代码(前置工作代码

    2023年04月11日
    浏览(39)
  • pytest自动化框架运行全局配置文件pytest.ini

    还记得在之前的篇章中有讲到Pytest是目前主要流行的自动化框架之一,他有基础的脚本编码规则以及两种运行方式。 pytest的基础编码规则是可以进行修改,这就是今日文章重点。 看到这大家心中是否提出了两个问题:pytest的基础编码规则在哪可以修改?又是如何修改? 让我

    2024年02月09日
    浏览(39)
  • 自动化测试(三):接口自动化pytest测试框架

    API:Application Programming Interface 接口自动化按照自动化的工具可分为 基于 接口测试工具 的接口自动化 eg1:Postman+Newman+git/Svn+Jenkins(基于Javascript语言)接口自动化 Postman :创建和发送 API 请求,并对响应进行断言和验证。 Newman : Postman 的命令行工具,它允许测试人员在没有界

    2024年02月10日
    浏览(42)
  • Pytest自动化测试框架---(单元测试框架)

    unittest是python自带的单元测试框架,它封装好了一些校验返回的结果方法和一些用例执行前的初始化操作,使得单元测试易于开展,因为它的易用性,很多同学也拿它来做功能测试和接口测试,只需简单开发一些功能(报告,初始化webdriver,或者http请求方法)便可实现。 但自

    2024年02月14日
    浏览(47)
  • pytest 框架自动化测试

    随笔记录 目录 1. 安装  2. 安装pytest 相关插件 2.1 准备阶段 2.2 安装  2.3 验证安装成功  3. pytest测试用例的运行方式 3.1 主函数模式 3.1.1 主函数执行指定文件  3.1.2 主函数执行指定模块 3.1.3 主函数执行某个文件中的某个类、方法、函数 3.1.4 主函数执行生成allure报告 3.2 命令

    2024年02月19日
    浏览(37)
  • 自动化测试框架 —— pytest框架入门篇

    今天就给大家说一说pytest框架。 今天这篇文章呢,会从以下几个方面来介绍: 1、首先介绍一下pytest框架 2、带大家安装Pytest框架 3、使用pytest框架时需要注意的点 4、pytest的运行方式 5、pytest框架中常用的插件 pytest 是 python 的第三方单元测试框架,比自带 unittest 更简洁和高效

    2024年02月03日
    浏览(45)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包