Skip to content

Instantly share code, notes, and snippets.

@gurneyalex
Created August 16, 2021 14:41
Show Gist options
  • Save gurneyalex/e5d5f58a7af905659b9ca0bdafd2590f to your computer and use it in GitHub Desktop.
Save gurneyalex/e5d5f58a7af905659b9ca0bdafd2590f to your computer and use it in GitHub Desktop.
from unittest import TestCase
def some_method(msg):
raise ValueError(msg)
class MyTest(TestCase):
def test_assert_raises_regex(self):
with self.assertRaisesRegex(ValueError, r'some message'):
some_method("expect some message")
def test_assert_raises_regex_with_newline(self):
with self.assertRaisesRegex(ValueError, r'expect\n some'):
some_method("expect\n some message")
def test_assert_raises_regex_with_newline_and_anchor(self):
with self.assertRaisesRegex(ValueError, r'expect\n some.*$'):
some_method("expect\n some message")
def test_assert_raises_regex_with_newline_and_anchor_and_quotes(self):
with self.assertRaisesRegex(ValueError, r'expect\n some ".*$'):
some_method("expect\n some \"message")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment