def test_logs_invalid_object(capsys): """ GithubReports Class Invalid Log Object Test This test will test the log method to ensure that if the logs constructor object is provided an invalid log object to write to that the invalid object will be ignored, resulting in all class logs being written to stdout, stderr. Expected Result: Invalid object ignored. Logged events are written to stdout, stderr. """ # Test to ensure that passing a non valid log object is properly caught. GitHubReportObj = GithubReports(verbose=True, log=42) assert (isinstance(GitHubReportObj, object)) assert (GitHubReportObj._log is None) # Write a test log entry to each log level GitHubReportObj.log("Pytest debug log write test", 'debug', 'test_log') GitHubReportObj.log("Pytest info log write test", 'info', 'test_log') GitHubReportObj.log("Pytest warning log write test", 'warning', 'test_log') GitHubReportObj.log("Pytest error log write test", 'error', 'test_log') # Capture stdout, stderr to test log messages out, err = capsys.readouterr() # sys.stdout.write(out) # sys.stderr.write(err) assert "DEBUG CLS->GitHubReports.test_log: \ -> Pytest debug log write test" in out assert "INFO CLS->GitHubReports.test_log: \ -> Pytest info log write test" in out assert "WARNING CLS->GitHubReports.test_log: \ -> Pytest warning log write test" in out assert "ERROR CLS->GitHubReports.test_log: \ -> Pytest error log write test" in err
def test_exception_handler(capsys): """ GithubReports Class Exception Handler Test This test will test the class wide exception handler. Expected Result: When an exception condition is encountered, exceptions will output in correct format to stderr. """ # Instantiate a GithubReports object, and test for expected test values. GitHubReportObj = GithubReports(verbose=True) assert (isinstance(GitHubReportObj, object)) # Write a log with a bad value to trigger an exception. GitHubReportObj.log('Message', 42, 2) # Capture stdout, stderr to check the log messages # for the expected outputs. out, err = capsys.readouterr() # sys.stdout.write(out) # sys.stderr.write(err) assert "ERROR CLS->GitHubReports.log: \ -> EXCEPTION occurred in: CLS->GitHubReports.log" in err
def test_logs_verbose_enabled(capsys): """ GithubReports Class Verbose Enabled Log Test This test will test to ensure that when verbose mode is enabled that all logs are written to stdout, stderr Expected Result: Logged events are written to stdout, stderr. Verbose enabled. """ # Instantiate a GithubReports object, and test for expected test values. GitHubReportObj = GithubReports(verbose=True) assert (isinstance(GitHubReportObj, object)) # Test verbose setting was correctly set to input True value assert (GitHubReportObj._verbose) assert (GitHubReportObj.verbose) # Write a test log entry to each log level GitHubReportObj.log("Pytest debug log write test", 'debug', 'test_log') GitHubReportObj.log("Pytest info log write test", 'info', 'test_log') GitHubReportObj.log("Pytest warning log write test", 'warning', 'test_log') GitHubReportObj.log("Pytest error log write test", 'error', 'test_log') # Capture stdout, stderr to test log messages out, err = capsys.readouterr() # sys.stdout.write(out) # sys.stderr.write(err) assert "DEBUG CLS->GitHubReports.test_log: \ -> Pytest debug log write test" in out assert "INFO CLS->GitHubReports.test_log: \ -> Pytest info log write test" in out assert "WARNING CLS->GitHubReports.test_log: \ -> Pytest warning log write test" in out assert "ERROR CLS->GitHubReports.test_log: \ -> Pytest error log write test" in err
def test_logs_verbose_disabled(capsys): """ GithubReports Class Verbose Disabled Log Test This test will test to ensure that when verbose mode is disabled that no logs other then errors are output. Expected Result: Logged events are silent, Error messages are output to the console. """ # Instantiate a GithubReports object # Test the returned object verbose attribute for expected values. GitHubReportObj = GithubReports(verbose=False) assert (isinstance(GitHubReportObj, object)) # Test verbose setting assert (not GitHubReportObj._verbose) # Write a test log entry GitHubReportObj.log("Pytest debug log write test", 'debug', 'test_log') GitHubReportObj.log("Pytest info log write test", 'info', 'test_log') GitHubReportObj.log("Pytest warning log write test", 'warning', 'test_log') GitHubReportObj.log("Pytest error log write test", 'error', 'test_log') # Capture stdout, stderr to test log messages out, err = capsys.readouterr() # sys.stdout.write(out) # sys.stderr.write(err) assert "DEBUG CLS->GitHubReports.test_log: \ -> Pytest debug log write test" not in out assert "INFO CLS->GitHubReports.test_log: \ -> Pytest info log write test" not in out assert "WARNING CLS->GitHubReports.test_log: \ -> Pytest warning log write test" not in out assert "ERROR CLS->GitHubReports.test_log: \ -> Pytest error log write test" in err
def test_logs_logger_object(capsys): """ GithubReports Class Log Object Test This test will test the log method to ensure that if the class constructor is passed a logger object object to write to that all class logs will be written to that provided object instead of stdout, stderr. Expected Result: Logged events are written to the provided log object. Verbose enabled. """ # Create a test log object that will just collect logs and add them to a # list, which we can check for produced log messages class Log(object): """Test Log Object""" def __init__(self): """Class Constructor""" self.debug_logs = [] self.info_logs = [] self.warning_logs = [] self.error_logs = [] def debug(self, message): """Log Debug Messages""" self.debug_logs.append(message) def info(self, message): """Log Info Messages""" self.info_logs.append(message) def warning(self, message): """Log Warning Messages""" self.warning_logs.append(message) def error(self, message): """Log Error Messages""" self.error_logs.append(message) # Instantiate a new log object to collect test logs. LogObj = Log() # Instantiate a GithubReports object, and test for expected test values. GitHubReportObj = GithubReports(verbose=True, log=LogObj) assert (isinstance(GitHubReportObj, object)) assert (isinstance(GitHubReportObj._log, object)) assert (isinstance(GitHubReportObj.log, object)) # Test verbose setting assert (GitHubReportObj._verbose) assert (GitHubReportObj.verbose) assert (GitHubReportObj._log is not None) # Test the Log object to make sure the expected object attributes exist assert (hasattr(GitHubReportObj._log, 'debug')) assert (hasattr(GitHubReportObj._log, 'info')) assert (hasattr(GitHubReportObj._log, 'warning')) assert (hasattr(GitHubReportObj._log, 'error')) assert (hasattr(GitHubReportObj._log, 'debug_logs')) assert (hasattr(GitHubReportObj._log, 'info_logs')) assert (hasattr(GitHubReportObj._log, 'warning_logs')) assert (hasattr(GitHubReportObj._log, 'error_logs')) # Write test log entries for each of the different types of logs GitHubReportObj.log("Pytest log debug test", 'debug', 'test_log_object') GitHubReportObj.log("Pytest log info test", 'info', 'test_log_object') GitHubReportObj.log("Pytest log warning test", 'warning', 'test_log_object') GitHubReportObj.log("Pytest log error test", 'error', 'test_log_object') # Test that the Log object debug_logs, info_logs, warning_logs and # error_logs properties are lists assert (isinstance(GitHubReportObj._log.debug_logs, list)) assert (isinstance(GitHubReportObj._log.info_logs, list)) assert (isinstance(GitHubReportObj._log.warning_logs, list)) assert (isinstance(GitHubReportObj._log.error_logs, list)) # Test that each of the log_lists have items written into them assert (len(LogObj.debug_logs) >= 1) assert (len(LogObj.info_logs) >= 1) assert (len(LogObj.warning_logs) >= 1) assert (len(LogObj.error_logs) >= 1) # Test the log messages to make sure they match the written logs assert (LogObj.debug_logs[-1] == "CLS->GitHubReports.test_log_object: \ -> Pytest log debug test") assert (LogObj.info_logs[-1] == "CLS->GitHubReports.test_log_object: \ -> Pytest log info test") assert (LogObj.warning_logs[-1] == "CLS->GitHubReports.test_log_object: \ -> Pytest log warning test") assert (LogObj.error_logs[-1] == "CLS->GitHubReports.test_log_object: \ -> Pytest log error test")