def test(funct_name): # print out the names of functions which can be tested in `funct_name` is not in `tests`, and exit if funct_name not in tests: print "ERROR: '{0}' is not a recognised function name; select from: {1}".format( funct_name, str(tests.keys())) return -1 # run test (using valid function name) print "Testing the {0} function ...\n".format(funct_name) correct = 0 # number of tests passed # run the user-defined function with each of the sets of arguments provided in `tests`, and chech that the # output is correct for test in tests[funct_name]: print " testing {0} ...".format(test[0]), userval = eval( test[0] ) # run the function with the supplied set of arguments (take the string and execute it) expval = test[1] if funct_name in ['swap_cards', 'play']: # if the returned value is correct, increment `correct` and print a congratulatory print statement if test_equivalent_inset(userval, expval): correct += 1 print "passed" # if the returned value is *in*correct, print diagnostics else: print "failed" print " * expected val in (type '{0}') {1}".format( type(expval), expval) print " * returned = (type '{0}') {1}".format( type(userval), userval) else: # if the returned value is correct, increment `correct` and print a congratulatory print statement if test_equivalent(userval, expval): correct += 1 print "passed" # if the returned value is *in*correct, print diagnostics else: print "failed" print " * expected = (type '{0}') {1}".format( type(expval), expval) print " * returned = (type '{0}') {1}".format( type(userval), userval) # print the overall number of tests passed vs. attempted print "\n{0}/{1} tests passed for {2}".format(correct, len(tests[funct_name]), funct_name)
def test(funct_name): # print out the names of functions which can be tested in `funct_name` is not in `tests`, and exit if funct_name not in tests: print "ERROR: '{0}' is not a recognised function name; select from: {1}".format(funct_name,str(tests.keys())) return -1 # run test (using valid function name) print "Testing the {0} function ...\n".format(funct_name) correct = 0 # number of tests passed # run the user-defined function with each of the sets of arguments provided in `tests`, and chech that the # output is correct for test in tests[funct_name]: print " testing {0} ...".format(test[0]), userval = eval(test[0]) # run the function with the supplied set of arguments (take the string and execute it) expval = test[1] if funct_name in ['swap_cards', 'play']: # if the returned value is correct, increment `correct` and print a congratulatory print statement if test_equivalent_inset(userval,expval): correct += 1 print "passed" # if the returned value is *in*correct, print diagnostics else: print "failed" print " * expected val in (type '{0}') {1}".format(type(expval), expval) print " * returned = (type '{0}') {1}".format(type(userval), userval) else: # if the returned value is correct, increment `correct` and print a congratulatory print statement if test_equivalent(userval,expval): correct += 1 print "passed" # if the returned value is *in*correct, print diagnostics else: print "failed" print " * expected = (type '{0}') {1}".format(type(expval), expval) print " * returned = (type '{0}') {1}".format(type(userval), userval) # print the overall number of tests passed vs. attempted print "\n{0}/{1} tests passed for {2}".format(correct,len(tests[funct_name]),funct_name)
def run(*test_labels): results = ResultSet() run_server = globals()['run_' + SERVER_TARGET] run_client = globals()['run_' + CLIENT_TARGET] ordered = sorted(tests.keys()) tests_to_run = test_labels if not tests_to_run: tests_to_run = ordered else: t = [] for i in tests_to_run: if i in ordered: t.append(i) tests_to_run = t try: os.mkdir(OUTPUT_DIR) except: shutil.rmtree(OUTPUT_DIR) os.mkdir(OUTPUT_DIR) for name in tests_to_run: context = tests[name] server_context = context[SERVER_TARGET] client_context = context[CLIENT_TARGET] test = results.start(name) proxy_process = run_proxy(os.path.join(OUTPUT_DIR, name, name + '.carrays')) server_process = run_server(**server_context) client_process = run_client(**client_context) client_process.wait() server_process.terminate() proxy_process.terminate() client_pipes = { 'stdout': client_process.stdout.read().strip().split('\n'), 'stderr': client_process.stderr.read().strip().split('\n') } server_pipes = { 'stdout': server_process.stdout.read().strip().split('\n'), 'stderr': server_process.stderr.read().strip().split('\n') } proxy_pipes = { 'stdout': proxy_process.stdout.read().strip().split('\n'), 'stderr': proxy_process.stderr.read().strip().split('\n') } result = get_sikuli_result(client_pipes['stdout']) test.setServerPipes(server_pipes) test.setClientPipes(client_pipes) test.setProxyPipes(proxy_pipes) if result is None: test.unexpectedError() else: # call the correct method on the test object getattr(test, result)() test.finish() return results