def test_chained_failure(): # capture log capture2 = StringIO() logger = logging.getLogger('capture2') handler = logging.StreamHandler(capture2) logger.addHandler(handler) adapted = WarningLoggingAdapter(logger, None) assert_warn('foo', logger=adapted).is_length(4).is_in( 'bar').does_not_contain_duplicates() # dump log to string out = capture2.getvalue() capture2.close() assert_that(out).contains( '[test_warn.py:96]: Expected <foo> to be of length <4>, but was <3>.') assert_that(out).contains( '[test_warn.py:96]: Expected <foo> to be in <bar>, but was not.') assert_that(out).contains( '[test_warn.py:96]: Expected <foo> to not contain duplicates, but did.' )
assert_that({'a': 1, 'b': 2}).contains_entry({'a': 1}) assert_that({'fname': "Taysom", 'lname': "Hill"}).has_fname("Taysom") people = [{ 'fname': "Drew" }, { 'fname': "Jameis" }, { 'fname': "Taysom" }, { 'fname': "Ian" }] assert_that(people).extracting('fname').contains("Jameis") # flattening people = [['Drew', 'Brees'], ['Jameis', 'Winston']] assert_that(people).extracting(-1).is_equal_to(['Brees', 'Winston']) # flattening text = 'text text [pass] [error] [pass] pass [fail] text' pattern = r'\[.+?\]' assert_that(text).matches(pattern) assert_warn('foo').is_upper() with soft_assertions(): assert_that(10).is_equal_to(11) assert_that(10).is_equal_to(10) assert_that(10).is_equal_to(12) fail('should have thrown exception')
# Just A Warning # There are times when you only want a warning message instead of an failing test. # In this case, just replace assert_that with assert_warn. from assertpy import assert_warn assert_warn('foo').is_length(4) assert_warn('foo').is_empty() assert_warn('foo').is_false() assert_warn('foo').is_digit() assert_warn('123').is_alpha() assert_warn('foo').is_upper() assert_warn('FOO').is_lower() assert_warn('foo').is_equal_to('bar') assert_warn('foo').is_not_equal_to('foo') assert_warn('foo').is_equal_to_ignoring_case('BAR') # The above assertions just print the following warning messages, and an AssertionError is never raised: # # Expected <foo> to be of length <4>, but was <3>. # Expected <foo> to be empty string, but was not. # Expected <False>, but was not. # Expected <foo> to contain only digits, but did not. # Expected <123> to contain only alphabetic chars, but did not. # Expected <foo> to contain only uppercase chars, but did not. # Expected <FOO> to contain only lowercase chars, but did not. # Expected <foo> to be equal to <bar>, but was not. # Expected <foo> to be not equal to <foo>, but was. # Expected <foo> to be case-insensitive equal to <BAR>, but was not.
def test_assert_warn(self): assert_warn('foo').is_length(4) assert_warn('foo').is_empty() assert_warn('foo').is_false() assert_warn('foo').is_digit() assert_warn('123').is_alpha() assert_warn('foo').is_upper() assert_warn('FOO').is_lower() assert_warn('foo').is_equal_to('bar') assert_warn('foo').is_not_equal_to('foo') assert_warn('foo').is_equal_to_ignoring_case('BAR')
def test_failures(self): if sys.version_info[0] == 3: from io import StringIO else: from StringIO import StringIO # capture stdout old = sys.stdout sys.stdout = StringIO() assert_warn('foo').is_length(4) assert_warn('foo').is_empty() assert_warn('foo').is_false() assert_warn('foo').is_digit() assert_warn('123').is_alpha() assert_warn('foo').is_upper() assert_warn('FOO').is_lower() assert_warn('foo').is_equal_to('bar') assert_warn('foo').is_not_equal_to('foo') assert_warn('foo').is_equal_to_ignoring_case('BAR') # stop capturing stdout out = sys.stdout.getvalue() sys.stdout.close() sys.stdout = old assert_that(out).contains( 'Expected <foo> to be of length <4>, but was <3>.') assert_that(out).contains( 'Expected <foo> to be empty string, but was not.') assert_that(out).contains('Expected <False>, but was not.') assert_that(out).contains( 'Expected <foo> to contain only digits, but did not.') assert_that(out).contains( 'Expected <123> to contain only alphabetic chars, but did not.') assert_that(out).contains( 'Expected <foo> to contain only uppercase chars, but did not.') assert_that(out).contains( 'Expected <FOO> to contain only lowercase chars, but did not.') assert_that(out).contains( 'Expected <foo> to be equal to <bar>, but was not.') assert_that(out).contains( 'Expected <foo> to be not equal to <foo>, but was.') assert_that(out).contains( 'Expected <foo> to be case-insensitive equal to <BAR>, but was not.' )
def test_success(self): assert_warn('foo').is_length(3) assert_warn('foo').is_not_empty() assert_warn('foo').is_true() assert_warn('foo').is_alpha() assert_warn('123').is_digit() assert_warn('foo').is_lower() assert_warn('FOO').is_upper() assert_warn('foo').is_equal_to('foo') assert_warn('foo').is_not_equal_to('bar') assert_warn('foo').is_equal_to_ignoring_case('FOO')
def test_failures(): if sys.version_info[0] == 3: from io import StringIO else: from StringIO import StringIO # capture log capture = StringIO() logger = logging.getLogger('capture') handler = logging.StreamHandler(capture) logger.addHandler(handler) adapted = WarningLoggingAdapter(logger, None) assert_warn('foo', logger=adapted).is_length(4) assert_warn('foo', logger=adapted).is_empty() assert_warn('foo', logger=adapted).is_false() assert_warn('foo', logger=adapted).is_digit() assert_warn('123', logger=adapted).is_alpha() assert_warn('foo', logger=adapted).is_upper() assert_warn('FOO', logger=adapted).is_lower() assert_warn('foo', logger=adapted).is_equal_to('bar') assert_warn('foo', logger=adapted).is_not_equal_to('foo') assert_warn('foo', logger=adapted).is_equal_to_ignoring_case('BAR') # dump log to string out = capture.getvalue() capture.close() assert_that(out).contains('[test_warn.py:61]: Expected <foo> to be of length <4>, but was <3>.') assert_that(out).contains('[test_warn.py:62]: Expected <foo> to be empty string, but was not.') assert_that(out).contains('[test_warn.py:63]: Expected <False>, but was not.') assert_that(out).contains('[test_warn.py:64]: Expected <foo> to contain only digits, but did not.') assert_that(out).contains('[test_warn.py:65]: Expected <123> to contain only alphabetic chars, but did not.') assert_that(out).contains('[test_warn.py:66]: Expected <foo> to contain only uppercase chars, but did not.') assert_that(out).contains('[test_warn.py:67]: Expected <FOO> to contain only lowercase chars, but did not.') assert_that(out).contains('[test_warn.py:68]: Expected <foo> to be equal to <bar>, but was not.') assert_that(out).contains('[test_warn.py:69]: Expected <foo> to be not equal to <foo>, but was.') assert_that(out).contains('[test_warn.py:70]: Expected <foo> to be case-insensitive equal to <BAR>, but was not.')
def test_failures(self): if sys.version_info[0] == 3: from io import StringIO else: from StringIO import StringIO # capture stdout old = sys.stdout sys.stdout = StringIO() assert_warn('foo').is_length(4) assert_warn('foo').is_empty() assert_warn('foo').is_false() assert_warn('foo').is_digit() assert_warn('123').is_alpha() assert_warn('foo').is_upper() assert_warn('FOO').is_lower() assert_warn('foo').is_equal_to('bar') assert_warn('foo').is_not_equal_to('foo') assert_warn('foo').is_equal_to_ignoring_case('BAR') # stop capturing stdout out = sys.stdout.getvalue() sys.stdout.close() sys.stdout = old assert_that(out).contains('Expected <foo> to be of length <4>, but was <3>.') assert_that(out).contains('Expected <foo> to be empty string, but was not.') assert_that(out).contains('Expected <False>, but was not.') assert_that(out).contains('Expected <foo> to contain only digits, but did not.') assert_that(out).contains('Expected <123> to contain only alphabetic chars, but did not.') assert_that(out).contains('Expected <foo> to contain only uppercase chars, but did not.') assert_that(out).contains('Expected <FOO> to contain only lowercase chars, but did not.') assert_that(out).contains('Expected <foo> to be equal to <bar>, but was not.') assert_that(out).contains('Expected <foo> to be not equal to <foo>, but was.') assert_that(out).contains('Expected <foo> to be case-insensitive equal to <BAR>, but was not.')