Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to test value error in pytest in python

import pytest

def test_passes():
    with pytest.raises(Exception) as e_info:
        x = 1 / 0

def test_passes_without_info():
    with pytest.raises(Exception):
        x = 1 / 0

def test_fails():
    with pytest.raises(Exception) as e_info:
        x = 1 / 1

def test_fails_without_info():
    with pytest.raises(Exception):
        x = 1 / 1

# Don't do this. Assertions are caught as exceptions.
def test_passes_but_should_not():
    try:
        x = 1 / 1
        assert False
    except Exception:
        assert True

# Even if the appropriate exception is caught, it is bad style,
# because the test result is less informative
# than it would be with pytest.raises(e)
# (it just says pass or fail.)

def test_passes_but_bad_style():
    try:
        x = 1 / 0
        assert False
    except ZeroDivisionError:
        assert True

def test_fails_but_bad_style():
    try:
        x = 1 / 1
        assert False
    except ZeroDivisionError:
        assert True
Comment

PREVIOUS NEXT
Code Example
Python :: 2d arrays with rows and columns 
Python :: 2d array row and column index 
Python :: filter lambda python 
Python :: what is index in list in python 
Python :: Django rest framework update or delete 
Python :: strip() 
Python :: if key not in dictionary python 
Python :: find if value exists in dictionary python 
Python :: plotly subplots 
Python :: python list extend() 
Python :: python server 
Python :: with torch.no_grad() 
Python :: salvar plot python 
Python :: python logging level 
Python :: python convert 12 hour time to 24 hour 
Python :: python format string 
Python :: python or 
Python :: how to add to a list python 
Python :: journalctl not showing all python prints 
Python :: plotly scatter facet change labels 
Python :: Python Tuples Tuples allow duplicate values 
Python :: numpy combine two arrays selecting min 
Python :: from string to flaot python numpy 
Python :: regular expressions in python 
Python :: gtts python 
Python :: request login python 
Python :: python string ignore characters 
Python :: shibang for python file in linux 
Python :: path selecter in tkinter 
Python :: install python 3 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =