Skip to content

Instantly share code, notes, and snippets.

@liladas
Created May 6, 2018 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liladas/a16f72d8ed3b283376d35825f9a32ebd to your computer and use it in GitHub Desktop.
Save liladas/a16f72d8ed3b283376d35825f9a32ebd to your computer and use it in GitHub Desktop.
Assertion Check
#!/usr/bin/env python3
import sys, logging
class FailedValidationException(Exception):
pass
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()
self.log.info("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 '''
self.passed_assertions.clear()
self.failed_assertions.clear()
for idx, check in enumerate(self.collect_assertion_checks()):
''' try to run each test, catch errors '''
check_passed = False
self.log.info("Running check {}: {} ...".format(idx, check))
try:
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))
else:
check_passed = True
self.passed_assertions.append(check)
finally:
self.log.info("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
self.log.info("{} 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