Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

# coding=utf-8 

import pytest 

def whatever(): 

return 9/0 

def test_whatever(): 

try: 

whatever() 

except ZeroDivisionError as exc: 

pytest.fail(exc, pytrace=True)

How to make pytest print traceback, so I would see where in the whatever function an exception was raised?

1 Answer

0 votes
by (106k points)

To properly assert that an exception gets raised in pytest you can use the below-mentioned code:-

def test_raises():

with pytest.raises(Exception) as excinfo: 

raise Exception('some info') 

assert execinfo.value.args[0] == 'some info' 

assert str(execinfo.value) == 'some info'

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...