Skip to content

Instantly share code, notes, and snippets.



Created May 6, 2018
What would you like to do?
Assertion Check
#!/usr/bin/env python3
import sys, logging
class FailedValidationException(Exception):
class ClassToValidate(object):
def assert__testing_external(self):
return True
def assert__should_failed(self):
raise FailedValidationException("Failed external check")
class ClassWithAssertionChecks(object):
''' implements an assertion checker loop '''
def __init__(self, obj=None, log=None):
self.passed_assertions = []
self.failed_assertions = []
self.obj = obj if obj else self
self.passed_all_checks = False
self.log = log
if not self.log:
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
self.log = logging.getLogger('AssertionChecker')
import pdb; pdb.set_trace()"initializing assertion checker for {}".format(self.obj.__class__.__name__))
def collect_assertion_checks(self):
''' collect assertion check functions '''
return [func for func in dir(self.obj.__class__) if callable(getattr(self.obj.__class__, func)) and func.startswith("assert__")]
def check_assertions(self):
''' perform assertion check '''
for idx, check in enumerate(self.collect_assertion_checks()):
''' try to run each test, catch errors '''
check_passed = False"Running check {}: {} ...".format(idx, check))
getattr(self.obj.__class__, check)(self)
except AssertionError as e:
self.log.error("Assertion caught while checking {}".format(check), exc_info=True)
self.failed_assertions.append((check, e))
except Exception as e:
self.log.error("Exception caught while checking {}".format(check), exc_info=True)
self.failed_assertions.append((check, e))
check_passed = True
finally:"Check {}: {} {}".format(idx, check, "passed" if check_passed else "failed"))
num_passed = len(self.passed_assertions)
num_failed = len(self.failed_assertions)
num_checks = num_passed + num_failed"{} of {} assertion checks passed.".format(num_passed, num_checks))
self.log.warn("{} of {} assertion checks failed.".format(num_failed, num_checks))
self.passed_all_checks = num_failed == 0
return self.passed_all_checks
def assert__values_equal(self):
if 1234 == 1234:
return True
raise FailedValidationException("Failed equality check")
def assert__values_equal_fail(self):
if 1234 == 1235:
return True
raise FailedValidationException("Failed equality check")
def assert_num_rows_greater_than_zero(self):
num_rows = 1
if num_rows > 0:
return True
raise FailedValidationException("Failed number of rows check")
if __name__=="__main__":
result = ClassWithAssertionChecks().check_assertions()
print("\nDone.\nResult = {}".format("Passed" if result else "Failed"))
result = ClassWithAssertionChecks(ClassToValidate()).check_assertions()
print("\nDone.\nResult = {}".format("Passed" if result else "Failed"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.