Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I'm trying to test code where I want to test multiple rules within a single with pytest.raises(ValueError) exception, is there a Pythonic way to do this? In the example below, I want to test that all 4 function calls will throw the value error.

With pytest.raises(ValueError):

  function_that_throws_exception(param1)

  function_that_throws_exception(param2)

  function_that_throws_exception(param3)

  function_that_throws_exception(param4)

1 Answer

0 votes
by (36.8k points)

I would suggest using parametrize:

@pytest.mark.parametrize("param", [param1, param2...])

def test_function_that_throws_exception(param):

    with pytest.raises(ValueError):

       function_that_throws_exception(param)

If you are a beginner and want to know more about Data Science the do check out the Data Science course 

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...