Pytest Testing Idioms: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 10: Line 10:
def test_something_that_should_throw_exception():
def test_something_that_should_throw_exception():
     with pytest.raises(Exception) as e_info:
     with pytest.raises(Exception) as e_info:
        my_method()
</syntaxhighlight>
If the tested code raises a more specific exception, you can use that instead:
<syntaxhighlight lang='python'>
def test_something_that_should_throw_exception():
    with pytest.raises(ValueException) as e_info:
         my_method()
         my_method()
</syntaxhighlight>
</syntaxhighlight>


<font color=darkkhaki>
<font color=darkkhaki>
* How do I ensure a specific type of exception?
 
* How to I test the exception message?
* How to I test the exception message?
</font>
</font>


Also see: {{Internal|Python_Language_Exceptions#Overview|Python Language &#124; Exceptions}}
Also see: {{Internal|Python_Language_Exceptions#Overview|Python Language &#124; Exceptions}}

Revision as of 21:39, 16 February 2022

External

Internal

Overview

Tested Code is Supposed to Throw Exception

import pytest

def test_something_that_should_throw_exception():
    with pytest.raises(Exception) as e_info:
        my_method()

If the tested code raises a more specific exception, you can use that instead:

def test_something_that_should_throw_exception():
    with pytest.raises(ValueException) as e_info:
        my_method()

  • How to I test the exception message?

Also see:

Python Language | Exceptions