def _create_test_case(self): """ Create Test case if testopia test case doesn't exist""" path = self._namespace.suite.source fn=open(self._namespace.suite.source) items=fn.read() regex = re.compile("Case.*\d{1,10}|\s.*:(?P<name>.*)(((?:\n|\r\n?).+)+)") find = regex.findall(items) tp = Testopia("*****@*****.**","password",'http://bugzilla/xmlrpc.cgi') plan_id = 1253 author_id = 1 isautomated = True category_id = 931 #tp.testcase_create('testing',plan_id,author_id,isautomated,category_id,1) for i in range(0,len(find)): alias= str(sys.argv[1].split('.')[0]) + " plan:"+str(plan_id)+" case: "+str(i) try: create_case=tp.testcase_create(find[i][0],int(plan_id),int(8),False,int(931),int(2),alias,'None',1,1) except: print "error\n" create_case=tp.testcase_create(find[i][0].split('(')[0],int(plan_id),int(8),False,int(931),int(2),alias,'None',1,1) try: store_case=tp.testcase_store_text(create_case['case_id'],1,"automation","*****@*****.**",find[i][1].replace('\n','<br>'), "Ensure it pass") except: pass
def main(argv=None): if argv is None: argv = sys.argv print "Command Line : %s" % argv try: try: opts, args = getopt.getopt(argv[1:], "ho:v", ["help", "output=", "mode=", "build_number="]) except getopt.error, msg: raise Usage(msg) #default values, this will be overwritten by command line args or envronment vars #command line args > envronment vars > default values #mode can be add_build, write_results, update_test_cases mode = "write_result" testopia_url = "http://11.22.33.44/" testopia_xmlrpc_url = None testopia_id = "*****@*****.**" testopia_password = "******" product_name = "PI_SampleProduct" environment = "xxxxxxxx" build_number = "0000" #replace default value by envronment vars logger.info("-------------Environment Variables-------------") for (k,v) in os.environ.items(): logger.info("%-20s : %s" % (k, v)) logger.info("-----------------------------------------------") if (os.environ.get("BUILD_NUMBER", None) != None): build_number = os.environ["BUILD_NUMBER"] if (os.environ.get("TESTOPIA_URL", None) != None): testopia_url = os.environ["TESTOPIA_URL"] if (os.environ.get("TESTOPIA_ID", None) != None): testopia_id = os.environ["TESTOPIA_ID"] if (os.environ.get("TESTOPIA_PASSWORD", None) != None): testopia_password = os.environ["TESTOPIA_PASSWORD"] if (os.environ.get("PRODUCT_NAME", None) != None): product_name = os.environ["PRODUCT_NAME"] if (os.environ.get("ENVIRONMENT", None) != None): environment = os.environ["ENVIRONMENT"] #replace default value by command line args for option, value in opts: if option == "-v": verbose = True if option in ("-h", "--help"): raise Usage(help_message) if option in ("-o", "--output"): output = value if option in ("--build_number"): build_number = value if option in ("--mode"): mode = value logger.info("-------------All Variables After Replace-------------") logger.info("%-20s : %s" % ("mode", mode)) logger.info("%-20s : %s" % ("testopia_url", testopia_url)) #testopia_xmlrpc_url = testopia_url + "bugzilla/xmlrpc.cgi" #logger.info(check_url(testopia_xmlrpc_url)) testopia_xmlrpc_url = testopia_url + "xmlrpc.cgi" logger.info("%-20s : %s" % ("testopia_xmlrpc_url", testopia_xmlrpc_url)) logger.info("%-20s : %s" % ("testopia_id", testopia_id)) logger.info("%-20s : %s" % ("testopia_password", testopia_password)) #if the build number get from jenkins it will looks like "job/deploy%20build/3/" build_number = "PI%s" % (build_number.strip('/').split('/')[-1].zfill(4)) logger.info("%-20s : %s" % ("build_number", build_number)) logger.info("%-20s : %s" % ("product_name", product_name)) logger.info("%-20s : %s" % ("environment", environment)) logger.info("%-20s : %s" % ("test_cases_result_path", test_cases_result_path)) logger.info("-------------Start Processing-------------") tp = Testopia(testopia_id, testopia_password, testopia_xmlrpc_url) author_id = get_testopia_author_id(tp, testopia_id) logger.info("author '%s' id is %d" % (testopia_id, author_id)) if (product_name == "PI_SampleProduct"): product_id = 12 else: product_id = get_testopia_product_id(tp, product_name) logger.info("product '%s' id is %d" % (product_name, product_id)) if (mode == "add_build") : build_description = "%s auto create this build" % str(datetime.now()) logger.info("create a build") build = tp.build_create(build_number, product_id, description=build_description) logger.info("build : %s" % (build)) elif (mode == "write_result") : build_id = get_testopia_build_id(tp, product_id, build_number) logger.info("build '%s' id is %d" % (build_number, build_id)) plan_id = get_testopia_testplan_id(tp, product_id, default_testplan_name) logger.info("testplan '%s' id is %d" % (default_testplan_name, plan_id)) environment_id = get_testopia_environment_id(tp, product_id, environment) logger.info("environment '%s' id is %d" % (environment, environment_id)) #create a test run test_run_summary = "auto create test run" test_run_notes = "%s auto create this run" % str(datetime.now()) logger.info("create a test run") testrun = tp.testrun_create(build_id, environment_id, plan_id, test_run_summary, author_id, plan_text_version=0, notes=test_run_notes, product_version='unspecified') logger.info("testrun : %s" % testrun) csv_reader = csv.reader(open(test_cases_result_path, 'rb'), delimiter=',', quotechar='"') for row in csv_reader: tc_alias = row[0] tc_summary = row[1] tc_result = row[2] #get test case id from alias case_id = get_testopia_testcase_id(tp, tc_alias) if (case_id != None): logger.info("test case '%s' id is %d" % (tc_alias, case_id)) #add test case to test run tp.testrun_add_cases(testrun['run_id'], case_id) #update test case result in this run case_run_status_id = get_testopia_caserun_status_id(tp, tc_result) #logger.info("case result '%s' id is %d" % (tc_result, case_run_status_id)) update_case_run = tp.testcaserun_update(testrun['run_id'], case_id, build_id, environment_id, case_run_status_id=case_run_status_id) logger.info("update case result %s(case_run_status_id :%s)" % (tc_result, update_case_run['case_run_status_id'])) else: logger.error("fail to get test case '%s' id" % tc_alias) elif (mode == "update_test_cases") : type_id = get_testopia_type_id(tp, "Unit") #check is testplan exist logger.info("check is testplan '%s' exist" % default_testplan_name) if (tp.testplan_is_exist(product_id, default_testplan_name)): logger.info("testplan '%s' is exist" % default_testplan_name) else: logger.info("testplan '%s' is not exist so create it" % default_testplan_name) tp.testplan_create(default_testplan_name, product_id, author_id, type_id, 'unspecified') plan_id = get_testopia_testplan_id(tp, product_id, default_testplan_name) logger.info("testplan '%s' id is %d" % (default_testplan_name, plan_id)) #update test cases to testplan csv_reader = csv.reader(open(test_cases_result_path, 'rb'), delimiter=',', quotechar='"') for row in csv_reader: tc_alias = row[0] tc_summary = row[1] if (tp.testcase_is_exist(tc_alias)) : logger.info("testcase '%s' is exist so update it" % tc_alias) testcase = tp.testcase_update(tc_alias, tc_summary, isautomated=True, case_status_id=2, priority_id=1) logger.debug("testcase : %s" % testcase) else : logger.info("testcase '%s' is not exist so create it" % tc_alias) testcase = tp.testcase_create(summary=tc_summary, plan_id=plan_id, author_id=author_id, isautomated=True, category_id=1, case_status_id=2, alias=tc_alias, priority_id=1) logger.debug("testcase : %s" % testcase)