允许每次测试多次失败的pytest插件。

pytest-check的Python项目详细描述


Pytest检查

允许每次测试多次失败的pytest插件。


这个pytest插件是一个重写和 重命名pytest-expect

安装

来自PPI:

$ pip install pytest-check

或者来自Github。

$ pip install git+https://github.com/okken/pytest-check

用法

使用导入的示例:

importpytest_checkascheckdeftest_example():a=1b=2c=[2,4,6]check.greater(a,b)check.less_equal(b,a)check.is_in(a,c,"Is 1 in the list")check.is_not_in(b,c,"make sure 2 isn't in list")

测试结果:

=================================== FAILURES ===================================
_________________________________ test_example _________________________________
FAILURE:
assert 1 > 2
  test_check.py, line 14, in test_example() -> check.greater(a, b)
FAILURE:
assert 2 <= 1
  test_check.py, line 15, in test_example() -> check.less_equal(b, a)
FAILURE: Is 1 in the list
assert 1 in [2, 4, 6]
  test_check.py, line 16, in test_example() -> check.is_in(a, c, "Is 1 in the list")
FAILURE: make sure 2 isn't in list
assert 2 not in [2, 4, 6]
  test_check.py, line 17, in test_example() -> check.is_not_in(b, c, "make sure 2 isn't in list")
------------------------------------------------------------
Failed Checks: 4
=========================== 1 failed in 0.11 seconds ===========================

使用夹具的示例:

deftest_example(check):a=1b=2c=[2,4,6]check.greater(a,b)check.less_equal(b,a)check.is_in(a,c,"Is 1 in the list")check.is_not_in(b,c,"make sure 2 isn't in list")

验证功能

  • check.equal-a==b
  • check.不等于-a!=b
  • check.is_true-bool(x)is true
  • check.is_false-bool(x)is false
  • check.is_none-x is none
  • check.is_not_none-x is not none
  • check.is_in-a in b
  • check.is_not_in-a not in b
  • check.is_instance-isinstance(a,b)
  • check.not_是实例-not is instance(a,b)
  • check.几乎等于-a==pytest.大约(b,rel,abs)参见:pytest.approx
  • 检查。不几乎等于-a!=pytest.约(b,rel,abs)见:pytest.approx
  • check.greater-a>;b
  • check.greater_等于-a>;=b
  • check.减去-a<;b
  • check.小于等于-a<;=b

定义自己的检查功能

装饰器允许您包装任何具有断言的测试助手 语句中的语句为非阻塞断言函数。

frompytest_checkimportcheck_func@check_funcdefis_four(a):asserta==4deftest_all_four():is_four(1)is_four(2)is_four(3)is_four(4)

以上结果将导致:

...
________________________________ test_all_four _________________________________
FAILURE: assert 1 == 4
  test_fail.py, line 8, in test_all_four() -> is_four(1)
FAILURE: assert 2 == 4
  test_fail.py, line 9, in test_all_four() -> is_four(2)
FAILURE: assert 3 == 4
  test_fail.py, line 10, in test_all_four() -> is_four(3)
------------------------------------------------------------
Failed Checks: 3
=========================== 1 failed in 0.12 seconds ===========================

使用check作为上下文管理器

您可以使用check()上下文管理器在测试中包装任何要在之后继续的断言。

frompytest_checkimportcheckdeftest_context_manager():withcheck:x=3assert1<x<4

但是,在任何with check:中,您仍然无法通过assert语句, 因此,您需要为多个断言使用多个with check:块:

deftest_multiple_failures():withcheck:assert1==0withcheck:assert1>2withcheck:assert1<5<4

贡献

我们非常欢迎您的贡献。可以使用tox运行测试。 测试覆盖率现在是100%。请务必保持100%。 如果你有一个很棒的拉请求,需要帮助,使覆盖率回升,让我知道。

许可证

“pytest check”是根据MIT许可证的条款发布的免费开源软件

问题

如果您遇到任何问题,请file an issue连同详细说明。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Docker&SeleniumJava:无法在Docker容器上运行的chrome浏览器中上载图像/文件   在python中运行java命令   Java垃圾收集器异常行为   java java是否根据底层操作系统执行字节码级优化?   java是否可以休眠自定义查询返回映射而不是列表?   java Spring引导RabbitMQ接收器Jackson反序列化到POJO   apache flex在ActionScript3中创建对象相等“HashMap”作为java HashMap   java如何在Eclipse集成中切换JProfiler启动器   缓存JSP页面结果的java最佳实践?   java集成jaxb绑定文件,使用CXF生成基于WSDL的客户端   java为什么在上传操作结束之前,客户端没有检测到HttpServletResponse的PrintWriter内容?   java在接口内创建类和在类内创建接口有什么用   java向文件写入错误Android Studio   java合并多个RealmList并对结果列表排序?   谷歌API视觉java。lang.NoSuchMethodError   java如何使用逗号分别存储每个值,然后将它们存储到单独的数组中?