即使其中一些断言失败,如何运行所有 PyTest 断言? [英] How to run all PyTest assertions even if some of them fail?

查看:48
本文介绍了即使其中一些断言失败,如何运行所有 PyTest 断言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来在 PyTest 中运行我的单元测试中的所有断言,即使其中一些断言失败.我知道必须有一个简单的方法来做到这一点.我检查了 CLi 选项并浏览了此站点以查找类似的问题/答案,但没有看到任何内容.抱歉,如果已经回答了这个问题.

I am looking for a way to run all of the assertions in my unit tests in PyTest, even if some of them fail. I know there must be a simple way to do this. I checked the CLi options and looked through this site for similar questions/answers but didn't see anything. Sorry if this has already been answered.

例如,考虑下面的代码片段,旁边有 PyTest 代码:

For example, consider the following code snippet, with PyTest code alongside it:

def parrot(i):
    return i

def test_parrot():
    assert parrot(0) == 0
    assert parrot(1) == 1
    assert parrot(2) == 1
    assert parrot(2) == 2

默认情况下,执行在第一次失败时停止:

By default, the execution stops at the first failure:

$ python -m pytest fail_me.py 
=================== test session starts ===================
platform linux2 -- Python 2.7.10, pytest-2.9.1, py-1.4.31, pluggy-0.3.1
rootdir: /home/npsrt/Documents/repo/codewars, inifile: 
collected 1 items 

fail_me.py F

=================== FAILURES ===================
___________________ test_parrot ___________________

    def test_parrot():
        assert parrot(0) == 0
        assert parrot(1) == 1
>       assert parrot(2) == 1
E       assert 2 == 1
E        +  where 2 = parrot(2)

fail_me.py:7: AssertionError
=================== 1 failed in 0.05 seconds ===================

我想做的是让代码在 PyTest 遇到第一次失败后继续执行.

What I'd like to do is to have the code continue to execute even after PyTest encounters the first failure.

推荐答案

它运行了您的所有测试.你只写了一个测试,那个测试就跑了!

It ran all of your tests. You only wrote one test, and that test ran!

如果您想要非致命断言,如果断言失败,测试将继续进行(如 Google 测试的 EXPECT 宏),请尝试 pytest-expect,它提供了该功能.这是他们网站提供的示例:

If you want nonfatal assertions, where a test will keep going if an assertion fails (like Google Test's EXPECT macros), try pytest-expect, which provides that functionality. Here's the example their site gives:

def test_func(expect):
    expect('a' == 'b')
    expect(1 != 1)
    a = 1
    b = 2
    expect(a == b, 'a:%s b:%s' % (a,b))

您可以看到预期失败不会停止测试,并且所有失败的预期都会被报告:

You can see that expectation failures don't stop the test, and all failed expectations get reported:

$ python -m pytest test_expect.py
================ test session starts =================
platform darwin -- Python 2.7.9 -- py-1.4.26 -- pytest-2.7.0
rootdir: /Users/okken/example, inifile: 
plugins: expect
collected 1 items 

test_expect.py F

====================== FAILURES ======================
_____________________ test_func ______________________
>    expect('a' == 'b')
test_expect.py:2
--------
>    expect(1 != 1)
test_expect.py:3
--------
>    expect(a == b, 'a:%s b:%s' % (a,b))
a:1 b:2
test_expect.py:6
--------
Failed Expectations:3
============== 1 failed in 0.01 seconds ==============

这篇关于即使其中一些断言失败,如何运行所有 PyTest 断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆