Does pytest have anything like google tests non-fatal EXPECT_* behavior?

2024/9/8 9:15:49

I'm more familiar with the google test framework and know about the primary behavior pair they support about ASSERT_* vs EXPECT_* which are the fatal and non-fatal assert modes.

From the documentation:

The assertions come in pairs that test the same thing but havedifferent effects on the current function. ASSERT_* versions generatefatal failures when they fail, and abort the current function.EXPECT_* versions generate nonfatal failures, which don't abort thecurrent function. Usually EXPECT_* are preferred, as they allow morethan one failures to be reported in a test. However, you should useASSERT_* if it doesn't make sense to continue when the assertion inquestion fails.

Question: does pytest also have a non fatal assert flavor or mode I can enable?

It's nice to allow a full range of tests to maximally execute to get the richest failure history rather than abort at the first failure and potentially hide subsequent failures that have to be discovered piecewise by running multiple instances of the test application.

Answer

I use pytest-assume for non-fatal assertions. It does the job pretty well.

Installation

As usual,

$ pip install pytest-assume

Usage example

import pytestdef test_spam():pytest.assume(True)pytest.assume(False)a, b = True, Falsepytest.assume(a == b)pytest.assume(1 == 0)pytest.assume(1 < 0)pytest.assume('')pytest.assume([])pytest.assume({})

If you feel writing pytest.assume is a bit too much, just alias the import:

import pytest.assume as expectdef test_spam():expect(True)...

Running the above test yields:

$ pytest -v
============================= test session starts ==============================
platform linux -- Python 3.6.5, pytest-3.6.0, py-1.5.3, pluggy-0.6.0 -- /data/gentoo64-prefix/u0_a82/projects/stackoverflow/so-50630845
cachedir: .pytest_cache
rootdir: /data/gentoo64-prefix/u0_a82/projects/stackoverflow/so-50630845, inifile:
plugins: assume-1.2
collecting ... collected 1 itemtest_spam.py::test_spam FAILED                                            [100%]=================================== FAILURES ===================================
__________________________________ test_spam ___________________________________
test_spam.py:6: AssumptionFailurepytest.assume(False)test_spam.py:9: AssumptionFailurepytest.assume(a == b)test_spam.py:11: AssumptionFailurepytest.assume(1 == 0)test_spam.py:12: AssumptionFailurepytest.assume(1 < 0)test_spam.py:13: AssumptionFailurepytest.assume('')test_spam.py:14: AssumptionFailurepytest.assume([])test_spam.py:14: AssumptionFailurepytest.assume([])test_spam.py:15: AssumptionFailurepytest.assume({})------------------------------------------------------------
Failed Assumptions: 7
=========================== 1 failed in 0.18 seconds ===========================
https://en.xdnf.cn/q/73208.html

Related Q&A

Radon transformation in python

Here is a dummy code:def radon(img):theta = np.linspace(-90., 90., 180, endpoint=False)sinogram = skimage.transform.radon(img, theta=theta, circle=True)return sinogram # end defI need to get the sinogr…

Librosa raised OSError(sndfile library not found) in Docker

Im trying to write the Dockerfile for a small python web project and there is something wrong with the dependencies. Ive been doing some search on the internet and it said that Librosa library requires…

Implementing Tags using Django rest framework

TDLR : what is the best way to implement tags in django-rest-framework. where the tags has a created_by field which is the currently authenticated user.I am trying to achieve a very simple/common thing…

Python audiolab install, unable to install (or find) libsndfile on Mac OSX

Trying to install scikits.audiolab-0.11.0 on Mac, bit it requires libsndfile: http://www.mega-nerd.com/libsndfile/. I did install libsndfile supposedly, using libsndfile_python-1.0.0-py2.7-macosx10.5.m…

Connecting to events of another widget

This is most likely a duplicate question, but I have to ask it because other answers arent helping in my case, since I am new to pyqt (switched from tkinter few days ago).I am wondering if is it possib…

Diff multidimensional dictionaries in python

I have two dictionariesa = {home: {name: Team1, score: 0}, away: {name: Team2, score: 0}} b = {home: {name: Team1, score: 2}, away: {name: Team2, score: 0}}The keys never change but I want to get that …

Pandas DatetimeIndex vs to_datetime discrepancies

Im trying to convert a Pandas Series of epoch timestamps to human-readable times. There are at least two obvious ways to do this: pd.DatetimeIndex and pd.to_datetime(). They seem to work in quite dif…

Slicing a circle in equal segments, Python

I have a set of close of 10,000 points on the sky. They are plotted using the RA (right ascension) and DEC (declination) on the sky. When plotted, they take the shape of a circle. What I would like to …

Pyautogui screenshot. Where does it go? How to save and find later?

I am learning from Al Sweigarts you tube video for automating the boring stuff. I got to the part of taking screenshots. He didnt really explain in his video so I tested things out. I found that it tak…

How to get pip to point to newer version of Python

I have two versions of Python installed on my centOS server. [ethan@demo ~]$ python2.6 --version Python 2.6.6 [ehtan@demo ~]$ python --version Python 2.7.3The older version (2.6) is required by some es…