def helper(capsys, terminal_input, expected_tail_of_terminal_output, intput_valid_accounts, expected_output_transactions): """Helper function for testing Arguments: capsys -- object created by pytest to capture stdout and stderr terminal_input -- list of string for terminal input expected_tail_of_terminal_output list of expected string at the tail of terminal intput_valid_accounts -- list of valid accounts in the valid_account_list_file expected_output_transactions -- list of expected output transactions """ # cleanup package reload(app) # create a temporary file in the system to store output transactions temp_fd, temp_file = tempfile.mkstemp() transaction_summary_file = temp_file # create a temporary file in the system to store the valid accounts: temp_fd2, temp_file2 = tempfile.mkstemp() valid_account_list_file = temp_file2 with open(valid_account_list_file, 'w') as wf: wf.write('\n'.join(intput_valid_accounts)) # prepare program parameters sys.argv = ['app.py', valid_account_list_file, transaction_summary_file] # set terminal input sys.stdin = io.StringIO(os.linesep.join(terminal_input)) # run the program app.main() # capture terminal output / errors # assuming that in this case we don't use stderr out, err = capsys.readouterr() # split terminal output in lines out_lines = out.splitlines() # compare terminal outputs at the end.` for i in range(1, len(expected_tail_of_terminal_output) + 1): index = i * -1 assert expected_tail_of_terminal_output[index] == out_lines[index] # compare transactions: with open(transaction_summary_file, 'r') as of: content = of.read().splitlines() for ind in range(len(content)): assert content[ind] == expected_output_transactions[ind] # clean up os.close(temp_fd) os.remove(temp_file)
def run_app(capsys, terminal_input, expected_terminal_tails, expected_output_file=None): """ a helper function that test requirements for the example app Arguments: capsys -- object created by pytest to capture stdout and stderr terminal_input : list -- list of string as terminal input for stdin stream expected_terminal_tails : list -- list of string to be matched at the tail of the terminal output expected_output_file : str -- the path to the correct output file [optional] """ # setup parameters temp_fd, temp_file = tempfile.mkstemp() sys.argv = ['app.py', temp_file] # set input sys.stdin = io.StringIO(os.linesep.join(terminal_input)) # run the program app.main() # capture terminal output / errors # assuming that in this case we don't use stderr out, err = capsys.readouterr() # split terminal output in lines out_lines = out.splitlines() # compare terminal outputs from the the last time backward` for i in range(1, len(expected_terminal_tails) + 1): index = i * -1 assert expected_terminal_tails[index] == out_lines[index] # compare expected file output: if expected_output_file is not None: with open(temp_file, 'r') as temp_file_of: content = temp_file_of.read() with open(expected_output_file, 'r') as exp_file_of: exp_content = exp_file_of.read() assert content == exp_content # clean up os.close(temp_fd) os.remove(temp_file)
from qa327.app import main if __name__ == "__main__": main()
def helper(capsys, test_id): """ a helper function that test requirements for the example app Arguments: capsys -- object created by pytest to capture stdout and stderr """ # cleanup package reload(app) # locate test case folder: case_folder = os.path.join(path, test_id) # read terminal input: with open(os.path.join(case_folder, 'terminal_input.txt')) as rf: terminal_input = rf.read().splitlines() # read expected tail portion of the terminal output: with open(os.path.join(case_folder, 'terminal_output_tail.txt')) as rf: terminal_output_tail = rf.read().splitlines() # create a temporary file in the system to store output transactions temp_fd, temp_file = tempfile.mkstemp() transaction_summary_file = temp_file # prepare program parameters sys.argv = [ 'app.py', os.path.join(case_folder, 'valid_account_list_file.txt'), transaction_summary_file ] # set terminal input sys.stdin = io.StringIO('\n'.join(terminal_input)) # run the program app.main() # capture terminal output / errors # assuming that in this case we don't use stderr out, err = capsys.readouterr() # split terminal output in lines out_lines = out.splitlines() # compare terminal outputs at the end.` for i in range(1, len(terminal_output_tail) + 1): index = i * -1 assert terminal_output_tail[index] == out_lines[index] # compare transactions: with open(transaction_summary_file, 'r') as of: content = of.read() with open(os.path.join(case_folder, 'transaction_summary_file.txt'), 'r') as exp_file_of: expected_content = exp_file_of.read() assert content == expected_content # clean up os.close(temp_fd) os.remove(temp_file)