refactor code from run_tests.py into Python module