def test_spy_knows_if_it_was_called(self): spy = create_spy() spy.assert_was_not_called() assert_raises(AssertionError, spy.assert_was_called) spy() assert_raises(AssertionError, spy.assert_was_not_called) spy.assert_was_called()
def test_fails_if_no_exception_was_raised(self): try: assert_raises(Exception, self._good_callable()) except AssertionError: pass else: self.fail('No assertion raised')
def test_can_specify_additional_custom_message(self): try: assert_raises(ValueError, self._good_callable(), message='Foo') except AssertionError as e: assert 'ValueError not raised! Foo' == exception_message(e), repr(exception_message(e)) else: self.fail('AssertionError not raised!')
def test_passes_unexpected_exceptions(self): try: assert_raises(ValueError, self._fail_with(AssertionError())) except AssertionError: pass else: self.fail('Did not raise ValueError')
def test_fails_with_sensible_default_error_message(self): try: assert_raises(ValueError, self._good_callable()) except AssertionError as e: assert 'ValueError not raised!' == exception_message(e), repr(exception_message(e)) else: self.fail('AssertionError not raised!')
def test_can_detect_invalid_wsa_header(self): dispatcher = SOAPDispatcher(echo_service()) header = wsa.Header.parsexml( '<Header><Invalid xmlns="http://www.w3.org/2005/08/addressing">/Action</Invalid></Header>' ) assert_raises(etree.DocumentInvalid, lambda: dispatcher._validate_header(header))
def test_fails_with_sensible_default_error_message(self): try: assert_raises(ValueError, self._good_callable()) except AssertionError as e: assert 'ValueError not raised!' == exception_message(e), repr( exception_message(e)) else: self.fail('AssertionError not raised!')
def test_can_specify_additional_custom_message(self): try: assert_raises(ValueError, self._good_callable(), message='Foo') except AssertionError as e: assert 'ValueError not raised! Foo' == exception_message(e), repr( exception_message(e)) else: self.fail('AssertionError not raised!')
def test_passes_if_callable_raised_exception(self): assert_raises(ValueError, self._fail_with(ValueError())) # also test for Python 2.6 specific "behavior"/bug where # Python 2.6 sometimes only passes a string (instead of # the exception instance) to the context manager # https://bugs.python.org/issue7853 def raises_valueerror(): from datetime import date # not sure how to reproduce the issue generically but # this date call triggers it at least. date(2000, 12, 50) assert_raises(ValueError, raises_valueerror)
def test_element_with_ref_attribute_rejects_forbidden_attributes(self): raise SkipTest( 'Elements with "ref" attribute currently do not restrict setting other attributes.' ) element = xsdspec.Element() element.ref = 'foo' element.minOccurs = 3 element.maxOccurs = '6' # element.id (not present in xsdspec.Element) def set_(attribute, value): return lambda: setattr(element, attribute, value) assert_raises(ValueError, set_('name', u'bar')) assert_raises(ValueError, set_('type', u'xs:string')) assert_raises(ValueError, set_('nillable', u'True')) simple_type = xsdspec.SimpleType(restriction=xsdspec.Restriction( base='string')) assert_raises(ValueError, set_('simpleType', simple_type)) # assert_raises(ValueError, set_('complexType', u'True')) element.ref = None # doesn't raise anymore because we deleted the "ref" attribute element.name = u'bar'
def test_should_know_positional_call_parameters(self): spy = create_spy() spy(23) spy.assert_was_called_with(23) assert_raises(AssertionError, lambda: spy.assert_was_called_with()) assert_raises(AssertionError, lambda: spy.assert_was_called_with(23, 24)) assert_raises(AssertionError, lambda: spy.assert_was_called_with(foo=23)) assert_raises(AssertionError, lambda: spy.assert_was_called_with(23, fnord='baz'))
def test_passing_tests_marked_as_expected_failure_are_treated_as_errors(self): @expect_failure def passing(): pass with assert_raises(AssertionError, message='should treat passing tests as error'): passing()
def test_append_restriction(self): l = xsd.ListElement(xsd.String, maxOccurs=1, tagname='toto').empty_value() l.append('a') e = assert_raises(ValueError, lambda: l.append('a')) assert_equals('You must not add more than 1 items to this list.', str(e))
def test_cannot_omit_required_attribute(self): instance = self.SampleType(value='someValue', type='someType') element = etree.Element('sample') with assert_raises(ValueError) as context: instance.render(element, instance) # the exception raised should mention the field is required assert_contains('required', str(context.caught_exception))
def test_should_know_keyword_call_parameters(self): spy = create_spy() assert_raises(AssertionError, lambda: spy.assert_was_called_with()) spy() spy.assert_was_called_with() spy(foo='bar') spy.assert_was_called_with(foo='bar') assert_raises(AssertionError, lambda: spy.assert_was_called_with()) assert_raises(AssertionError, lambda: spy.assert_was_called_with('bar')) assert_raises(AssertionError, lambda: spy.assert_was_called_with(foo='bar', fnord='baz'))
def test_passing_tests_marked_as_expected_failure_are_treated_as_errors( self): @expect_failure def passing(): pass with assert_raises(AssertionError, message='should treat passing tests as error'): passing()
def test_can_return_contextmanager(self): with assert_raises(ValueError): raise ValueError() try: with assert_raises(ValueError): raise AssertionError() except AssertionError: pass else: self.fail('Did not raise ValueError') try: with assert_raises(ValueError): pass except AssertionError: pass else: self.fail('No assertion raised')
def test_can_return_contextmanager(self): with assert_raises(ValueError): raise ValueError() try: with assert_raises(ValueError): raise AssertionError() except AssertionError: pass else: self.fail("Did not raise ValueError") try: with assert_raises(ValueError): pass except AssertionError: pass else: self.fail("No assertion raised")
def test_can_assert_logged_messages(self): log, lc = build_collecting_logger() log.info('foo') log.debug('bar') assert_did_log_message(lc, 'foo') assert_did_log_message(lc, 'foo', level=logging.INFO) with assert_raises(AssertionError): assert_did_log_message(lc, 'foo', level=logging.DEBUG) assert_no_log_messages(lc, min_level=logging.WARN)
def test_should_know_keyword_call_parameters(self): spy = create_spy() assert_raises(AssertionError, lambda: spy.assert_was_called_with()) spy() spy.assert_was_called_with() spy(foo='bar') spy.assert_was_called_with(foo='bar') assert_raises(AssertionError, lambda: spy.assert_was_called_with()) assert_raises(AssertionError, lambda: spy.assert_was_called_with('bar')) assert_raises( AssertionError, lambda: spy.assert_was_called_with(foo='bar', fnord='baz'))
def test_soap12_fault_handling(self): service = soap.Service( location='mock_location', methods=[], name=None, schemas=[], targetNamespace=None, version=soap.SOAPVersion.SOAP12, ) stub = soap.Stub(location='empty', service=service) e = assert_raises(core.SOAPError, lambda: stub._handle_response(None, None, SOAP12_ERROR_ROLE)) assert_equals('env:Sender', e.code) assert_equals('\nMessage does not have necessary info\n', e.message) assert_equals('http://gizmos.com/order', e.actor)
def test_soap11_fault_handling(self): service = soap.Service( location='mock_location', methods=[], name=None, schemas=[], targetNamespace=None, version=soap.SOAPVersion.SOAP11, ) stub = soap.Stub(location='empty', service=service) e = assert_raises(core.SOAPError, lambda: stub._handle_response(None, None, SOAP11_ERROR_MESSAGE)) assert_equals('Result', e.code) assert_none(e.message) assert_equals('Resultset empty2.', e.actor)
def test_soap12_fault_handling(self): service = soap.Service( location='mock_location', methods=[], name=None, schemas=[], targetNamespace=None, version=soap.SOAPVersion.SOAP12, ) stub = soap.Stub(location='empty', service=service) e = assert_raises( core.SOAPError, lambda: stub._handle_response(None, None, SOAP12_ERROR_ROLE)) assert_equals('env:Sender', e.code) assert_equals('\nMessage does not have necessary info\n', e.message) assert_equals('http://gizmos.com/order', e.actor)
def test_tests_with_errors_marked_as_expected_failure_are_treated_as_errors(self): # This is actually a really important part of the "expected failure" # concept: The test code should not bit-rot due to API changes but # always exercise the exact problem. @expect_failure def error_test(): raise ValueError('foobar') try: with assert_raises(ValueError, message='should treat errors (anything besides AssertionError) as error'): error_test() except _ExpectedFailure: # need to handle this exception explicitly as test runners (e.g. # nosetests on Python 2.7) can't tell which part of the code # (test code or the PythonicTestcase decorator) threw the exception. # This ensures our decorator really just catches AssertionError. self.fail('ValueError should cause errors')
def test_soap11_fault_handling(self): service = soap.Service( location='mock_location', methods=[], name=None, schemas=[], targetNamespace=None, version=soap.SOAPVersion.SOAP11, ) stub = soap.Stub(location='empty', service=service) e = assert_raises( core.SOAPError, lambda: stub._handle_response(None, None, SOAP11_ERROR_MESSAGE)) assert_equals('Result', e.code) assert_none(e.message) assert_equals('Resultset empty2.', e.actor)
def test_element_with_ref_attribute_rejects_forbidden_attributes(self): raise SkipTest('Elements with "ref" attribute currently do not restrict setting other attributes.') element = xsdspec.Element() element.ref = 'foo' element.minOccurs = 3 element.maxOccurs = '6' # element.id (not present in xsdspec.Element) def set_(attribute, value): return lambda: setattr(element, attribute, value) assert_raises(ValueError, set_('name', u'bar')) assert_raises(ValueError, set_('type', u'xs:string')) assert_raises(ValueError, set_('nillable', u'True')) simple_type = xsdspec.SimpleType(restriction=xsdspec.Restriction(base='string')) assert_raises(ValueError, set_('simpleType', simple_type)) # assert_raises(ValueError, set_('complexType', u'True')) element.ref = None # doesn't raise anymore because we deleted the "ref" attribute element.name = u'bar'
def test_tests_with_errors_marked_as_expected_failure_are_treated_as_errors( self): # This is actually a really important part of the "expected failure" # concept: The test code should not bit-rot due to API changes but # always exercise the exact problem. @expect_failure def error_test(): raise ValueError('foobar') try: with assert_raises( ValueError, message= 'should treat errors (anything besides AssertionError) as error' ): error_test() except _ExpectedFailure: # need to handle this exception explicitly as test runners (e.g. # nosetests on Python 2.7) can't tell which part of the code # (test code or the PythonicTestcase decorator) threw the exception. # This ensures our decorator really just catches AssertionError. self.fail('ValueError should cause errors')
def assert_fail(self, value, message=None): return assert_raises(AssertionError, lambda: assert_is_not_empty(value, message=message))
def test_passes_if_callable_raised_exception(self): assert_raises(ValueError, self._fail_with(ValueError()))
def assert_fail(self, value, message=None): return assert_raises(AssertionError, lambda: assert_falseish(value, message=message))
def test_returns_caught_exception_instance(self): expected_exception = ValueError('foobar') e = assert_raises(ValueError, self._fail_with(expected_exception)) assert expected_exception == e assert id(expected_exception) == id(e)
def assert_fail(self, first, second, message=None): return assert_raises(AssertionError, lambda: assert_smaller(first, second, message=message))
def test_callable_can_also_raise_assertion_error(self): expected_exception = AssertionError('foobar') e = assert_raises(AssertionError, self._fail_with(expected_exception)) assert expected_exception == e assert id(expected_exception) == id(e)
def test_fails_if_values_are_not_equal(self): assert_raises(AssertionError, lambda: assert_equals(1, 2))
def assert_fail(self, expr1, expr2, message=None): with assert_raises(AssertionError) as exc_context: assert_is_not(expr1, expr2, message=message) return exc_context.caught_exception
def test_wrong_type(self): mixed = xsd.Element(xsd.DateTime) xmlelement = etree.Element('foo') assert_raises(Exception, lambda: mixed.render(xmlelement, 'bar', 1))
def test_can_check_for_restrictions_before_accepting_values(self): xsd_string = xsd.String(enumeration=('10', '20', '30'), pattern='1.') assert_equals('10', xsd_string.accept('10')) assert_raises(ValueError, lambda: xsd_string.accept('15')) assert_raises(ValueError, lambda: xsd_string.accept('20'))
def test_fails_with_sensible_default_error_message(self): # using a string here on purpose so we can check that repr is used in # exception message e = assert_raises(AssertionError, lambda: assert_equals('foo', 'bar')) assert "'foo' != 'bar'" == exception_message(e), repr(exception_message(e))
def assert_fail(self, value, klass, message=None): return assert_raises(AssertionError, lambda: assert_isinstance(value, klass, message=message))
def assert_fail(self, first, second, message=None): return assert_raises( AssertionError, lambda: assert_greater(first, second, message=message))
def assert_fail(self, expected, actual, max_delta=None, message=None): assertion = lambda: assert_almost_equals(expected, actual, max_delta=max_delta, message=message) return assert_raises(AssertionError, assertion)
def test_fails_with_sensible_default_error_message(self): # using a string here on purpose so we can check that repr is used in # exception message e = assert_raises(AssertionError, lambda: assert_equals('foo', 'bar')) expected_str = '%r != %r' % ('foo', 'bar') assert expected_str == exception_message(e), repr(exception_message(e))
def assert_fail(self, value, klass, message=None): return assert_raises( AssertionError, lambda: assert_isinstance(value, klass, message=message))
def test_can_specify_additional_custom_message(self): e = assert_raises(AssertionError, lambda: assert_equals(1, 2, message='foo')) assert "1 != 2: foo" == exception_message(e), repr( exception_message(e))
def assert_fail(self, value, actual_iterable, message=None): return assert_raises(AssertionError, lambda: assert_not_contains(value, actual_iterable, message=message))
def assert_fail(self, expected, actual, message=None): return assert_raises( AssertionError, lambda: assert_not_equals(expected, actual, message=message))
def test_can_specify_additional_custom_message(self): e = assert_raises(AssertionError, lambda: assert_equals(1, 2, message='foo')) assert "1 != 2: foo" == exception_message(e), repr(exception_message(e))
def test_raises_exception_when_instantiating_invalid_dates(self): assert_raises(ValueError, lambda: XSDDate(2014, 2, 50)) assert_raises(ValueError, lambda: XSDDate(2014, 13, 10)) assert_raises(ValueError, lambda: XSDDate(2011, 2, 29))
def assert_fail(self, expected, actual, message=None): return assert_raises(AssertionError, lambda: assert_length(expected, actual, message=message))