博客
关于我
8 个很棒的 pytest 插件 | Linux 中国
阅读量:291 次
发布时间:2019-03-03

本文共 1160 字,大约阅读时间需要 3 分钟。

Python 测试工具最好的一方面是其强大的生态系统。以下是8个值得推荐的 pytest 插件。

我们是 pytest 的忠实粉丝,并将其作为工作和开源项目的默认 Python 测试工具。在本月的 Python 专栏中,我们分享了为什么我们喜欢 pytest,以及一些让 pytest 测试工作更有趣的插件。

pytest 是一个轻量级的测试框架,能够轻松编写小型测试,也能扩展支持复杂功能测试。它允许你在名为 test_*.py 的文件中定义测试,并将其定义为以 test_ 开头的函数。pytest 将自动查找项目中所有测试,并在控制台中运行时执行它们。它还支持多种标志和参数,能够根据需要配置输出结果、指定运行测试范围以及输出信息。

pytest 最好的特点是其强大的插件生态系统。由于 pytest 非常流行,多年来开发了大量插件来扩展、定制和增强其功能。以下是我们最喜欢的8个插件。

  • pytest-sugar

    这个插件改变了 pytest 的默认输出,添加了进度条,并立即显示失败的测试。安装简单,直接运行 pytest 即可享受更漂亮的输出。

  • pytest-cov

    pytest-cov 在 pytest 中增加了代码覆盖率支持,显示已经测试过的代码行和未测试的部分。它还提供项目测试覆盖率统计。

  • pytest-picked

    这个插件允许你在提交代码但未提交的文件中运行测试。安装后运行 pytest --picked,只测试自上次提交后已修改的文件。

  • pytest-instafail

    这个插件修改了 pytest 的默认行为,使其立即显示失败和错误,而不是等到所有测试完成。

  • pytest-tldr

    这是一个全新的 pytest 插件,限制输出为你需要的内容。与 pytest-sugar 类似,安装后无需配置。它默认输出失败测试的回溯信息,忽略了颜色编码。添加 -v 标志可获得更详细输出。

  • pytest-xdist

    这个插件允许并行运行测试。例如,运行 pytest -n 2 将在两个 CPU 上执行测试。它还支持 --looponfail 标志,自动重新运行失败测试。

  • pytest-django

    这个插件为 Django 应用提供 pytest 支持。它简化了测试开发,省去了使用 unittest 和复制样板测试代码的麻烦,并且比标准 Django 测试套件运行更快。

  • django-test-plus

    虽然这个插件主要是为 unittest 开发的,但它现在也支持 pytest。它包含自己的 TestCase 类,使得你的测试代码更简洁,能够快速输出频繁的测试案例。

  • 我们提到的这些库并不是扩展 pytest 的唯一选择。Pytest 插件的生态系统广阔,值得你去探索。你最喜欢哪些插件?

    转载地址:http://snal.baihongyu.com/

    你可能感兴趣的文章
    org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement profile
    查看>>
    sql查询中 查询字段数据类型 int 与 String 出现问题
    查看>>
    org.apache.commons.beanutils.BasicDynaBean cannot be cast to ...
    查看>>
    org.apache.dubbo.common.serialize.SerializationException: com.alibaba.fastjson2.JSONException: not s
    查看>>
    sqlserver学习笔记(三)—— 为数据库添加新的用户
    查看>>
    org.apache.http.conn.HttpHostConnectException: Connection to refused
    查看>>
    org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
    查看>>
    org.apache.ibatis.exceptions.PersistenceException:
    查看>>
    org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
    查看>>
    org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
    查看>>
    org.apache.poi.hssf.util.Region
    查看>>
    org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugManifest'
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>
    org.springframework.amqp.AmqpConnectException:java.net.ConnectException:Connection timed out:connect
    查看>>
    org.springframework.beans.factory.BeanDefinitionStoreException
    查看>>
    org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
    查看>>
    org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
    查看>>