Is there a way to specify which pytest tests to run from a file?

You can use -k option to run test cases with different patterns: py.test tests_directory/foo.py tests_directory/bar.py -k ‘test_001 or test_some_other_test’ This will run test cases with name test_001 and test_some_other_test deselecting the rest of the test cases. Note: This will select any test case starting with test_001 or test_some_other_test. For example, if you have test case … Read more

How to properly assert that an exception gets raised in pytest?

pytest.raises(Exception) is what you need. Code 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 … Read more